Skip to content

feat(plugins): expose authenticated caller to route handlers as ctx.user - #1947

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:feat/plugin-route-caller
Aug 16, 2026
Merged

ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:feat/plugin-route-caller

Conversation

@swissky

@swissky swissky commented Jul 11, 2026

Copy link
Copy Markdown
Member

What does this PR do?

The plugin API catch-all resolves the authenticated user, uses it to enforce plugins:manage + scope + CSRF — and then drops it before invoking the plugin's handler. Plugins had no first-class way to know who is calling a private route, forcing bad workarounds (reading a user id from the request body is trivially spoofable).

This threads the validated caller through handlePluginApiRoute into the route context, exactly as proposed in #812:

  • Native format: ctx.user on RouteContext.
  • Standard (sandboxed) format: routeCtx.user — wired through the in-process adapter, the Cloudflare Worker Loader wrapper, and both workerd runners (SerializedRequest.user is structured-clone/JSON-safe).
  • The exposed shape is the existing read-only UserInfo ({ id, email, name, role, createdAt }) — the same shape ctx.users returns, with sensitive host-side fields (avatarUrl, data, …) stripped by toRouteCallerInfo().

Semantics (per the issue):

  • Private routes: ctx.user is the authenticated caller, validated by the existing requirePerm/requireScope checks one frame up.
  • Public routes: ctx.user is undefined — the catch-all only forwards the caller after private-route auth, so an ambient admin session browsing the site is never bound to a public route.
  • Machine tokens with no bound user: undefined.

Purely additive — no change for plugins that don't read ctx.user, no migration, no flag. Caller identity is intentionally not gated by the users:read capability (it's the caller's own identity, not a directory lookup).

Tests: route handler exposes/omits ctx.user (unit), catch-all forwards the caller only for private routes (unit, including the ambient-session case), sandbox adapter puts user on routeCtx and strips it from pluginCtx, toRouteCallerInfo mapping (Date→ISO, field stripping). Docs updated in the API-routes guide (new "The authenticated caller" section + route context reference) and the native-plugin guide.

Closes #812
Discussion: #1757

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Fable 5

Screenshots / test output

tests/unit/plugins + tests/unit/astro/plugin-api-route-auth.test.ts
 Test Files  30 passed (30)
      Tests  629 passed (629)

tests/integration/plugins + tests/unit/astro
 Test Files  26 passed (26)
      Tests  276 passed (276)

@emdash-cms/sandbox-workerd: 76 passed · @emdash-cms/cloudflare: 222 passed

@changeset-bot

changeset-bot Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 306504b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
emdash Minor
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Minor
@emdash-cms/plugin-mcp-smoke Major
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1947

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1947

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1947

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1947

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1947

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1947

emdash

npm i https://pkg.pr.new/emdash@1947

create-emdash

npm i https://pkg.pr.new/create-emdash@1947

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1947

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1947

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1947

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1947

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1947

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@1947

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1947

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1947

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1947

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1947

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1947

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1947

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1947

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1947

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1947

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1947

commit: 306504b

@github-actions

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

This PR has been inactive for 14 days. It will be closed automatically in 7 days if there is no further activity.

If you're still working on this, please push an update or leave a comment.

swissky and others added 2 commits August 16, 2026 12:08
The plugin API catch-all resolves and authorizes the caller, then drops
it before dispatch, leaving plugins no safe way to know who is calling
a private route. Thread the validated user through handlePluginApiRoute
into RouteContext (native format) and routeCtx.user (standard format,
in-process and worker sandboxes alike) as the read-only UserInfo shape.

Public routes and machine tokens with no bound user receive undefined;
the catch-all only forwards the caller after private-route auth, so an
ambient admin session is never bound to a public route.

Fixes emdash-cms#812
@ascorbic
ascorbic force-pushed the feat/plugin-route-caller branch from 872b71e to 6d56403 Compare August 16, 2026 11:13
@ascorbic ascorbic added bot:review Trigger an emdashbot code review on this PR and removed bot:review Trigger an emdashbot code review on this PR labels Aug 16, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right change for #812. Threading a sanitized UserInfo through both native and sandboxed route contexts, stripping host-only fields, and only binding the caller on private routes matches the issue's security requirements and EmDash's architecture. The tests cover the catch-all forwarding rules, the in-process adapter split, and the UserInfo mapping.

I traced the three dispatch paths: the plugin API catch-all, the in-process sandbox adapter, and both generated sandbox wrapper templates. The serialization is JSON/structured-clone safe, and public-route / unbound-token semantics are handled correctly.

Two issues keep this from a clean sign-off:

  1. MCP tool invocations don't forward the caller. handlePluginMcpTool calls handlePluginApiRoute without passing a caller, so plugin route handlers invoked via MCP tools see ctx.user === undefined even though those are private routes running with authenticated admin auth. This contradicts the PR's stated semantics that private routes always receive the caller.

  2. AGENTS.md comment violations. New comments reference issue #812 ("they're stale narrative" per AGENTS.md) and use justification language like "intentionally". This pattern appears in source files and the generated wrapper templates.

Both should be addressed before merge.


Findings

  • [needs fixing] packages/core/src/emdash-runtime.ts:3912

    This call invokes a private plugin route through handlePluginApiRoute but omits the authenticated caller. Because MCP tools are exposed only for private plugin routes and run with authenticated admin auth, the route handler will see ctx.user === undefined, which contradicts the PR's stated semantics that private routes receive the caller.

    The api/mcp.ts route has locals.user available; pass it through authInfo.extra, thread it into handlePluginMcpTool, and forward it here. actorId alone is not enough because toRouteCallerInfo needs email, name, role, and createdAt.

    		const result = await this.handlePluginApiRoute(
    			pluginId,
    			"POST",
    			route,
    			internalRequest,
    			caller,
    		);
    

    The plumbing also needs updates in packages/core/src/astro/routes/api/mcp.ts (include user in authInfo.extra), packages/core/src/mcp/server.ts (pass payload.user to handlePluginMcpTool), and this method's signature (add caller?: RouteCallerInput | null).

  • [needs fixing] packages/core/src/astro/routes/api/plugins/[pluginId]/[...path].ts:79

    AGENTS.md forbids referencing issues, PRs, or review threads in comments because they become stale narrative once merged. This PR adds #812 references in several source comments and in the generated sandbox wrapper templates.

    Examples found:

    • packages/core/src/emdash-runtime.ts:3748
    • packages/core/src/astro/types.ts:414
    • packages/core/src/plugins/sandbox/types.ts:145
    • packages/cloudflare/src/sandbox/wrapper.ts:262
    • packages/workerd/src/sandbox/wrapper.ts:465

    Please drop the issue references and keep only the behavioral explanation.

    	// Forward the caller only for private routes: it was validated by the
    	// requirePerm/requireScope checks above, so plugins can trust ctx.user.
    	// Public routes skip auth, so no caller is bound even when an ambient
    	// session happens to exist.
    

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Aug 16, 2026
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/needs-review No maintainer or bot review yet labels Aug 16, 2026
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Aug 16, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right change for #812. Threading a sanitized UserInfo through the native route context, the in-process sandbox adapter, both generated sandbox wrappers, and the MCP tool path matches the issue's security semantics and EmDash's architecture.

I re-read the full diff and the changed source files, then traced the four dispatch paths (plugin API catch-all, in-process sandbox adapter, Cloudflare Worker Loader wrapper, and workerd wrapper) and the MCP plumbing.

The two prior review findings are addressed:

  1. MCP tools now forward the authenticated caller. packages/core/src/astro/routes/api/mcp.ts puts locals.user into authInfo.extra, packages/core/src/mcp/server.ts threads payload.user through to handlePluginMcpTool, and packages/core/src/emdash-runtime.ts accepts and forwards that caller into handlePluginApiRoute. The tests in packages/core/tests/unit/mcp/authorization.test.ts and packages/core/tests/integration/runtime/plugin-media-route.test.ts assert the caller is passed.

  2. AGENTS.md comment violations are gone. I searched the changed source files and generated wrapper templates for #812, issue/PR references, and justification language like "intentionally" in the newly added comments; none remain. Existing issue references such as #1853, #2146, and #1293 are in comments from earlier PRs, not introduced here.

Coverage is solid: the catch-all tests verify caller forwarding for private routes and omission for public routes with an ambient session; adapt-sandbox-entry.test.ts verifies routeCtx.user placement and pluginCtx exclusion; routes.test.ts verifies toRouteCallerInfo strips extra host-side fields and converts Date to ISO; MCP tests verify the caller reaches the runtime. The changeset covers the published packages that changed.

No new logic bugs, security gaps, or AGENTS.md violations were found. Approvable as-is.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Aug 16, 2026
@ascorbic
ascorbic merged commit 8313255 into emdash-cms:main Aug 16, 2026
47 checks passed
@emdashbot emdashbot Bot mentioned this pull request Aug 16, 2026
@swissky
swissky deleted the feat/plugin-route-caller branch September 10, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose authenticated user to plugin route handlers via RouteContext

2 participants