Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: close response body (#7900)

p0ny4e93b6376d97
diff --git a/backend/plugin/app/relay/relay_client.go b/backend/plugin/app/relay/relay_client.go
index ad65457817..1fee70e2ee 100644
--- a/backend/plugin/app/relay/relay_client.go
+++ b/backend/plugin/app/relay/relay_client.go
@@ -68,6 +68,7 @@ func (c *Client) Create(relayEndpoint string, payload *CreatePayload) (string, e
if err != nil {
return "", err
}
+ defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", errors.Errorf("failed to create external approval with status %v", resp.Status)
}
@@ -99,6 +100,7 @@ func (c *Client) UpdateApproval(relayEndpoint string, id string, payload *Update
if err != nil {
return err
}
+ defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("failed to create external approval with status %v", resp.Status)
}
@@ -115,6 +117,7 @@ func (c *Client) GetApproval(relayEndpoint string, id string) (*ResponsePayload,
if err != nil {
return nil, err
}
+ defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("failed to get external approval status with status %v", resp.Status)
}
diff --git a/backend/plugin/idp/oauth2/oauth2.go b/backend/plugin/idp/oauth2/oauth2.go
index 633c07216c..59fab54c1c 100644
--- a/backend/plugin/idp/oauth2/oauth2.go
+++ b/backend/plugin/idp/oauth2/oauth2.go
@@ -98,6 +98,7 @@ func (p *IdentityProvider) UserInfo(token string) (*storepb.IdentityProviderUser
log.Error("Failed to get user information", zap.String("token", token), zap.Error(err))
return nil, errors.Wrap(err, "failed to get user information")
}
+ defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("Failed to read response body", zap.String("token", token), zap.Error(err))
diff --git a/backend/plugin/vcs/gitlab/gitlab.go b/backend/plugin/vcs/gitlab/gitlab.go
index 439ca17e6d..01e3f8ee23 100644
--- a/backend/plugin/vcs/gitlab/gitlab.go
+++ b/backend/plugin/vcs/gitlab/gitlab.go
@@ -1216,6 +1216,7 @@ func tokenRefresher(instanceURL string, oauthCtx oauthContext, refresher common.
if err != nil {
return errors.Wrapf(err, "POST %s", url)
}
+ defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
diff --git a/backend/runner/backuprun/runner.go b/backend/runner/backuprun/runner.go
index 65950ed910..a510e8c57f 100644
--- a/backend/runner/backuprun/runner.go
+++ b/backend/runner/backuprun/runner.go
@@ -386,12 +386,14 @@ func (r *Runner) startAutoBackups(ctx context.Context) {
if hookURL == "" {
return
}
- if _, err := http.PostForm(hookURL, nil); err != nil {
+ resp, err := http.PostForm(hookURL, nil)
+ if err != nil {
log.Warn("Failed to POST hook URL",
zap.String("hookURL", hookURL),
zap.Int("databaseID", database.UID),
zap.Error(err))
}
+ defer resp.Body.Close()
}(database, backupName, backupSetting.HookURL)
r.backupWg.Add(1)
}
diff --git a/backend/tests/fake/bitbucket.go b/backend/tests/fake/bitbucket.go
index fcf8f3db5c..37fb0f8131 100644
--- a/backend/tests/fake/bitbucket.go
+++ b/backend/tests/fake/bitbucket.go
@@ -314,26 +314,29 @@ func (bb *Bitbucket) SendWebhookPush(repositoryID string, payload []byte) error
// Trigger all webhooks
for _, webhook := range r.webhooks {
- req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payload))
- if err != nil {
- return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.URL)
- }
-
- req.Header.Set("X-Event-Key", "repo:push")
-
- resp, err := bb.client.Do(req)
- if err != nil {
- return errors.Wrapf(err, "failed to send POST request to %q", webhook.URL)
- }
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return errors.Wrap(err, "failed to read response body")
- }
- if resp.StatusCode != http.StatusOK {
- return errors.Errorf("unexpected response status code %d, body: %s", resp.StatusCode, body)
+ if err := func() error {
+ req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payload))
+ if err != nil {
+ return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.URL)
+ }
+ req.Header.Set("X-Event-Key", "repo:push")
+ resp, err := bb.client.Do(req)
+ if err != nil {
+ return errors.Wrapf(err, "failed to send POST request to %q", webhook.URL)
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return errors.Wrap(err, "failed to read response body")
+ }
+ if resp.StatusCode != http.StatusOK {
+ return errors.Errorf("unexpected response status code %d, body: %s", resp.StatusCode, body)
+ }
+ bb.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
+ return nil
+ }(); err != nil {
+ return err
}
- bb.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
}
return nil
}
diff --git a/backend/tests/fake/github.go b/backend/tests/fake/github.go
index 18f2176db3..d3a4eb9d44 100644
--- a/backend/tests/fake/github.go
+++ b/backend/tests/fake/github.go
@@ -521,32 +521,37 @@ func (gh *GitHub) SendWebhookPush(repositoryID string, payload []byte) error {
// Trigger all webhooks
for _, webhook := range r.webhooks {
- req, err := http.NewRequest("POST", webhook.Config.URL, bytes.NewReader(payload))
- if err != nil {
- return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.Config.URL)
+ if err := func() error {
+ req, err := http.NewRequest("POST", webhook.Config.URL, bytes.NewReader(payload))
+ if err != nil {
+ return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.Config.URL)
+ }
+
+ m := hmac.New(sha256.New, []byte(webhook.Config.Secret))
+ if _, err := m.Write(payload); err != nil {
+ return errors.Wrap(err, "failed to calculate SHA256 of the webhook secret")
+ }
+ signature := "sha256=" + hex.EncodeToString(m.Sum(nil))
+ req.Header.Set("X-Hub-Signature-256", signature)
+ req.Header.Set("X-GitHub-Event", "push")
+
+ resp, err := gh.client.Do(req)
+ if err != nil {
+ return errors.Wrapf(err, "failed to send POST request to %q", webhook.Config.URL)
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return errors.Wrap(err, "failed to read response body")
+ }
+ if resp.StatusCode != http.StatusOK {
+ return errors.Errorf("unexpected response status code %d, body: %s", resp.StatusCode, body)
+ }
+ gh.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
+ return nil
+ }(); err != nil {
+ return err
}
-
- m := hmac.New(sha256.New, []byte(webhook.Config.Secret))
- if _, err := m.Write(payload); err != nil {
- return errors.Wrap(err, "failed to calculate SHA256 of the webhook secret")
- }
- signature := "sha256=" + hex.EncodeToString(m.Sum(nil))
- req.Header.Set("X-Hub-Signature-256", signature)
- req.Header.Set("X-GitHub-Event", "push")
-
- resp, err := gh.client.Do(req)
- if err != nil {
- return errors.Wrapf(err, "failed to send POST request to %q", webhook.Config.URL)
- }
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return errors.Wrap(err, "failed to read response body")
- }
- if resp.StatusCode != http.StatusOK {
- return errors.Errorf("unexpected response status code %d, body: %s", resp.StatusCode, body)
- }
- gh.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
}
return nil
}
diff --git a/backend/tests/fake/gitlab.go b/backend/tests/fake/gitlab.go
index dc5f2aabaf..8106f6c664 100644
--- a/backend/tests/fake/gitlab.go
+++ b/backend/tests/fake/gitlab.go
@@ -517,25 +517,31 @@ func (gl *GitLab) SendWebhookPush(projectID string, payload []byte) error {
// Trigger webhooks.
for _, webhook := range pd.webhooks {
- // Send post request.
- req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payload))
- if err != nil {
- return errors.Wrapf(err, "fail to create a new POST request(%q)", webhook.URL)
+ if err := func() error {
+ // Send post request.
+ req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payload))
+ if err != nil {
+ return errors.Wrapf(err, "fail to create a new POST request(%q)", webhook.URL)
+ }
+ req.Header.Set("X-Gitlab-Token", webhook.SecretToken)
+ resp, err := gl.client.Do(req)
+ if err != nil {
+ return errors.Wrapf(err, "fail to send a POST request(%q)", webhook.URL)
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return errors.Wrap(err, "failed to read http response body")
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return errors.Errorf("http response error code %v body %q", resp.StatusCode, string(body))
+ }
+ gl.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
+ return nil
+ }(); err != nil {
+ return err
}
- req.Header.Set("X-Gitlab-Token", webhook.SecretToken)
- resp, err := gl.client.Do(req)
- if err != nil {
- return errors.Wrapf(err, "fail to send a POST request(%q)", webhook.URL)
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return errors.Wrap(err, "failed to read http response body")
- }
-
- if resp.StatusCode != http.StatusOK {
- return errors.Errorf("http response error code %v body %q", resp.StatusCode, string(body))
- }
- gl.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
}
return nil
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index 35530f772e..f39c646475 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -1887,7 +1887,7 @@ func postVCSSQLReview(ctl *controller, gitOpsInfo *v1pb.ProjectGitOpsInfo, reque
if err != nil {
return nil, err
}
-
+ defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
diff --git a/backend/tests/tests.go b/backend/tests/tests.go
index 38d710d2c0..9f569a0779 100644
--- a/backend/tests/tests.go
+++ b/backend/tests/tests.go
@@ -675,6 +675,7 @@ func (ctl *controller) Signup() error {
if err != nil {
return errors.Wrap(err, "fail to post login request")
}
+ defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "failed to read body")
@@ -694,7 +695,7 @@ func (ctl *controller) Login() error {
if err != nil {
return errors.Wrap(err, "fail to post login request")
}
-
+ defer resp.Body.Close()
cookie := ""
h := resp.Header.Get("Set-Cookie")
parts := strings.Split(h, "; ")