CEL API path validation and mpol namespace confinement
fix(security): CEL API path validation and mpol namespace confinement (#17985)
CEL API path validation and mpol namespace confinement95f02d-fix-cel-api-path-validation-and-mpol-cve-2026-122311fixcel api path validation mpol namespace
Committed by Rohanraj123
commit message
fix(security): CEL API path validation and mpol namespace confinement (#17985)
* fix(security): validate CEL resource.get/post/list path segments
CEL policies read/write arbitrary Kubernetes resources through
resource.Get/List/Post, implemented by contextProvider in
pkg/cel/libs/context.go. The resource, subresource, object name, and
apiVersion group/version arguments were forwarded unvalidated into
client-go's dynamic client, which builds the request path with
(*rest.Request).AbsPath - and AbsPath joins segments with path.Join,
which resolves "." and ".." segments. A resource or subresource
argument (or an object name, or the apiVersion) that was exactly ".."
could therefore redirect the actual request to a different resource
type, a different namespace, or even a different API group/version
than the one any namespace-confinement check ever saw - independent
of whatever namespace a namespaced CEL policy is restricted to.
Add validators in pkg/cel/libs/resource_path.go and apply them in
GetResource, PostResource, and ListResources: every value that becomes
a path segment (API group, version, resource, subresource, name,
namespace) must be non-empty, not "." or "..", and free of path
separators before it ever reaches the dynamic client.
Addresses GHSA-4pqm-wqq6-h9q2.
(cherry picked from commit 9732a060f9d1460d42865a21b72fb57bf16a15d0)
Signed-off-by: Rohanraj123 <[email protected]>
* fix(security): share one API path segment validator and cover subresources
The CEL resource validator was local to pkg/cel/libs and hand-rolled, so it
missed "%" and no other dynamic-client caller could reuse it. Move it to
dclient.ValidatePathSegment, built on apimachinery's IsValidPathSegmentName,
and validate the subresources parameter of GetResource, UpdateResource and
ApplyResource.
Replace the fake-client tests. A fake dynamic client never builds a URL, so
every traversal assertion passed against the vulnerable code. The tests now
drive a real dynamic client at an httptest server, assert that no request is
sent for a traversal input, and assert the exact path for legitimate calls.
Also covers the namespace-pinned CEL environment a namespaced policy gets.
(cherry picked from commit 9c715b58bece09b47e46097ea200411c86aefeda)
Signed-off-by: Rohanraj123 <[email protected]>
* fix(security): reject API path traversal in policy-supplied resource strings
Two more producers handed policy-author strings to the dynamic client as raw
path segments. The mutating policy target route splits
targetMatchConstraints.resources[] on "/" and passes the remainder to
UpdateResource as a subresource. GetGVRs builds a GroupVersionResource straight
from the same kind of string with no mapper lookup, and the deleting controller
lists it, so a ".." entry redirected that list under the controller's own
ServiceAccount.
Both now reject unsafe segments. GetGVRs skips the entry and logs it rather
than reinterpreting it, so a rule that was previously inert stays inert instead
of becoming a live deleting rule. An empty subresource keeps its old meaning of
"no subresource".
(cherry picked from commit 6c5d4d20354aada368d8ac50446c9c998e19796e)
Signed-off-by: Rohanraj123 <[email protected]>
* fix(security): confine NamespacedMutatingPolicy mutate-existing targets to their own namespace
pkg/background/mpol/processor.go resolves mutate-existing targets for CEL
MutatingPolicy/NamespacedMutatingPolicy via two independent paths, neither of
which reliably confined a namespaced policy's targets to its own namespace and
non-cluster-scoped kinds. Both kinds share the same spec type and the API gives
no structural guarantee, so confinement has to be enforced here.
- Resource-rule path: for a namespaced policy, skip any GVK whose REST mapping
isn't namespace-scoped before it is added to the result set, using the
meta.RESTMapper already available to the processor.
- CEL target-expression path: the expression's output was trusted verbatim, so a
namespaced policy could return a cross-namespace or cluster-scoped target and
have it patched. resolveTargetRoute now also reports the REST scope, and any
target that is cluster-scoped or outside the policy's namespace is rejected
before the re-fetch and write.
- Write path: UpdateResource used the mutated resource's own
namespace/kind/apiVersion/name, so a mutation that rewrote those identifying
fields could redirect the write past the confinement above. Reject any patch
whose identity does not match the object it is meant to be patching.
Regression tests cover all three.
Rebased onto release-1.19-n4k after #1019 reshaped collectGVK and
resolveTargetRoute; the two original commits are combined because that change
rewrote the same hunks.
(cherry picked from commit 635c28b1648330be5017ae1ecb9539d2aca47a8f)
Signed-off-by: Rohanraj123 <[email protected]>
* fix(security): decide mpol target scope by REST scope, not kind name
The resource-rules target arm asked two different questions about the same
thing. collectGVK filtered cluster-scoped kinds out by REST scope, while
Process decided whether a list may span the cluster from the kind's *name*
(`r.gvk.Kind == "Namespace"`). A namespace-scoped CRD whose Kind is
"Namespace" in a non-core group satisfies the filter and still trips the
name check, so the confinement was only ever one unrelated CRD away from
reopening. Worse, the name check assigned to `ns`, the loop's own range
variable, so a single reset applied to every later kind in the same rule
set: a policy whose rules named such a CRD alongside ConfigMaps listed and
patched ConfigMaps in every namespace.
Scope is the only oracle that answers this correctly, so resolve it once in
collectGVK, carry it on gvkItem, and branch on it in Process via a local
listNS rather than the range variable. This is the shape the deleting
controller already uses.
Also:
- Fail closed when a kind has no REST mapping: an unknown scope must never
be treated as safe to list cluster-wide.
- List a cluster-scoped kind once, not once per namespace a cluster policy's
namespaceSelector matched. Every namespace key shares one gvkItem set, so
it was resolving and writing the same objects N times.
- Report a rule set that resolves no kind at all, and a failed lookup of the
policy's own namespace. Individual KindFor misses stay unreported on
purpose: resource rules are a group x version x resource cross-product, so
most misses are legitimate non-combinations.
- Add one confinement checkpoint covering every resolution arm, so the
invariant survives a future arm that forgets to confine as it resolves.
Tests drive the resource-rules arm end to end for a namespaced policy, which
nothing covered before, and assert on the namespace of every list and every
write. The cluster-scoped MutatingPolicy regression is pinned separately,
since both kinds share one spec type.
(cherry picked from commit da79245e0f6026415e386bb73b4adaa629f184f2)
Signed-off-by: Rohanraj123 <[email protected]>
* test(mpol): port namespace-confinement tests onto main fixtures
The cherry-picked mpol commits depend on test helpers (fakeNSClient,
fakeStatusControl.failedMsg, mockUpdateTrackingClient, 4-value
resolveTargetRoute) that conflicted with main's own mpol tests.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Rohanraj123 <[email protected]>
* fix(security): address review feedback on API path validation
- admissionpolicy: only the core API group may be empty in GetGVRs; reject an
empty version or resource
- admissionpolicy: add AGENTS.md documenting the GVR path-segment invariants
- mpol test: assert the cluster-scoped kind error from collectGVK
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Rohanraj123 <[email protected]>
* docs: link pkg/admissionpolicy AGENTS.md from root
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Rohanraj123 <[email protected]>
---------
Signed-off-by: Rohanraj123 <[email protected]>
Co-authored-by: Raunak Madan <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>Editor assessment
This commit patches CVE-2026-122311. The body is exceptionally detailed: it explains that resource, subresource, object name and apiVersion were forwarded unvalidated into client-go, where AbsPath joins segments with path.Join and therefore resolves dot-dot, redirecting the request past every namespace-confinement check. It then covers the shared validator, the real-client test rewrite that exposed why fake clients hid the bug, the mpol scope decisions, and the fail-closed REST-mapping rule. Strong content, though the squash log and PR reference add noise.