Skip to content

fix(inspect): close CRL response body on io.ReadAll error path#463

Merged
cert-manager-prow[bot] merged 1 commit intocert-manager:mainfrom
SAY-5:fix/inspect-secret-defer-body-close-442
Apr 30, 2026
Merged

fix(inspect): close CRL response body on io.ReadAll error path#463
cert-manager-prow[bot] merged 1 commit intocert-manager:mainfrom
SAY-5:fix/inspect-secret-defer-body-close-442

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented Apr 22, 2026

Closes #442.

Bug

`checkCRLValidCert` in `pkg/inspect/secret/util.go` closed `resp.Body` only on the success path:

```go
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("error getting HTTP response: %w", err)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("error reading HTTP body: %w", err) // ← body leaked
}
resp.Body.Close()
```

Any time `io.ReadAll` returned a non-nil error (truncated response, transport reset, peer TLS close, etc.) the function returned early and the underlying TCP connection was leaked. `bodyclose` doesn't catch this class of bug because the analyzer treats the body as handled as long as `Close` appears anywhere in the function, without verifying reachability from every error-return — same gap as the upstream `httpresponse` analyzer (golang/go#75902).

Fix

One-line edit: add `defer resp.Body.Close()` immediately after the successful `http.Do()` and drop the explicit post-read call. The body is now closed from every return site.

Testing

  • `go build ./...` — clean.
  • `go vet ./...` — clean.
  • `go test ./pkg/inspect/secret/...` — one unrelated pre-existing failure in `Test_describeTrusted` (a macOS/Go cert-string drift: `certificate is not trusted` vs `signed by unknown authority`) which is not touched by this PR and reproduces on `main` before the change.

Signed-off-by: SAY-5 SAY-5@users.noreply.github.com

`checkCRLValidCert` in `pkg/inspect/secret/util.go` closed `resp.Body`
only on the success path. If `io.ReadAll` returned a non-nil error
(truncated response, transport reset, etc.) the function returned
early with the body still open, leaking the underlying TCP
connection. `bodyclose` doesn't flag this because it considers the
body handled as long as `Close` appears anywhere in the function, even
if it's unreachable on error paths.

Switch to a `defer resp.Body.Close()` immediately after the successful
`http.Do()` and drop the explicit post-read call -- now the body is
always closed regardless of which return site fires.

Closes cert-manager#442.

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
@cert-manager-prow cert-manager-prow Bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 22, 2026
@cert-manager-prow
Copy link
Copy Markdown
Contributor

Hi @SAY-5. Thanks for your PR.

I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@cert-manager-prow cert-manager-prow Bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 22, 2026
@erikgb
Copy link
Copy Markdown
Member

erikgb commented Apr 30, 2026

/ok-to-test

@cert-manager-prow cert-manager-prow Bot added ok-to-test and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 30, 2026
@erikgb erikgb requested a review from Copilot April 30, 2026 18:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an HTTP response body leak in checkCRLValidCert so the CRL fetch connection is closed even when io.ReadAll fails (addressing #442).

Changes:

  • Add defer resp.Body.Close() immediately after a successful http.DefaultClient.Do(req).
  • Remove the explicit resp.Body.Close() call that only ran on the success path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@erikgb erikgb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SAY-5! 🚀

/lgtm
/approve

@cert-manager-prow cert-manager-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Apr 30, 2026
@cert-manager-prow
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: erikgb

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 30, 2026
@cert-manager-prow cert-manager-prow Bot merged commit 35c7825 into cert-manager:main Apr 30, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. lgtm Indicates that a PR is ready to be merged. ok-to-test size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

inspect secret: response body not closed on error path during CRL check

3 participants