api: fix response body leak when rawRequestWithContext returns error#31901
api: fix response body leak when rawRequestWithContext returns error#31901raman1236 wants to merge 3 commits into
Conversation
rawRequestWithContext can return both a non-nil response and a non-nil
error (e.g., for non-2xx status codes or redirect errors). In several
API client methods, the response body was only closed when err was nil:
if err == nil {
defer resp.Body.Close()
}
This means when an error occurs with a valid response, the body is
never closed, causing a resource leak. The fix changes the check to:
if resp != nil {
defer resp.Body.Close()
}
This ensures the response body is always properly closed regardless
of whether an error is returned, matching the pattern already used
in rawRequestWithContext itself and other API methods.
|
@ramanvasi is attempting to deploy a commit to the HashiCorp Team on Vercel. A member of the Team first needs to authorize it. |
|
Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement Learn more about why HashiCorp requires a CLA and what the CLA includes 1 out of 2 committers have signed the CLA.
Have you signed the CLA already but the status is still pending? Recheck it. |
|
@raman1236 please ensure that any commits made with your email, etc, all line up to the same github account information, otherwise the CLA bot gets confused |
|
@heatherezell Thanks for the heads up! I've signed the CLA with my |
|
Friendly ping — this PR has been open for a few weeks now. The CLA is signed, tests pass, and the fix is a straightforward one-liner that prevents response body leaks in the API client. Would love a review when you get a chance. Thanks! |
a7dc6fc to
b9faad7
Compare
Description
Fix a resource leak in the Vault API client where HTTP response bodies are not closed when
rawRequestWithContextreturns both a non-nil response and a non-nil error.Problem
Several API client methods check
if err == nilbefore closing the response body:However,
rawRequestWithContextcan return a non-nil response along with a non-nil error (e.g., for non-2xx HTTP status codes, redirect errors, etc.). When this happens, the response body is never closed, causing a resource leak that can gradually exhaust system resources.Fix
Changed the check from
if err == niltoif resp != nil:This matches the pattern already used within
rawRequestWithContextitself (client.golines 1522-1524) and ensures the response body is always properly closed regardless of whether an error is returned.Affected Methods (11 files, 23 instances)
sys_audit.go:DisableAuditWithContextsys_auth.go:DisableAuthWithContextsys_config_cors.go:ConfigureCORSWithContext,DisableCORSWithContextsys_generate_root.go:generateRootCancelCommonWithContextsys_leases.go:RevokeWithContext,RevokePrefixWithContext,RevokeForceWithContext,RevokeWithOptionsWithContextsys_mounts.go:UnmountWithContext,TuneMountAllowNilWithContextsys_plugins.go:RegisterPluginWithContext,DeregisterPluginWithContextsys_plugins_runtimes.go:RegisterPluginRuntime,DeregisterPluginRuntimesys_policy.go:DeletePolicyWithContextsys_rekey.go:RekeyCancelWithContextWithNonce,RekeyRecoveryKeyCancelWithContextWithNonce,RekeyVerificationCancelWithContext,RekeyRecoveryKeyVerificationCancelWithContext,RekeyDeleteBackupWithContext,RekeyDeleteRecoveryBackupWithContextsys_rotate.go:RotateWithContext