Skip to content

fix(agents): authenticate Mastra's built-in routes (foreman-359q) - #60

Merged
hamchowderr merged 1 commit into
mainfrom
claude/project-thread-ktl133
Oct 24, 2026
Merged

hamchowderr merged 1 commit into
mainfrom
claude/project-thread-ktl133

Conversation

@hamchowderr

Copy link
Copy Markdown
Owner

Before: server.auth was never set, so Mastra let every built-in /api/* route through to anyone (checkRouteAuth in @mastra/server returns early when no auth is configured). That covered memory threads, stored agents, the agent-controller session routes that take the user id from the URL, a2a, and background tasks, plus our own /background-tasks/stream.

After: Mastra authenticates its built-in routes and our apiRoutes with Foreman's identity resolver, which accepts a Supabase JWT or an fmn_ key. Signing in isn't enough for the built-in /api/* routes, because they accept any resourceId or thread id the caller sends, so one user could read another user's threads. authorize therefore denies them to signed-in users too. The web app never calls them; it uses the chat-kit routes, which scope by user. Two things keep working:

  • Mastra Studio, under mastra dev, uses the same x-mastra-dev-playground rule Mastra applies when no auth is set.
  • Routes declared requiresAuth: false, such as the native channel webhooks, skip the check entirely.

How: lib/server-auth.ts exports a plain MastraAuthConfig (authenticateToken and authorize), and the Mastra instance sets it as server.auth. Identity is memoised per request, so the chat-kit middleware, Mastra's route auth and /chat share one lookup. Our own Hono routes are dispatched in server.middleware and already check auth themselves, so nothing changes for them. Finding 1 of the Mastra/Zapier audit.

Checked: tsc, Biome, vitest 558 passed / 10 skipped, plus new unit tests in tests/unit/server-auth.test.ts. I haven't exercised this against a running server. That still needs checking: Studio under mastra dev, the web chat signed in, and curl /api/memory/threads returning 401.


Generated by Claude Code

server.auth was never set, so Mastra let every built-in /api route
through: memory threads, stored agents, agent-controller sessions keyed
by a resourceId in the URL, a2a, background tasks. Set it to Foreman's
identity resolver (Supabase JWT or fmn_ key). Those routes trust the
caller's resourceId, so authorize also closes /api/* to signed-in users;
the web app never calls them. Studio still works under mastra dev, and
requiresAuth:false routes (native channel webhooks) are unaffected.
The chat-kit middleware and /chat share one identity lookup per request.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JGhMwdrzUdii5pspk2YmwG
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
foreman Ready Ready Preview Oct 22, 2026 5:38pm UTC

Request Review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-22T17:41:01.539711Z
We couldn’t complete this review. Comment @codex review to try again.
0c6d9a9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@hamchowderr
hamchowderr merged commit 526bf41 into main Oct 24, 2026
8 checks passed

This branch was successfully deployed

1 active deployment
Preview — 0c6d9a95 Deployed Oct 22, 2026 by vercel[bot]
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.

2 participants