Skip to content

A policy can finally say "requires the write scope" - #87

Merged
blakebauman merged 2 commits into
mainfrom
feat/scope-aware-policy
Aug 25, 2026
Merged

blakebauman merged 2 commits into
mainfrom
feat/scope-aware-policy

Conversation

@blakebauman

Copy link
Copy Markdown
Member

Phase 2 of the audit follow-up. Independent of #84.

The gap

fold could not express the most ordinary enterprise authorization predicate there is. Scopes arrive in the scope claim as a space-delimited string (RFC 6749 §3.3), and claimsMatch compares whole values — so claims: {"scope": "write"} does not match a token carrying read write admin. There was no way to write it that worked.

subjects.scopes reads scope, or scp when that yields nothing, as either a space-delimited string or an array — so a rule is written once against the concept rather than against an issuer's spelling. The two are never merged: an issuer sending both is sending the same set twice, and merging would silently union two claims that disagreed.

Because PolicySubjects is shared, tenants selectors get the same field from the same definition.

The semantic choice worth reviewing

Scopes match conjunctively — every scope named must be held — unlike subs and groups, which are alternatives.

The reasoning: subs and groups answer who is this, and one identity is enough. A scope answers what were they granted, where a list reads as a set of requirements rather than a choice. So scopes gate like claims do — combine with an identity, or stand alone as the whole subject.

Denials that say what would fix them

Where scopes were the only obstacle, -32042 names them in the message and in data.missingScopes, and the audit event carries them. An agent re-authorizes for exactly what it lacks instead of retrying blind.

Two disclosure rules bound this, and they matter more than the convenience:

  • Only scopes the caller lacks appear. Re-authorizing accumulates permissions rather than replacing them, so a caller holding read who needs read write is told to obtain write alone — asking for the pair risks an IdP issuing a token that drops what they had.
  • A rule contributes only when scopes were the sole obstacle and it targets that exact invocation. If a non-scope condition rejected them first, or the rule was about a different tool, the denial names nothing. Disclosing a requirement guarding something the caller cannot reach would leak that it exists. Same discipline args already follows — it reports which path failed, never the value the rule wanted.

Both are mutation-tested: removing the targeting guard, or returning the full requirement instead of the shortfall, each fails a specific test.

Cost to documents that don't use it

Nothing. The accounting is gated on a hasScopes flag computed at construction, and the shortfall is only computed under argEnforce — exclusive to DecideCall — so list filtering never touches it. Latency gate unmoved at 0.179 ms.

A correction on the tenant index

My first version of this change claimed the index predicates were a live admission bug — that {groups:["eng"], scopes:["admin"]} filed under byGroup would match on the group alone. That was wrong, and the design record now says so. considerTenants re-matches every candidate an index produces, so the index only narrows and the matcher stays the authority.

The exclusions are kept as consistency and defence in depth — dropping that re-match as redundant is exactly the optimization someone reaches for one day. The predicate that is load-bearing today is the exclusion of subs from indexableGroups: subs and groups are alternatives, so a principal can satisfy such a selector by subject while holding no group at all, and a group lookup would never surface it. Conjunctive dimensions can't produce that failure; disjunctive ones can.

Single-scope selectors get their own index. Multi-scope ones cannot — a conjunction is not answerable by a held-scope lookup — so they scan, deliberately.

No behavioural test can catch a mis-filing, precisely because behaviour is correct either way. TestScopeBearingSelectorsAreNotIndexedByAnotherDimension asserts the partitioning itself and is the only thing that fails when a predicate loses a condition; its comment says so, so nobody deletes it as redundant. The cross-product test was also extended with scopes — it passes either way, which is the finding rather than a gap.

Verification

  • go test -race ./... — all green
  • conformance 40/40 — which also exercises that the new data field on -32042 is tolerated on the wire
  • bench 0.179 ms added p50 (gate 5 ms); indexed tenant resolution still flat at 36–91 ns across 10–10,000 declarations, zero allocations
  • lint 0 issues, gofmt / vet / tidy clean

Integration tests use real SDK peers throughout; the visibility test connects a second client directly to the upstream to prove the tool's absence is the gateway's doing.

Known gaps

MissingScopes is asserted on tools/call but not prompts/get or resources/read (same code path in authorize), and there's no benchmark asserting that scope-free documents pay nothing — it's argued structurally rather than measured.

🤖 Generated with Claude Code

blakebauman and others added 2 commits August 25, 2026 14:51
fold could not express the most ordinary enterprise authorization
predicate there is. Scopes arrive in the `scope` claim as a
space-delimited string (RFC 6749), and the claims matcher compares whole
values — so `claims: {"scope": "write"}` does not match a token carrying
`read write admin`, and no amount of writing it differently helped.

`subjects.scopes` reads `scope`, or `scp` when that yields nothing, as
either a space-delimited string or an array, so a rule is written once
against the concept rather than against an issuer's spelling. The two
are never merged: an issuer sending both is sending the same set twice,
and merging would silently union two claims that disagreed.

Scopes match conjunctively — every one named must be held. That is
deliberately unlike subs and groups, which are alternatives: those
answer "who is this", where one identity is enough, while a scope
answers "what were they granted", where a list is a set of
requirements rather than a choice. So scopes gate like claims do,
combining with an identity or standing alone. Because PolicySubjects is
shared, tenants get the same selector from the same definition.

A denial now says what would fix it. Where scopes were the only obstacle
the -32042 error names them, in the message and in data.missingScopes,
and the audit event carries them, so an agent re-authorizes for what it
lacks instead of retrying blind. Two disclosure rules bound that, and
they matter more than the convenience. Only scopes the caller lacks
appear, because re-authorizing accumulates permissions rather than
replacing them and asking for the pair risks losing what they had. And a
rule contributes only when scopes were the sole obstacle and it targets
that exact invocation — naming a requirement that guards something the
caller could not reach anyway would disclose that it exists. It is the
discipline args already follows, which reports which path failed and
never the value the rule wanted.

Documents naming no scopes evaluate exactly as before: the accounting is
gated on a flag computed at construction, and the list path never
computes a shortfall at all, since it is confined to the call mode.

On the tenant index, one correction worth recording. Scope-bearing
selectors are excluded from the claim and group indexes, but that is
consistency and defence in depth rather than a bug fix: considerTenants
re-matches every candidate an index produces, so a mis-filed selector
narrows oddly and never admits wrongly. It is kept because dropping that
re-match as redundant is exactly the optimization someone will reach for
one day. The predicate that is load-bearing today is the exclusion of
subs from indexableGroups, because subs and groups are alternatives and
a principal can satisfy such a selector while holding no group at all.
Single-scope selectors get their own index; multi-scope ones cannot,
since a conjunction is not answerable by a held-scope lookup.

No behavioural test can catch a mis-filing, precisely because behaviour
is correct either way, so the partitioning is asserted directly.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman merged commit 019c2a0 into main Aug 25, 2026
8 checks passed
@blakebauman
blakebauman deleted the feat/scope-aware-policy branch August 25, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant