api: fix response body leak when rawRequestWithContext returns error#31901
Open
raman1236 wants to merge 2 commits intohashicorp:mainfrom
Open
api: fix response body leak when rawRequestWithContext returns error#31901raman1236 wants to merge 2 commits intohashicorp:mainfrom
raman1236 wants to merge 2 commits intohashicorp:mainfrom
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. |
Contributor
|
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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