Repository navigation
fix: Avoid nil dereference when AdmissionRequest.RequestResource is unset - #9086
Conversation
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]>
There was a problem hiding this comment.
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/RequestSubResourcetoAdmissionRequest.Resource/SubResource(identity, approval, resource validation). - Simplified
resourcevalidationto use the built-in validation mapping directly (removing an unused per-instance mapping field). - Updated unit tests and fuzz tests to populate
Resource/SubResourceand implicitly exercise nilRequestResourcehandling.
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.
| 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": { |
|
Thanks for taking the root-cause route here rather than the narrow nil-guard in #9081 — gating on the always-present 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 (
|
|
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 |
5cc7329 to
5d6071b
Compare
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]>
|
Thanks @lunarwhite — this is a thorough fix, and the latest commit closes the loop nicely:
Test coverage is solid (unset, type-mismatch and the fuzz update). The two remaining Copilot threads are outdated — they refer to the old /approve with claude opus-4.8 |
|
[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 |
|
/cherry-pick release-1.21 with claude fable-5 |
|
@wallrj: new pull request created: #9235 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. |
|
@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. |
Pull Request Motivation
Fixes #9064
The issue reports a nil-pointer panic in
certificateRequestIdentity.Validatewhen anAdmissionReviewis POSTed directly to the webhook/validateendpoint. The reporter's payload setsresource(Certificate CREATE) but omitsrequestResource. After JSON decode,RequestResourceis 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
AdmissionReviewto the webhook, so these fields are set without human intervention. Posting anAdmissionReviewdirectly 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
RequestResourcein identityValidate. That addresses the symptom in a narrower scope rather than root cause.This PR uses always-present
Resource/SubResourcefor 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
RequestResourceleft two related gaps. A zero-valuedResource(e.g. from a non-conformant aggregated/virtual apiserver) would silently skip every plugin's gate and beAllowedinstead of denied. Andapproval/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 byresource. The 2nd commit closes both:Resourceis now checked for the zero value before each plugin's existing gate, andapproval/resourcevalidationuse checked type casts instead of bare assertions, matching identity's existing pattern.Kind
/kind bug
Release Note