Skip to content

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

Closed

Description

@jimhoyd

Problem or use case

Follow-up from #571 (bearer/API-key authentication, packages/auth). The triage
decision for #571 says the auth extension owns "exposing the principal/scopes
to the handler context" for a bearer-protected route. Implementing #571
(PR TBD) found that no such channel exists today, and closing it cleanly is
bigger than that PR should carry.

Today ExtensionInstance.authorize(requirement, request) and middleware(config, request, next) (packages/core/src/extensions.ts) only ever gate a request —
return a HandlerResult to deny, or undefined/call next() to allow. Neither
has any way to hand data forward into the FunctionContext a route's own
trusted function/middleware chain receives (packages/core/src/runtime.ts,
around where guestHeaders/context are built from the original request
headers, not from extensionRequest.headers). Extension-declared
credentialHeaders (e.g. cookie, authorization, always both per
packages/core/src/extensions.ts's "Session and bearer credentials never cross
into application guests") are stripped from what the guest sees, by design —
so even the raw bearer token itself is invisible to a protected route's own
function code, let alone a derived principal.

Concretely: a project route declares auth: {bearer: {scopes: [...]}}. The
gate correctly enforces the scope and returns 401/403 as needed. But the
route's own function/middleware currently has no way to learn which API
key (id/name/scopes) authenticated the request — it can only know a request
was authorized, not by whom.

Proposed behavior and alternatives

One workable shape traced during #571's implementation: reserve a header
namespace (e.g. x-urlcode-context-*) that the runtime always strips from
inbound request headers before building extensionRequest (so a client can
never spoof it), and build guestHeaders/context from extensionRequest.headers
(already a per-request clone) instead of the original headers — letting an
extension's authorize()/middleware() write a value into that namespace
(e.g. request.headers.set('x-urlcode-context-auth-principal', JSON.stringify(...)))
that a route's trusted function can then read directly off its Request
object. This is generic core infrastructure (not auth-specific), reuses the
existing header-based channel credentialHeaders already establishes in the
opposite direction, and needs no change to FunctionContext's shape or the
sandboxed dispatch path's serialization — but it is a new core semantic seam
(docs/RUNTIME-IMPLEMENTATION.md card + focused fixture per AGENTS.md) touching
a boundary SECURITY.md documents as deliberate, so it deserves its own
reviewed design rather than landing inside an unrelated feature PR.

Alternatives: a dedicated context.principal-style field threaded through
FunctionContext end to end (larger, more invasive change, also needs
sandbox-path parity); or leaving this to project code that calls the
extension's own service object directly (works today for operator-side script,
not for a project's own trusted function/middleware, since it never
receives the raw credential either).

Evidence

packages/auth's bearer/API-key capability (#571) ships without this: the gate
enforces auth/scopes correctly, but a protected route cannot read who
authenticated. Documented as a known limitation in packages/auth/README.md and
docs/EXTENSIONS.md pending this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions