Skip to content

fix(vault): keep Vault response bodies out of Issuer status - #9206

Merged
cert-manager-prow[bot] merged 4 commits into
cert-manager:masterfrom
FelixPhipps:fix/vault-setup-response-body-disclosure
Sep 3, 2026
Merged

cert-manager-prow[bot] merged 4 commits into
cert-manager:masterfrom
FelixPhipps:fix/vault-setup-response-body-disclosure

Conversation

@FelixPhipps

Copy link
Copy Markdown
Member

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/api set ResponseError.RawError and handed the body back verbatim, and Setup() copied it straight into Issuer.status.conditions[Ready].message:

  • Response reflection: the raw body is persisted to the API server and readable by anyone who can 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 via Issuer.spec.vault.server #8756).
  • Two affected paths: client initialisation via vaultinternal.New(...), and the health check via IsVaultInitializedAndUnsealed().
  • Escalation via Events: pkg/controller/issuers/sync.go copies the error returned by Setup() into a Warning Event, so sanitising only the condition message would leave the same content exposed one layer up.
  • Unbounded size: even a genuine Vault error was copied in full, so a large response could bloat the persisted status.

Solution

Adds vaultinternal.SafeErrorMessage, which uses errors.AsType[*vault.ResponseError] so a wrapped error cannot bypass the check, then branches on RawError — 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.ResponseError type 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 from pkg/issuer/acme to pkg/util/errorsso both issuers share one implementation. Legitimate Vault servers behave unchanged.

Test plan

  • TestVault_SetupDoesNotLeakResponseBody — an httptest.Server returning 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.
  • Verified the test is load-bearing by mutation: neutering the RawError branch, 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.
  • TestTruncateMessage moved to pkg/util/errors unchanged; existing TestVault_Setup cases 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

Fixed an issue where the body of a non-Vault HTTP response from `spec.vault.server` could be copied into the Vault Issuer's Ready condition and its Kubernetes Events. Such responses now report only the HTTP status code, and Vault's own error messages are truncated before being persisted.```

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]>
@cert-manager-prow cert-manager-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Aug 27, 2026
@FelixPhipps

Copy link
Copy Markdown
Member Author

/kind bug

@cert-manager-prow cert-manager-prow Bot added kind/bug Categorizes issue or PR as related to a bug. area/acme Indicates a PR directly modifies the ACME Issuer code area/vault Indicates a PR directly modifies the Vault Issuer code size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Aug 27, 2026
@wallrj
wallrj requested a lite review from Copilot August 28, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/errors and reuses them from the ACME issuer.
  • Adds Vault-specific sanitization helpers (SafeErrorMessage / LoggableErrorMessage) and uses them in Vault Issuer Setup() to prevent leaking raw response bodies into status/events.
  • Updates Vault login helpers to wrap errors with %w to preserve *vault.ResponseError typing 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.

Comment thread pkg/issuer/acme/setup_test.go Resolved
Comment thread pkg/util/errors/errors_test.go Resolved
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]>
Comment thread internal/vault/errors.go Resolved
Comment thread internal/vault/errors.go Resolved
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]>
@wallrj
wallrj requested a balanced review from Copilot September 1, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 %s which stringifies err and loses the unwrap chain. Using %w would preserve the original error for errors.Is/As and 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)
	}

Comment thread pkg/issuer/vault/setup.go Resolved
Comment thread pkg/issuer/vault/setup.go Resolved
Comment thread pkg/util/errors/errors.go Resolved
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]>
@wallrj

wallrj commented Sep 3, 2026

Copy link
Copy Markdown
Member

Thanks @FelixPhipps — sanitising once inside internal/vault closes the leak at every sink, TestExportedFunctionsDoNotLeakResponseBodies pins it, and the follow-up fixes for the review comments all check out. Same class of fix as #9202, so cherry-picking to release-1.21 to match.

/approve
/lgtm
/label tide/merge-method-squash
/cherry-pick release-1.21

with claude fable-5

@cert-manager-prow cert-manager-prow Bot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Sep 3, 2026
@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj: once the present PR merges, I will cherry-pick it on top of release-1.21 in a new PR and assign it to you.

Details

In response to this:

Thanks @FelixPhipps — sanitising once inside internal/vault closes the leak at every sink, TestExportedFunctionsDoNotLeakResponseBodies pins it, and the follow-up fixes for the review comments all check out. Same class of fix as #9202, so cherry-picking to release-1.21 to match.

/approve
/lgtm
/label tide/merge-method-squash
/cherry-pick release-1.21

with claude fable-5

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 lgtm Indicates that a PR is ready to be merged. label Sep 3, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[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

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 Sep 3, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit c84f58b into cert-manager:master Sep 3, 2026
6 checks passed
@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj: new pull request created: #9262

Details

In response to this:

Thanks @FelixPhipps — sanitising once inside internal/vault closes the leak at every sink, TestExportedFunctionsDoNotLeakResponseBodies pins it, and the follow-up fixes for the review comments all check out. Same class of fix as #9202, so cherry-picking to release-1.21 to match.

/approve
/lgtm
/label tide/merge-method-squash
/cherry-pick release-1.21

with claude fable-5

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.

@wallrj

wallrj commented Sep 3, 2026

Copy link
Copy Markdown
Member

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:

  • logVaultError fires: 417 warn-level setup.go:181 "Failed to initialize Vault client" lines carrying the safe message.
  • Genuine Vault error envelopes pass through intact, e.g. Code: 400. Errors: * invalid role name "lsmdtz24v6-role" and * invalid role ID from the create-vault-issuer tests appear in full in the Event text — parsed as Vault's envelope, so kept, bounded by the 1 KiB truncation.
  • Zero hits for response body has been omitted and the (truncated) marker — expected: the suite only talks to a real Vault, which always returns the envelope, and no error exceeded 1024 bytes. The redaction branch is covered by TestExportedFunctionsDoNotLeakResponseBodies.

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
FelixPhipps deleted the fix/vault-setup-response-body-disclosure branch September 3, 2026 09:09
@wallrj wallrj mentioned this pull request Sep 3, 2026
39 of 41 tasks
@wallrj

wallrj commented Sep 11, 2026

Copy link
Copy Markdown
Member

@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.

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. area/acme Indicates a PR directly modifies the ACME Issuer code area/vault Indicates a PR directly modifies the Vault Issuer code dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants