Skip to content

fix: Avoid nil dereference when AdmissionRequest.RequestResource is unset - #9086

Merged
cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
lunarwhite:fix-nil-deref
Aug 10, 2026
Merged

cert-manager-prow[bot] merged 2 commits into
cert-manager:masterfrom
lunarwhite:fix-nil-deref

Conversation

@lunarwhite

@lunarwhite lunarwhite commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

Pull Request Motivation

Fixes #9064

The issue reports a nil-pointer panic in certificateRequestIdentity.Validate when an AdmissionReview is POSTed directly to the webhook /validate endpoint. The reporter's payload sets resource (Certificate CREATE) but omits requestResource. After JSON decode, RequestResource is nil, and the identity plugin panics on the first field access.

This is unlikely to show up in normal cluster operation. Normally API server sends the AdmissionReview to the webhook, so these fields are set without human intervention. Posting an AdmissionReview directly to the webhook is not a supported user path, but the shared validate chain still should not panic when optional fields are absent. So it's worth hardening.

The issue attributes this to a missing TLS client certificate, but TLS client verification is orthogonal and off by default in Helm.

There is an earlier PR: #9081 that adds a nil-guard around RequestResource in identity Validate. That addresses the symptom in a narrower scope rather than root cause.

This PR uses always-present Resource/SubResource for gating and lookup in identity, approval, and resourcevalidation. This matches the webhook-facing GVR and turns the failure into a normal admission response instead of a recovered panic.

Update per review feedback: Moving off RequestResource left two related gaps. A zero-valued Resource (e.g. from a non-conformant aggregated/virtual apiserver) would silently skip every plugin's gate and be Allowed instead of denied. And approval/resourcevalidation's bare type assertions could still panic on a crafted request whose object kind doesn't match its resource, since the decoder decodes by the payload's own kind, not by resource. The 2nd commit closes both: Resource is now checked for the zero value before each plugin's existing gate, and approval/resourcevalidation use checked type casts instead of bare assertions, matching identity's existing pattern.

Kind

/kind bug

Release Note

Fix validating webhook panics when AdmissionReview requests omit optional fields, by routing identity, approval, and resource validation on the always-present Resource/SubResource fields and denying (rather than silently allowing) requests with an unset or mismatched resource. As a side effect, validation is now also enforced for equivalent-converted requests on non-v1 API versions, which previously could skip validation.

Gate identity/approval and look up resourcevalidation on Resource/SubResource
instead of optional RequestResource/RequestSubResource, which can be nil on
incomplete AdmissionReviews and panics the validate chain.

Signed-off-by: Yuedong Wu <[email protected]>
@cert-manager-prow cert-manager-prow Bot added kind/bug Categorizes issue or PR as related to a bug. 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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 30, 2026
@wallrj
wallrj requested a lite review from Copilot August 9, 2026 09:26

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

Hardens the cert-manager admission webhook plugins against AdmissionReview payloads that omit optional requestResource / requestSubResource, preventing nil-pointer panics by consistently using the always-present resource / subResource fields for gating and lookups.

Changes:

  • Switched admission plugin routing/gating from AdmissionRequest.RequestResource/RequestSubResource to AdmissionRequest.Resource/SubResource (identity, approval, resource validation).
  • Simplified resourcevalidation to use the built-in validation mapping directly (removing an unused per-instance mapping field).
  • Updated unit tests and fuzz tests to populate Resource/SubResource and implicitly exercise nil RequestResource handling.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/webhook/admission/resourcevalidation/resourcevalidation.go Uses request.Resource for mapping key derivation to avoid nil deref when RequestResource is absent.
internal/webhook/admission/resourcevalidation/resourcevalidation_test.go Updates tests to set AdmissionRequest.Resource and removes mapping injection.
internal/webhook/admission/certificaterequest/identity/certificaterequest_identity.go Gates identity mutation/validation using request.Resource instead of optional RequestResource.
internal/webhook/admission/certificaterequest/identity/certificaterequest_identity_test.go Updates identity tests to use AdmissionRequest.Resource (avoids reliance on RequestResource).
internal/webhook/admission/certificaterequest/approval/fuzz_test.go Updates fuzz setup to set Resource/SubResource.
internal/webhook/admission/certificaterequest/approval/certificaterequest_approval.go Gates approval validation using Resource/SubResource instead of RequestResource/RequestSubResource.
internal/webhook/admission/certificaterequest/approval/certificaterequest_approval_test.go Updates approval tests to use Resource/SubResource.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 75 to 79
requestResource := schema.GroupVersionResource{
Group: request.RequestResource.Group,
Version: request.RequestResource.Version,
Resource: request.RequestResource.Resource,
Group: request.Resource.Group,
Version: request.Resource.Version,
Resource: request.Resource.Resource,
}
expectedWarnings []string
expectedError error
}{
"should not perform any validation if no validation functions are registered": {
@wallrj

wallrj commented Aug 9, 2026

Copy link
Copy Markdown
Member

Thanks for taking the root-cause route here rather than the narrow nil-guard in #9081 — gating on the always-present Resource/SubResource across identity, approval and resourcevalidation is the right shape, and this addresses #9064 more completely than #9081 does.

I ran a fairly thorough review over the diff (and cross-checked against #9081). A few things worth a look before merge, roughly in severity order:

1. The approval gate has flipped from fail-closed to fail-open (certificaterequest_approval.go:83)

The old gate keyed enforcement on request.RequestResource; the new one keys on request.Resource. There is no Resource-presence check anywhere in the dispatch chain, so an AdmissionReview that carries requestResource but a zero-valued resource now silently skips all three plugins and is Allowed — where master would have enforced (or panicked).

In practice the real kube-apiserver always sets resource, so this only bites non-conformant callers (aggregated/virtual API servers, conformance harnesses). But it's a behaviour change in the unsafe direction: the approval RBAC check gets skipped rather than run. Worth at least a sentence acknowledging it, and ideally a guard that treats a zero-valued Resource as "don't skip".

2. The crafted-payload panic class isn't fully closed (certificaterequest_approval.go:89, and resourcevalidation.go:91)

The bare assertions oldObj.(*certmanager.CertificateRequest) / obj.(*certmanager.CertificateRequest) still panic on a crafted payload. The decoder decodes by the payload's own kind, not by resource (custom_decoder.go:43), so a POST with resource=certificaterequests but an object whose kind is Certificate passes the gate and then panics on the assertion. controller-runtime v0.24.1 recovers this to a 500 — the same user-visible outcome as the #9064 panic this PR sets out to fix. The sibling identity.Validate already does a checked cast with a clean error; approval and resourcevalidation should match it. Otherwise the same crafted-POST class the PR is meant to harden against is only half-closed.

3. Legacy-version requests are now subject to v1 validation (resourcevalidation.go:76) — worth a release note

The lookup key moved from RequestResource.Version (the original requested version) to the converted Resource.Version (v1). Under matchPolicy: Equivalent, an equivalent-matched non-v1 request that previously missed the validation map and skipped validation is now enforced. Since CRDs have served only v1 since ~v1.7 this is almost certainly a latent-bug fix rather than a regression — but it's a stricter behaviour change that isn't mentioned in the description. A release-note line would be kind to anyone on very old CRDs.

Minor / cleanup

  • resourcevalidation.go:75: the five-line field-by-field copy into schema.GroupVersionResource can be the one-line conversion schema.GroupVersionResource(request.Resource) (identical fields; Go ignores struct tags in conversions). The local variable is also still named requestResource while now holding request.Resource — exactly the distinction this PR exists to fix, so worth renaming.
  • certificaterequest_identity_test.go:224: the new gating test covers the wrong-resource case but not the wrong-group branch of the gate, which TestMutate_Ignores does cover for Mutate. A refactor that drops the Group check would pass CI.
  • resourcevalidation_test.go:44: the name "should not perform any validation if no validation functions are registered" is now misleading — the injectable empty mapping was removed, so it runs against the real populated mapping and passes only because sample-group/sample-resource isn't in it.

Overall: this is the better of the two PRs and the right approach — I'd just like to see #1 and #2 resolved so we're not trading a recovered panic for a silent authorization skip (and two siblings still panicking) on the same non-standard inputs.

with claude fable-5

@cert-manager-prow cert-manager-prow Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 9, 2026
@lunarwhite

Copy link
Copy Markdown
Member Author

Checking failed e2e-test job, log indicates the local kind cluster's kube-apiserver became unreachable (EOF) partway through the run. For unit test job, there was a timing-dependent flakiness in a CSR-signing controller test.

/retest

@lunarwhite

lunarwhite commented Aug 9, 2026 •

Copy link
Copy Markdown
Member Author

@wallrj Thanks for your review. I've add a dedicated commit to address your comments: 5d6071b. Ready for your re-review when you're around. Thanks!

Check Resource for the zero value in identity, approval and
resourcevalidation before their existing gates, instead of relying on
the gate alone, which let a zero-valued Resource fall through as
Allowed rather than denied. Replace approval's and resourcevalidation's
bare type assertions with checked casts, which could otherwise still
panic on a crafted request whose object kind doesn't match its resource.

Also simplify the GVR conversion in resourcevalidation.go, add the
missing wrong-group test case, and rename the test whose name no
longer matches what it exercises.

Signed-off-by: Yuedong Wu <[email protected]>
@wallrj

wallrj commented Aug 10, 2026

Copy link
Copy Markdown
Member

Thanks @lunarwhite — this is a thorough fix, and the latest commit closes the loop nicely:

  • Unset Resource is now denied via the new admission.ErrResourceUnset rather than silently skipped, so the approval RBAC gate can't fail open on a hand-crafted AdmissionReview.
  • The bare type assertions are replaced with checked casts, plus the objType/checkType guard in resourcevalidation — a crafted payload whose object kind disagrees with its resource now returns a clean error instead of panicking past the decoder.
  • Gating moved onto the always-populated Resource/SubResource instead of the optional RequestResource.

Test coverage is solid (unset, type-mismatch and the fuzz update). The two remaining Copilot threads are outdated — they refer to the old requestResource local and test name, both already renamed here.

/approve
/lgtm
/label tide/merge-method-squash

with claude opus-4.8

@cert-manager-prow cert-manager-prow Bot added tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. lgtm Indicates that a PR is ready to be merged. labels Aug 10, 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 Aug 10, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 66dbc89 into cert-manager:master Aug 10, 2026
6 checks passed
@lunarwhite
lunarwhite deleted the fix-nil-deref branch August 10, 2026 12:20
@wallrj

wallrj commented Sep 1, 2026

Copy link
Copy Markdown
Member

/cherry-pick release-1.21

with claude fable-5

@cert-manager-bot

Copy link
Copy Markdown
Contributor

@wallrj: new pull request created: #9235

Details

In response to this:

/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 11, 2026

Copy link
Copy Markdown
Member

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

Nil pointer dereference in certificateRequestIdentity.Validate() when admission request lacks TLS client certificate

4 participants