-
Notifications
You must be signed in to change notification settings - Fork 2
Propose: filter MCP logs and surface token activity stats #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2f45ce3
docs(openspec): propose mcp log filters and token activity stats
b022eba
feat(mcp): filter MCP logs and surface token activity stats
621ccc1
chore(openspec): archive bundle-dependabot-prs
ffdac38
fix(api): tighten input validation on MCP log and token routes
555b04a
fix(security): harden MCP session resume, scope SQL allow-list, add C…
f403ca4
test(e2e): assert JSON-RPC error envelope on MCP auth failures
d07e695
fix(security): tighten CSRF middleware and SQL validator denylists
5cfbfde
test(e2e): pass Bearer token on remaining MCP scope-enforcement tool …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { describe, it, expect, beforeAll } from "vitest"; | ||
| import { Hono } from "hono"; | ||
| import { csrfMiddleware } from "./csrf"; | ||
|
|
||
| beforeAll(() => { | ||
| process.env.CORS_ORIGINS = "http://localhost:5173,https://app.example.com"; | ||
| process.env.MONGODB_URI = "mongodb://localhost:27017/test"; | ||
| process.env.BETTER_AUTH_SECRET = "test-secret-with-at-least-32-chars-long"; | ||
| process.env.UI_PASSWORD = "test-password"; | ||
| }); | ||
|
|
||
| function buildApp() { | ||
| const app = new Hono(); | ||
| app.use("/api/*", csrfMiddleware); | ||
| app.post("/api/projects/x/mcp-tokens", (c) => c.json({ ok: true })); | ||
| app.delete("/api/projects/x/mcp-tokens/y", (c) => c.json({ ok: true })); | ||
| app.get("/api/projects/x/mcp-tokens", (c) => c.json({ ok: true })); | ||
| app.post("/api/auth/sign-in/email", (c) => c.json({ ok: true })); | ||
| return app; | ||
| } | ||
|
|
||
| describe("csrfMiddleware", () => { | ||
| it("allows GET without Origin", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens"); | ||
| expect(res.status).toBe(200); | ||
| }); | ||
|
|
||
| it("rejects POST when both Origin and Referer are absent", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json" }, | ||
| body: "{}", | ||
| }); | ||
| expect(res.status).toBe(403); | ||
| const body = (await res.json()) as { error: string }; | ||
| expect(body.error).toMatch(/missing Origin/i); | ||
| }); | ||
|
|
||
| it("rejects DELETE when both Origin and Referer are absent", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens/y", { | ||
| method: "DELETE", | ||
| }); | ||
| expect(res.status).toBe(403); | ||
| }); | ||
|
|
||
| it("allows POST from trusted Origin", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| origin: "http://localhost:5173", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(res.status).toBe(200); | ||
| }); | ||
|
|
||
| it("rejects POST from foreign Origin", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| origin: "https://evil.example.com", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(res.status).toBe(403); | ||
| }); | ||
|
|
||
| it("rejects DELETE from foreign Origin", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens/y", { | ||
| method: "DELETE", | ||
| headers: { origin: "https://evil.example.com" }, | ||
| }); | ||
| expect(res.status).toBe(403); | ||
| }); | ||
|
|
||
| it("falls back to Referer when Origin is absent", async () => { | ||
| const app = buildApp(); | ||
|
|
||
| const ok = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| referer: "http://localhost:5173/some/page", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(ok.status).toBe(200); | ||
|
|
||
| const bad = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| referer: "https://evil.example.com/page", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(bad.status).toBe(403); | ||
| }); | ||
|
|
||
| it("skips /api/auth/* (Better Auth handles its own CSRF)", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/auth/sign-in/email", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| origin: "https://evil.example.com", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(res.status).toBe(200); | ||
| }); | ||
|
|
||
| it("rejects malformed Origin header", async () => { | ||
| const app = buildApp(); | ||
| const res = await app.request("/api/projects/x/mcp-tokens", { | ||
| method: "POST", | ||
| headers: { | ||
| "content-type": "application/json", | ||
| origin: "not a url", | ||
| }, | ||
| body: "{}", | ||
| }); | ||
| expect(res.status).toBe(403); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Context, Next } from "hono"; | ||
| import { getEnv } from "@archmax/core/config/env"; | ||
|
|
||
| const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]); | ||
|
|
||
| function originFromHeader(value: string): string | null { | ||
| try { | ||
| return new URL(value).origin; | ||
| } catch { | ||
| // Origin header may itself be a bare origin like "https://example.com" | ||
| // (no path). new URL() accepts that, so failures here mean garbage input. | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * CSRF / origin enforcement for cookie-authenticated mutation routes. | ||
| * | ||
| * Every state-changing `/api/*` request (POST/PUT/PATCH/DELETE) is required | ||
| * to carry an `Origin` (or `Referer`) header that resolves to one of | ||
| * `corsOrigins`. Real browsers always attach `Origin` on credentialed | ||
| * non-GET requests, so this stops browser-driven CSRF without needing a | ||
| * separate token round-trip. Missing both headers is *also* rejected: the | ||
| * routes below this middleware are session-cookie authenticated, so a | ||
| * caller that suppresses Origin/Referer would otherwise drive cookie | ||
| * mutations unprotected. Non-browser API clients should authenticate via | ||
| * the dedicated bearer-token MCP surface (`/mcp/:slug/mcp`) instead. | ||
| * | ||
| * `/api/auth/*` is exempted because Better Auth runs before this middleware | ||
| * in app.ts and applies its own CSRF protection. | ||
| */ | ||
| export async function csrfMiddleware(c: Context, next: Next) { | ||
| if (SAFE_METHODS.has(c.req.method)) return next(); | ||
| if (c.req.path.startsWith("/api/auth/")) return next(); | ||
|
|
||
| const originHeader = c.req.header("origin"); | ||
| const refererHeader = c.req.header("referer"); | ||
|
|
||
| if (!originHeader && !refererHeader) { | ||
| return c.json( | ||
| { error: "Forbidden: missing Origin/Referer header" }, | ||
| 403, | ||
| ); | ||
| } | ||
|
|
||
| const trusted = new Set(getEnv().corsOrigins); | ||
| const candidate = originHeader ?? refererHeader!; | ||
| const origin = originFromHeader(candidate); | ||
|
|
||
| if (!origin || !trusted.has(origin)) { | ||
| return c.json({ error: "Forbidden: invalid request origin" }, 403); | ||
| } | ||
|
|
||
| await next(); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.