Skip to content

fix: Reject invalid CIDRs in Certificate nameConstraints at admission time - #9184

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
lunarwhite:fix-cidr-val
Aug 21, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:masterfrom
lunarwhite:fix-cidr-val

Conversation

@lunarwhite

Copy link
Copy Markdown
Member

Pull Request Motivation

A Certificate with an invalid CIDR such as 10.0.0.0 (no prefix length) in spec.nameConstraints.permitted.ipRanges or spec.nameConstraints.excluded.ipRanges was accepted by the validating webhook, and then failed on every CSR-generation attempt with invalid CIDR address: 10.0.0.0. Because the failure is derived entirely from the spec, retrying could never succeed, the Certificate was permanently un-issuable.

Fixes #9181

E2E Verification

Rejection with exact message (permitted.ipRanges)
$ kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: s1-permitted-bad
  namespace: nc-cidr-after
spec:
  isCA: true
  commonName: s1-permitted-bad
  secretName: s1-permitted-bad-tls
  issuerRef:
    name: selfsigned
    kind: Issuer
    group: cert-manager.io
  nameConstraints:
    permitted:
      ipRanges:
        - "10.0.0.0"
EOF
Error from server (Forbidden): error when creating "s1-permitted-bad.yaml": admission webhook "webhook.cert-manager.io" denied the request: spec.nameConstraints.permitted.ipRanges[0]: Invalid value: "10.0.0.0": invalid CIDR address

$ kubectl get certificate -n nc-cidr-after
No resources found in nc-cidr-after namespace.
Rejection via excluded.ipRanges
$ kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: s2-excluded-bad
  namespace: nc-cidr-after
spec:
  isCA: true
  commonName: s2-excluded-bad
  secretName: s2-excluded-bad-tls
  issuerRef:
    name: selfsigned
    kind: Issuer
    group: cert-manager.io
  nameConstraints:
    excluded:
      ipRanges:
        - "10.0.0.0"
EOF
Error from server (Forbidden): error when creating "s2-excluded-bad.yaml": admission webhook "webhook.cert-manager.io" denied the request: spec.nameConstraints.excluded.ipRanges[0]: Invalid value: "10.0.0.0": invalid CIDR address
A valid Certificate cannot be edited into an invalid one

This is the desirable direction of the UPDATE coverage. It prevents a Ready Certificate from being broken.

$ kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: s7-update-block
  namespace: nc-cidr-after
spec:
  isCA: true
  commonName: s7-update-block
  secretName: s7-update-block-tls
  privateKey:
    algorithm: ECDSA
    size: 256
  issuerRef:
    name: selfsigned
    kind: Issuer
    group: cert-manager.io
  nameConstraints:
    permitted:
      ipRanges:
        - "10.0.0.0/8"
EOF
certificate.cert-manager.io/s7-update-block created

$ kubectl wait --for=condition=Ready --timeout=120s certificate/s7-update-block -n nc-cidr-after
certificate.cert-manager.io/s7-update-block condition met

$ kubectl patch certificate s7-update-block -n nc-cidr-after --type=merge -p '{"spec":{"nameConstraints":{"permitted":{"ipRanges":["10.0.0.0"]}}}}'
Error from server (Forbidden): admission webhook "webhook.cert-manager.io" denied the request: spec.nameConstraints.permitted.ipRanges[0]: Invalid value: "10.0.0.0": invalid CIDR address

Kind

/kind bug

Release Note

The webhook now rejects Certificates whose `spec.nameConstraints.permitted.ipRanges` or `spec.nameConstraints.excluded.ipRanges` contain an invalid CIDR, rather than accepting a spec that can never be issued.

@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 Aug 21, 2026
@erikgb
erikgb requested a lite review from Copilot August 21, 2026 12:22

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 PR fixes a bug where Certificate resources with invalid CIDRs in spec.nameConstraints.{permitted,excluded}.ipRanges were accepted by the validating webhook but could never be issued, by moving CIDR parsing/validation into admission-time validation.

Changes:

  • Refactors nameConstraints validation into a dedicated helper and runs it during ValidateCertificateSpec.
  • Adds admission-time validation for each entry in permitted.ipRanges and excluded.ipRanges using net.ParseCIDR.
  • Extends unit tests to cover valid and invalid CIDR scenarios (and confirms feature-gate short-circuiting).

Reviewed changes

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

File Description
internal/apis/certmanager/validation/certificate.go Adds admission-time CIDR parsing for nameConstraints ipRanges and refactors the validation into helper functions.
internal/apis/certmanager/validation/certificate_test.go Adds test cases for valid/invalid CIDR entries in nameConstraints ipRanges and preserves feature-gate behavior.

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

@erikgb erikgb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

CI is passing and this looks like a good improvement! Thanks @lunarwhite! 🚀

/lgtm
/approve

@cert-manager-prow cert-manager-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Aug 21, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: erikgb

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 21, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit 9a5c1d2 into cert-manager:master Aug 21, 2026
6 checks passed
@lunarwhite
lunarwhite deleted the fix-cidr-val branch August 21, 2026 13:45
@wallrj wallrj mentioned this pull request Sep 1, 2026
39 of 41 tasks
@wallrj wallrj mentioned this pull request Nov 4, 2026
2 of 40 tasks
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/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid CIDR in Certificate nameConstraints.ipRanges is accepted at admission time

3 participants