Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service): use Connection header on HTTP requests #156

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion service/deployed_version/deployed_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (l *Lookup) httpRequest(logFrom utils.LogFrom) (rawBody []byte, err error)
}

// Set headers
req.Header.Set("Connection", "close")
for _, header := range l.Headers {
req.Header.Set(header.Key, header.Value)
}
Expand All @@ -190,7 +191,7 @@ func (l *Lookup) httpRequest(logFrom utils.LogFrom) (rawBody []byte, err error)
if err != nil {
// Don't crash on invalid certs.
if strings.Contains(err.Error(), "x509") {
err = fmt.Errorf("x509 (Cert invalid)")
err = fmt.Errorf("x509 (certificate invalid)")
jLog.Warn(err, logFrom, true)
return
}
Expand Down
8 changes: 6 additions & 2 deletions service/latest_version/filters/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ func (r *Require) DockerTagCheck(
req.Header.Set("Authorization", "Bearer "+token)
}
}
req.Header.Set("Connection", "close")
// Do the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("%s:%s - %s",
r.Docker.Image, tag, err)
}
defer resp.Body.Close()

// Parse the body
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%s:%s - %s",
Expand Down Expand Up @@ -215,6 +217,7 @@ func (d *DockerCheck) refreshDockerHubToken() (err error) {
if err != nil {
return err
}
req.Header.Set("Connection", "close")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
// Do the request
client := &http.Client{}
Expand All @@ -223,8 +226,9 @@ func (d *DockerCheck) refreshDockerHubToken() (err error) {
jLog.Error(err, utils.LogFrom{Primary: "docker-hub", Secondary: d.Image}, true)
return
}
defer resp.Body.Close()

// Parse the body
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(string(body))
Expand Down
3 changes: 2 additions & 1 deletion service/latest_version/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ func (l *Lookup) httpRequest(logFrom utils.LogFrom) (rawBody []byte, err error)
return
}

// Set headers
req.Header.Set("Connection", "close")
if l.Type == "github" && utils.DefaultIfNil(l.GetAccessToken()) != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", *l.GetAccessToken()))
}

client := &http.Client{Transport: customTransport}
resp, err := client.Do(req)

if err != nil {
// Don't crash on invalid certs.
if strings.Contains(err.Error(), "x509") {
Expand Down
8 changes: 5 additions & 3 deletions web/api/v1/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
func (api *API) basicAuth() mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
username, password, ok := r.BasicAuth()
if ok {
// Hash purely to prevent ConstantTimeCompare leaking lengths
Expand All @@ -51,6 +52,7 @@ func (api *API) basicAuth() mux.MiddlewareFunc {
}
}

w.Header().Set("Connection", "close")
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
Expand All @@ -59,9 +61,7 @@ func (api *API) basicAuth() mux.MiddlewareFunc {

// SetupRoutesAPI will setup the HTTP API routes.
func (api *API) SetupRoutesAPI() {
api.Router.HandleFunc("/api/v1/version", func(w http.ResponseWriter, r *http.Request) {
api.httpVersion(w, r)
})
api.Router.HandleFunc("/api/v1/version", api.httpVersion)
}

// SetupRoutesNodeJS will setup the HTTP routes to the NodeJS files.
Expand All @@ -85,6 +85,8 @@ func (api *API) SetupRoutesNodeJS() {
func (api *API) httpVersion(w http.ResponseWriter, r *http.Request) {
logFrom := utils.LogFrom{Primary: "apiVersion"}
api.Log.Verbose("-", logFrom, true)
w.Header().Set("Connection", "close")
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(api_types.VersionAPI{
Version: utils.Version,
BuildDate: utils.BuildDate,
Expand Down
2 changes: 2 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func NewRouter(cfg *config.Config, jLog *utils.JLog, hub *api_v1.Hub) *mux.Route

// WebSocket
api.Router.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Connection", "close")
defer r.Body.Close()
api_v1.ServeWs(api, hub, w, r)
})

Expand Down
1 change: 1 addition & 0 deletions webhook/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (w *WebHook) GetRequest() (req *http.Request) {

SetGitLabParameter(req, w.GetSecret())
}
req.Header.Set("Connection", "close")
w.SetCustomHeaders(req)
return
}
Expand Down