Repository navigation
fix(vault): keep Vault response bodies out of Issuer status - #9206
cert-manager-prow[bot] merged 4 commits into
Conversation
spec.vault.server is not restricted, so the endpoint being dialled during Setup() is not necessarily Vault. When it returned a non-2xx response whose body could not be decoded as a Vault error response, the Vault client handed the body back verbatim and we copied it straight into the Ready condition of the Issuer. That is persisted to the API server and is readable by anyone who can read the Issuer, turning the issuer into a way to read back the responses of arbitrary reachable endpoints. Sanitise both failing paths in Setup(): client initialisation and the initialized-and-unsealed health check. Non-Vault error responses now report only the status code; genuine Vault error responses are still reflected but are truncated so that even a well behaved server cannot fill the status. The full error is logged at debug level, also truncated so an unbounded response body cannot flood the log. The Issuer controllers copy the error returned by Setup() into a Kubernetes Warning Event, so the returned error is sanitised too, not just the condition message. The four Vault login helpers flattened errors with %s and err.Error(), which destroyed the *vault.ResponseError type before it could be inspected. They now wrap with %w; the rendered text is unchanged. The UTF-8 safe truncation added for the equivalent ACME fix moves from pkg/issuer/acme to pkg/util/errors so that both issuers share one implementation rather than duplicating security relevant logic. Signed-off-by: Felix Phipps <[email protected]>
|
/kind bug |
There was a problem hiding this comment.
Pull request overview
This pull request mitigates a response-reflection issue in the Vault Issuer setup path by ensuring non-Vault HTTP response bodies are not propagated into persisted Issuer conditions or Kubernetes Events, while still keeping useful Vault-native error details (bounded) for operators.
Changes:
- Introduces shared UTF-8-safe message truncation utilities in
pkg/util/errorsand reuses them from the ACME issuer. - Adds Vault-specific sanitization helpers (
SafeErrorMessage/LoggableErrorMessage) and uses them in Vault IssuerSetup()to prevent leaking raw response bodies into status/events. - Updates Vault login helpers to wrap errors with
%wto preserve*vault.ResponseErrortyping through wrapping, and adds targeted tests for the leak regression.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/util/errors/errors.go | Adds shared truncation marker + UTF-8-safe TruncateMessage helper for bounded persisted messages. |
| pkg/util/errors/errors_test.go | Adds unit coverage for truncation behavior (including rune-safety and invalid UTF-8). |
| pkg/issuer/vault/setup.go | Switches Vault setup error propagation to sanitized messages and adds structured logging helper. |
| pkg/issuer/vault/setup_test.go | Adds end-to-end regression test ensuring non-Vault bodies don’t reach condition messages or returned errors. |
| pkg/issuer/acme/setup.go | Reuses shared truncation utility instead of local truncateMessage. |
| pkg/issuer/acme/setup_test.go | Updates truncation marker reference after moving helper to shared package. |
| internal/vault/vault.go | Changes error wrapping from %s to %w to preserve wrapped error types for inspection. |
| internal/vault/errors.go | Adds SafeErrorMessage/LoggableErrorMessage to prevent reflected SSRF response bodies in persisted surfaces. |
| internal/vault/errors_test.go | Adds unit tests for Vault error sanitization and message/log length bounds. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
TruncateMessage reserves room for the truncation marker inside maxLen, so the returned string is never longer than the limit. The assertion in TestSafeErrorMessageBoundsLength still allowed the marker on top of it, a leftover from the private truncateMessage helper which appended the marker past the limit. The failure message already claimed the tighter bound, and the equivalent assertions in internal/vault/errors_test.go use it, so this makes the three consistent and lets the test actually prove that the marker fits within the budget. Signed-off-by: Felix Phipps <[email protected]>
Applying SafeErrorMessage in Setup() only closed the disclosure for the
Issuer. The signing controllers pass the errors from New and Sign
straight to Reporter.Pending and Reporter.Failed, which render them into
a Kubernetes Event and into the CertificateRequest Ready condition, and
the experimental CertificateSigningRequest path does the same. A user
who can create a CertificateRequest against the issuer could therefore
still reach the response body that this change set out to hide.
Sanitise once inside internal/vault instead, so New, Sign and
IsVaultInitializedAndUnsealed never hand back a raw body and a future
call site cannot reintroduce the leak by forgetting to ask for a safe
message. The returned error keeps the original in its chain, so
LoggableErrorMessage still recovers the full response for the logs and
errors.Is and errors.As continue to match on what the server returned.
Only errors carrying a *vault.ResponseError are wrapped. Everything else
is handed back untouched, because cmerrors.IsInvalidData type-asserts
rather than using errors.As and the signing controller relies on it to
decide not to retry.
Sign now wraps the response error with %w rather than stringifying it,
so it can be recognised on the way out.
Also record that the RawError guard is best-effort: RawError is false
whenever the body parsed as Vault's error envelope, which is not the
same as Vault having written it. Any endpoint answering with
{"errors": ["..."]} decodes cleanly and is still reflected, bounded only
by the truncation. Vault errors carry no marker equivalent to ACME's
urn:ietf:params:acme:error prefix that could distinguish the two.
Signed-off-by: Felix Phipps <[email protected]>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Pull request overview
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Suppressed comments (1)
internal/vault/vault.go:181
- This error uses
%swhich stringifieserrand loses the unwrap chain. Using%wwould preserve the original error forerrors.Is/Asand aligns with the changes elsewhere in this PR that explicitly preserve error types through wrapping.
csr, err := pki.DecodeX509CertificateRequestBytes(csrPEM)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode CSR for signing: %s", err)
}
Setup returned fmt.Errorf("%s", msg), which discarded the chain so that
errors.Is and errors.As could not match on what had gone wrong. Nothing
matches on it today, but now that internal/vault sanitises on the way
out there is no reason to throw the chain away: the error's own message
is already the safe one, so returning it whole gives the Kubernetes
Event the same text and keeps the chain for free.
SafeErrorMessage stays for the status condition, where it still earns
its place by bounding the length of a purely local error, which
sanitizeError deliberately leaves alone.
Also guard TruncateMessage against a non-positive bound. A negative
maxLen panicked on the slice, and while no caller passes one it is a
shared helper and the check is a line.
Signed-off-by: Felix Phipps <[email protected]>
|
Thanks @FelixPhipps — sanitising once inside /approve with claude fable-5 |
|
@wallrj: once the present PR merges, I will cherry-pick it on top of DetailsIn response to this:
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. |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wallrj The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
@wallrj: new pull request created: #9262 DetailsIn response to this:
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. |
|
Post-merge check: the sanitisation path is visible in the E2E logs. From pull-cert-manager-master-e2e-v1-36 at f550b3e, cert-manager-controller log:
So legitimate Vault errors still reach operators unmangled, and the redaction branch would only trigger against a non-Vault endpoint, which the E2E suite deliberately does not include. with claude fable-5 |
|
@FelixPhipps This change has been included in v1.21.2, which is now published. If you are able to install the release and verify that the change works as expected in your environment, that would be much appreciated. Thank you for the contribution. |
Description
Problem
spec.vault.server is an unrestricted URL, so the endpoint the Vault issuer dials during
Setup()is not necessarily Vault — it can be any address reachable from the controller. When that endpoint returned a non-2xx response whose body could not be decoded as a Vault error document,hashicorp/vault/apisetResponseError.RawErrorand handed the body back verbatim, andSetup()copied it straight intoIssuer.status.conditions[Ready].message:kubectl get issuer -o yaml, turning the Issuer into a way to read back responses from arbitrary internal endpoints (the SSRF primitive itself is tracked in cert-manager: SSRF viaIssuer.spec.vault.server#8756).vaultinternal.New(...), and the health check viaIsVaultInitializedAndUnsealed().Setup()into a Warning Event, so sanitising only the condition message would leave the same content exposed one layer up.Solution
Adds
vaultinternal.SafeErrorMessage, which useserrors.AsType[*vault.ResponseError]so a wrapped error cannot bypass the check, then branches onRawError— set precisely when the body was not a Vault error document. Those responses now report the status code only; genuine Vault errors are still reflected but truncated to 1 KiB. The full error is logged at debug level, truncated to 8 KiB so an unbounded body cannot flood the log. The sanitised message is used for the returned error as well as the condition.Four Vault login helpers flattened errors with %s and
err.Error(), destroying the*vault.ResponseErrortype before it could be inspected; they now wrap with %w, with no change to the rendered text. The UTF-8-safe truncation added for the equivalent ACME fix (#9202) moves frompkg/issuer/acmetopkg/util/errorsso both issuers share one implementation. Legitimate Vault servers behave unchanged.Test plan
TestVault_SetupDoesNotLeakResponseBody— anhttptest.Serverreturning 403 with a non-JSON sentinel body, covering both the client-init path (AppRole) and the health-check path (token auth); asserts the sentinel appears in neither the condition message nor the returned error.RawErrorbranch, and reverting the AppRole site to %s — each reproduces the leak and fails the test.TestSafeErrorMessage— local errors, raw bodies (bare and wrapped), and genuine Vault errors; every case asserts the sentinel is absent and the 1 KiB bound holds.TestSafeErrorMessageBoundsLength/TestLoggableErrorMessageBoundsLength— an oversized Vault error is marked truncated and stays within bounds.TestTruncateMessagemoved topkg/util/errorsunchanged; existingTestVault_Setupcases pass without modification.go build ./..., go vet, and gofmt clean; tests green across internal/vault, pkg/util/errors, pkg/issuer/..., and pkg/controller/{issuers,clusterissuers,certificaterequests}/....Release Note