Repository navigation
core,auth: expose an authorized extension's principal to route function/middleware context - #653
Merged
Merged
Conversation
…function/middleware context Adds a reserved x-urlcode-context-* header namespace (core/extensions.ts: extensionContextHeaderPrefix, stripReservedContextHeaders) that an ExtensionInstance.authorize()/middleware() hook can write into to hand a derived, non-secret value forward into a route's own trusted function/middleware context. The runtime strips this namespace from every inbound request before any extension or guest code sees it, so a client can never inject or spoof a value; guestHeaders/FunctionContext are now built from ExtensionRequest.headers (the extension-writable clone) instead of the original request headers, so a written value carries forward. Declared credentialHeaders (cookie, authorization, ...) are still stripped from that same guest-facing projection exactly as before. The channel stops at the guest: validateProxy now refuses any proxy requestHeaders/responseHeaders entry naming a reserved-namespace header, the same way it already refuses a credential-shaped name, so a route can never opt this namespace into forwarding to an external upstream. packages/auth's bearer/API-key gate uses this to write the verified key's id/name/scopes (base64-encoded JSON, never the raw key) into x-urlcode-context-auth-principal on success, closing the known gap from #571 where a bearer-protected route had no way to learn who authenticated it. Adds RIM-EXT-CONTEXT-001 to docs/RUNTIME-IMPLEMENTATION.md, documents the channel and its credential-stripping and proxy-forwarding boundaries in SECURITY.md and docs/EXTENSIONS.md, and updates packages/auth's README/ SECURITY/IMPLEMENTATION-STATUS/llms.txt away from the "not yet exposed" language. Adds focused fixtures (test/extension-context-header.test.ts) covering: value written by authorize()/middleware() reaches the guest Request/context; a client-supplied value in the namespace is always stripped and never observed by the extension or guest; credential headers stay withheld even when this namespace is populated; and a proxy route cannot name a reserved-namespace header in requestHeaders/responseHeaders. Extends packages/auth/test/auth-handler.test.ts to assert the principal header is set only on a successful bearer authorization and never on any denied path. Closes #618 Co-Authored-By: Claude Sonnet 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements #618, a follow-up scoped out of #571 (bearer/API-key auth):
authorize()/middleware()could gate a request but had no way to handdata forward into the route's own trusted
function/middlewarecontext,so a bearer-protected route could not learn which key authenticated it.
packages/core/src/extensions.ts,runtime.ts): adds areserved
x-urlcode-context-*header namespace. The runtime strips itfrom every inbound request before any extension or guest code observes
it, so a client can never inject or spoof a value there. An
authorize()/middleware()hook can write into it on the mutable,per-request
ExtensionRequest.headersclone;guestHeaders/FunctionContextare now built from that clone (instead of the originalrequest headers), so the written value carries forward into the route's
own
function/middleware'sRequestobject. DeclaredcredentialHeaders(cookie,authorization, ...) are still strippedfrom that same guest-facing projection exactly as before — this is not an
exemption from "session and bearer credentials never cross into
application guests."
packages/core/src/proxy.ts):validateProxyrefuses anyproxy.requestHeaders/responseHeadersentry naming a reserved-namespaceheader, the same way it already refuses a credential-shaped name. A route
can never opt this namespace into forwarding to an external upstream —
the channel stops at the guest.
packages/auth: thebearergate now writes the verified key'sid/name/scopes (base64-encoded JSON, never the raw key) into
x-urlcode-context-auth-principalon success. A route reads it directlyoff its
Request.RIM-EXT-CONTEXT-001todocs/RUNTIME-IMPLEMENTATION.md,documents the channel and its two boundaries (credential-stripping,
no-proxy-forwarding) in
SECURITY.mdanddocs/EXTENSIONS.md, andupdates
packages/auth's README/SECURITY/IMPLEMENTATION-STATUS/llms.txtaway from the "not yet exposed" language left by [Request]: declarative bearer/API-key auth with scopes, expiry and revocation #571.
Design
Followed the shape already investigated in the issue: a reserved header
namespace is the smallest change that reuses the existing header-based
channel
credentialHeadersalready establishes in the opposite direction,needs no
FunctionContextshape change, and needs no sandboxed-dispatchserialization change. Core never reads or interprets a value written into
the namespace —
packages/auth'sauthorize()is the only place thatwrites one, keeping core's boundary intact (
scripts/check-core-boundaries.tsstill passes; core never imports
packages/auth).One addition beyond the issue's original text: I blocked the namespace from
proxy.requestHeaders/responseHeaders(validateProxy), so a projectauthor can never opt this data into being forwarded to an external upstream
just by naming it. The issue's proposed design didn't explicitly rule this
in or out; I chose the more conservative reading, since the issue frames
this purely as a channel into "the route's own trusted function/middleware,"
not into anything a route talks to externally.
Test plan
npm run typechecknpm run lint(eslint)npm run verify— full pipeline: lint, typecheck, build, core testsuite (871 tests), and every workspace package's own verify
(ui/auth/admin/store/forms/mcp) plus workspace integration tests, all
green
test/extension-context-header.test.ts: a valuewritten by
authorize()/middleware()reaches the guestRequest/context; a client-supplied value in the namespace is alwaysstripped and never observed by the extension or the guest; declared
credential headers stay withheld even when this namespace is
populated; a proxy route cannot name a reserved-namespace header in
requestHeaders/responseHeaders, and cannot smuggle one to aproxied upstream even by trying
packages/auth/test/auth-handler.test.ts: the principalheader is set (id/name/scopes, base64 JSON, never the raw key) only
on a successful bearer authorization, and is absent on every denied
path (missing/malformed/unknown/expired/revoked/insufficient-scope)
Residual concerns (flagging per request, even though tests pass)
cookie/authorization/declared-credential name": nothing stops afuture extension author from writing an actual secret into this
namespace by a different name. I documented this expectation clearly
(extensions.ts JSDoc, SECURITY.md) but it is a convention, not something
core can mechanically verify, since core cannot know if an arbitrary
string an extension writes is secret-shaped.
beyond ordinary HTTP header limits (extensions are trusted operator code,
same trust tier as a
function/middlewareroute, so I didn't add abespoke cap — but it's worth a second look if a future extension writes
something large here).
packages/auth's principal valuespecifically to dodge header-value character-set issues from an
operator-supplied API key
name(JSON.stringify alone would still leavenon-Latin-1 characters that native
Headers.setrejects). This is apackages/auth-level choice, not something core mandates or validates —worth confirming it's the pattern we want other extensions to follow.
Closes #618
🤖 Generated with Claude Code