Skip to content

core,auth: expose an authorized extension's principal to route function/middleware context - #653

Merged
jimhoyd merged 1 commit into
mainfrom
claude/618-extension-principal-context
Sep 24, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/618-extension-principal-context

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

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 hand
data forward into the route's own trusted function/middleware context,
so a bearer-protected route could not learn which key authenticated it.

  • Core (packages/core/src/extensions.ts, runtime.ts): adds a
    reserved x-urlcode-context-* header namespace. The runtime strips it
    from 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.headers clone; guestHeaders/
    FunctionContext are now built from that clone (instead of the original
    request headers), so the written value carries forward into the route's
    own function/middleware's Request object. Declared
    credentialHeaders (cookie, authorization, ...) are still stripped
    from that same guest-facing projection exactly as before — this is not an
    exemption from "session and bearer credentials never cross into
    application guests."
  • Core (packages/core/src/proxy.ts): validateProxy refuses any
    proxy.requestHeaders/responseHeaders entry naming a reserved-namespace
    header, 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: the bearer gate now writes the verified key's
    id/name/scopes (base64-encoded JSON, never the raw key) into
    x-urlcode-context-auth-principal on success. A route reads it directly
    off its Request.
  • Docs: adds RIM-EXT-CONTEXT-001 to docs/RUNTIME-IMPLEMENTATION.md,
    documents the channel and its two boundaries (credential-stripping,
    no-proxy-forwarding) 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 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 credentialHeaders already establishes in the opposite direction,
needs no FunctionContext shape change, and needs no sandboxed-dispatch
serialization change. Core never reads or interprets a value written into
the namespace — packages/auth's authorize() is the only place that
writes one, keeping core's boundary intact (scripts/check-core-boundaries.ts
still 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 project
author 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 typecheck
  • npm run lint (eslint)
  • npm run verify — full pipeline: lint, typecheck, build, core test
    suite (871 tests), and every workspace package's own verify
    (ui/auth/admin/store/forms/mcp) plus workspace integration tests, all
    green
  • New focused fixture test/extension-context-header.test.ts: a 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 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 a
    proxied upstream even by trying
  • Extended packages/auth/test/auth-handler.test.ts: the principal
    header 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)

  • The channel is generic and unenforced beyond "not a literal
    cookie/authorization/declared-credential name": nothing stops a
    future 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.
  • There's no size/count cap on what an extension writes into this namespace
    beyond ordinary HTTP header limits (extensions are trusted operator code,
    same trust tier as a function/middleware route, so I didn't add a
    bespoke cap — but it's worth a second look if a future extension writes
    something large here).
  • I chose base64-encoded JSON for packages/auth's principal value
    specifically to dodge header-value character-set issues from an
    operator-supplied API key name (JSON.stringify alone would still leave
    non-Latin-1 characters that native Headers.set rejects). This is a
    packages/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

…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]>
@jimhoyd
jimhoyd merged commit 84b2b22 into main Sep 24, 2026
23 checks passed
@jimhoyd
jimhoyd deleted the claude/618-extension-principal-context branch September 24, 2026 13:22
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.

Expose an authorized extension's principal to the route's function/middleware context

1 participant