← Back to Pull requests

Align discovery path matching with system:discovery and rebuild the result cache key #24

Closed
unknown closed this pull request without merging on Sep 24, 2026 04:52 UTC
View on GitHub
unknown unknown commented on Aug 11, 2026 01:00 UTC
Author

Summary

Aligns how Guard matches non-resource (discovery) paths with the upstream Kubernetes rule it mirrors, and rebuilds the CheckAccess result cache key so that two requests that are not identical cannot produce the same key.

Both changes now follow the upstream Kubernetes construct they mirror, per review feedback.

Changes

1. Match discovery paths against the upstream system:discovery rule

AllowNonResPathDiscoveryAccess matched with a short list of strings.HasPrefix roots that did not correspond to any upstream rule, so the set of exempted paths was wider in some places and narrower in others than the rule it was meant to represent.

Matching now reproduces the non-resource URLs of the upstream system:discovery ClusterRole (plugin/pkg/auth/authorizer/rbac/bootstrappolicy/policy.go, bound to system:authenticated) together with how rbacv1.NonResourceURLMatches evaluates them: an entry ending in is a prefix with the trimmed, every other entry is an exact string match.

  • exact: /api, /apis, /healthz, /livez, /openapi, /readyz, /version, /version/

  • prefix: /api/, /apis/, /openapi/
  • Behaviour changes against the previous revision: /healthz/<anything> and /version/<anything> are no longer exempt, and /livez and /readyz now are, matching upstream. A path that is not discovery is not denied; it falls through to the regular Azure RBAC check.

    Guard is deliberately stricter than upstream on one point: a path containing a .. segment is never treated as discovery. nonResourceAttributes.path on a SelfSubjectAccessReview is set by the caller rather than derived by the API server's router, so unlike a routed path it is not already normalized.

    2. Build the result cache key with a length-prefixed hash

    getResultCacheKey built the key by joining fields with path.Join. That leaves the boundaries between fields ambiguous, and applies path.Clean to the result, so the key was a function of the normalized join rather than of the fields themselves.

    It now follows buildKey from k8s.io/apiserver/pkg/endpoints/filters/impersonation/cache.go: a fixed-width request-shape byte, then every field under a uint32 big-endian length prefix, hashed with SHA-256, with the un-hashed user name appended after the digest. No field value can shift a field boundary, so two requests share a key only when every field is byte equal, and because the digest is fixed width, two keys are equal only if their users are equal. Any digest collision is therefore confined to a single user's own keys, where it cannot yield a permission that user does not already hold.

    The shape byte distinguishes the three specs Guard can receive - resource attributes, non-resource attributes, or neither - so keys built from different attribute sets occupy disjoint spaces.

    Two deviations from upstream: no unsafe (its toString saves one allocation in an apiserver hot path; this runs once per SubjectAccessReview), and a three-state shape byte instead of addBool(IsResourceRequest), because a spec can carry neither attribute set.

    The field set is unchanged, so cache hit rates are unaffected: the key still uses the derived action, so get, list and watch continue to share one entry.

    Tests

  • Test_AllowNonResPathDiscoveryAccess - all 11 upstream entries allowed; subpaths of the exact-match entries (/healthz/etcd, /version/foo, /livez/poststarthook/..., /readyz/shutdown) rejected; loose-prefix look-alikes (/apiz, /healthzz, /livezz, /readyzz, /versionx) rejected; traversal, verb and flag guards covered.

  • Test_getResultCacheKey_distinctRequestsGetDistinctKeys - requests that must not share a decision do not share a key, including placeholder and field-boundary cases.

  • Test_getResultCacheKey_isDeterministic, Test_getResultCacheKey_isUserNamespaced - key depends only on the request, and is a 64 character digest suffixed with the user name.

  • Test_getResultCacheKey_readVerbsShareCacheKey - pins the preserved get/list/watch sharing, so keying on the raw verb fails the build.

  • Test_getResultCacheKey_noResourceNonResourceCollision - keys built from resource and non-resource attributes stay in disjoint spaces.
  • Literal-key assertions were replaced with invariant tests, since asserting a hardcoded digest would only restate the implementation. The distinctness table was checked against both a prefix-less encoding and the previous path.Join encoding to confirm it discriminates.

    Validation

  • go build ./... - OK

  • go vet ./... - OK

  • gofmt -l on all non-vendor Go files - clean

  • golangci-lint run in ghcr.io/appscode/golang-dev:1.25 via make lint - 0 issues

  • go test ./authz/... ./auth/providers/azure/graph/... - all pass
  • Diff: 598 lines (+448 / -150) across 7 files Reviewer: stlaz Review duration: 63592m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 598 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.383
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #24"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50% (MEDIUM) · Higher = larger blast radius
    50%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.6% · Safe to merge
    21.6%
    Repository