Skip to content

A client had to be refused before it could learn what to ask for - #89

Merged
blakebauman merged 1 commit into
mainfrom
feat/advertise-required-scopes
Aug 26, 2026
Merged

blakebauman merged 1 commit into
mainfrom
feat/advertise-required-scopes

Conversation

@blakebauman

Copy link
Copy Markdown
Member

Found by standing a gateway up with auth.mode: "required" and walking the chain a real client walks — 401 → WWW-Authenticate → RFC 9728 document → authorization_servers — rather than reasoning about it.

fold's half of that chain is correct. What is missing is one field.

The gap

The protected-resource document named the authorization server but not the scopes. So a client's only route to a scope requirement was to be denied by it: authorize with whatever scopes it guessed, connect, call a tool, and read the answer out of the -32042 error.

#87 made that denial carry a remedy, which helps the second attempt. This is about the first.

scopes_supported is now published — every scope policy.rules[].subjects.scopes names, deduplicated and sorted so the document is stable between fetches. RFC 9728 §2 recommends the field for exactly this. Verified live against a running gateway:

"scopes_supported": ["docs:read", "docs:write"]

It is a hint rather than a contract: holding all of them entitles a caller to nothing on its own, since scopes are one gate among several and a rule can also require an identity, an issuer, or a claim.

Tenant scopes are excluded — and the first version of this published them

Worth reviewing, because it was nearly a leak.

The endpoint is unauthenticated, and a tenant scope is usually a customer's name. A deployment selecting tenants that way — fold's own flagship config has tenants acme and globex — would have served its customer roster from a well-known URL. That is a different class of secret from a capability name like docs:write, and one fold does not otherwise disclose: a denial already refuses to name a requirement to a caller who failed on other grounds (TestDenialForAnotherReasonDisclosesNoScope).

The second reason is independent and equally sufficient: a tenant scope is an identity assertion, not a permission. An authorization server asked for tenant:acme will not mint it. Publishing it would have leaked something real in exchange for advice no client could act on.

The tests pin the exclusion at the boundary that would regress — a scope named only by a tenant is absent, while the same scope named by a rule as well still appears.

Reload

The document was marshalled once at startup. Policy hot-reloads, so a construction-time copy would have gone on advertising the rules fold started with. It is built per request now — one small marshal, no I/O, no crypto. Confirmed by SIGHUP against a live gateway before writing the test, and the test mutation-checks it: restoring the old behaviour fails that test and only that test.

Two pre-existing bugs fixed while in here

Both reachable only with EMA enabled, where the document is hand-written to carry an extension key the SDK's type cannot express. The body was EMA's to change; the contract around it was not:

GET Access-Control-Allow-Origin OPTIONS POST
EMA off 200 * 204 405
EMA on (before) 200 (none) 200 + body 200 + body

So with EMA on, a browser-based client's cross-origin discovery fetch failed the CORS check outright. Now matches the SDK handler.

Verification

  • go test -race ./... green; lint 0 issues; gofmt / vet / tidy clean
  • conformance 40/40; bench 0.184 ms added p50
  • Unit table over requiredScopes (nil snapshot, nil cfg, unsorted input across rules and tenants, dedup, tenant-only exclusion), asserting nil rather than []string{} for the empty cases since that is what makes omitempty drop the key
  • Wire tests assert the document a client actually fetches, including the EMA map[string]any round-trip
  • Chain-integrity test parses resource_metadata out of the WWW-Authenticate header and fetches it as given, so the pointer is proven followable rather than assumed

Context on the adoption question this came from

Also confirmed while probing, and it corrects something I said earlier: oauth.work — the IdP behind fold's own enterprise deployment — publishes registration_endpoint, so it does support dynamic client registration. The DCR gap is real only for IdPs that lack it (Okta, Entra typically), not for fold's own stack. docs/configuration.md now states the discovery chain, what completes it, and where it does not, which is the failure mode where an operator configures fold correctly and the problem is upstream of it.

🤖 Generated with Claude Code

fold's protected-resource metadata named the authorization server but
not the scopes, so a client's only route to a scope requirement was to
be denied by it: authorize with whatever it guessed, connect, call a
tool, and read the answer out of the -32042 error. The denial carries a
remedy now, which helps the second attempt; this is about the first.

scopes_supported is published in the RFC 9728 document — every scope the
policy rules name, deduplicated and sorted so the document is stable
between fetches. RFC 9728 §2 recommends the field for exactly this. It
is a hint rather than a contract: holding all of them entitles a caller
to nothing on its own, since scopes are one gate among several and a
rule can also require an identity, an issuer, or a claim.

Tenant selector scopes are deliberately excluded, and the first version
of this published them. Two reasons, either sufficient. The endpoint is
unauthenticated and a tenant scope is usually a customer's name, so a
deployment selecting tenants that way would have served its customer
roster from a well-known URL — a different class of secret from a
capability name, and one fold does not otherwise disclose, since a
denial already refuses to name a requirement to a caller who failed on
other grounds. And a tenant scope is an identity assertion rather than a
permission, so an authorization server asked for one will not mint it:
publishing it would have leaked something real in exchange for advice no
client could act on.

The document is now built per request rather than marshalled once at
startup. Policy hot-reloads, and a construction-time copy would have
gone on advertising the rules fold started with. It costs one small
marshal on a path that does no I/O and no crypto.

Two things fixed while in here, both pre-existing and both reached only
with EMA enabled, where the document is hand-written to carry the
extension key the SDK's type cannot: it went out with no CORS headers,
so a browser client's cross-origin discovery fetch failed the check; it
answered a preflight with the document instead of 204; and it answered
POST. The branch now matches the contract the SDK handler sets around
the body, which is the part that was never EMA's to change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman force-pushed the feat/advertise-required-scopes branch from 044b143 to 0dd5699 Compare August 26, 2026 01:22
@blakebauman
blakebauman merged commit f596bdc into main Aug 26, 2026
8 checks passed
@blakebauman
blakebauman deleted the feat/advertise-required-scopes branch August 26, 2026 01:31
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