Public: anyone can read this; the crew can change it.
A person who is not signed in gets a 401 with a sign-in link from every MCP tool call, never a stack trace #13056
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/13049-a-person-who-is-not-signed-in-gets-a-401 → main 2 commits fork
2 files
changed +60
−1
src/http/mcp.ts +12 −1
| @@ -502,6 +502,8 @@ async function startAnswer(ctx: Ctx, id: unknown, args: Args): Promise<unknown> | ||
| 502 | 502 | ); |
| 503 | 503 | } |
| 504 | 504 | |
| 505 | const ANON_HINT = "Sign in, make a token, and send it as `Authorization: Bearer <token>`."; | |
| 506 | ||
| 505 | 507 | const NOT_HERE = (allow: string): Response => |
| 506 | 508 | new Response(null, { status: 405, headers: { allow } }); |
| 507 | 509 | |
| @@ -562,9 +564,18 @@ export function mcpRoutes(router: Router<Handler>): void { | ||
| 562 | 564 | if (reported !== undefined) return json(await reported); |
| 563 | 565 | // The refusal keeps the OAuth challenge a client starts from, and names |
| 564 | 566 | // the one thing a stranger can do without signing in. |
| 567 | // The same body is a JSON-RPC error: a code, one plain sentence, the sign-in URL. | |
| 568 | const signIn = new URL(signInHref(ctx), ctx.url.origin).href; | |
| 569 | const rpcId = (message as Record<string, unknown>)["id"]; | |
| 565 | 570 | return new Response( |
| 566 | 571 | JSON.stringify({ |
| 567 | error: "unauthorized", | |
| 572 | jsonrpc: "2.0", | |
| 573 | id: typeof rpcId === "string" || typeof rpcId === "number" ? rpcId : null, | |
| 574 | error: { | |
| 575 | code: -32001, | |
| 576 | message: `Sign in to use this tool: ${signIn}`, | |
| 577 | data: { error: "unauthorized", signIn, hint: ANON_HINT }, | |
| 578 | }, | |
| 568 | 579 | hint: "Send `Authorization: Bearer <token>` with a token you hold, then repeat the call; without one, only `report`, `start` and `offer` answer.", |
| 569 | 580 | message: |
| 570 | 581 | "send a forge token: Authorization: Bearer <token>. Without one, the tool `start` (tools/call, name \"start\", arguments {name, description?, agent?}) makes you an account, a repo and a live app with a token; the tool `report` (tools/call, name \"report\", arguments {repo, title, body?, page?, agent?}) files a problem report on a public repository; and the tool `offer` (tools/call, name \"offer\", arguments {repo, title, patch}) offers a patch to a public repository; see /llms.txt", |
test/mcp-anonymous.test.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | /** | |
| 2 | * An anonymous MCP call fails kindly (#13049): for every tool the manifest lists, a 401 | |
| 3 | * whose body is a JSON-RPC error with a code, one plain sentence and the sign-in URL, | |
| 4 | * and no path or stack. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { toolNames } from "../src/http/mcp.ts"; | |
| 13 | import { TOKEN_FREE_TOOLS } from "../src/core/mcp_card.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string, app: App, servers: Servers; | |
| 18 | beforeAll(async () => { | |
| 19 | dir = await mkdtemp(join(tmpdir(), "forge-mcp-anon-")); | |
| 20 | app = createApp({ dataDir: dir, port: 0 }); | |
| 21 | servers = await serve(app); | |
| 22 | }); | |
| 23 | afterAll(async () => { | |
| 24 | await servers.stop(); | |
| 25 | await rm(dir, { recursive: true, force: true }); | |
| 26 | }); | |
| 27 | ||
| 28 | test("every tool, called without credentials, answers a JSON-RPC error with a sign-in URL", async () => { | |
| 29 | const names = toolNames().filter((n) => !TOKEN_FREE_TOOLS.includes(n)); | |
| 30 | expect(names.length).toBeGreaterThan(10); | |
| 31 | for (const name of names) { | |
| 32 | const res = await fetch(`${servers.url}/mcp`, { | |
| 33 | method: "POST", | |
| 34 | headers: { "content-type": "application/json", accept: "application/json" }, | |
| 35 | body: JSON.stringify({ jsonrpc: "2.0", id: 7, method: "tools/call", params: { name, arguments: {} } }), | |
| 36 | }); | |
| 37 | const text = await res.text(); | |
| 38 | const body = JSON.parse(text); | |
| 39 | expect(res.status, name).toBe(401); | |
| 40 | expect(body.jsonrpc, name).toBe("2.0"); | |
| 41 | expect(body.id, name).toBe(7); | |
| 42 | expect(typeof body.error.code, name).toBe("number"); | |
| 43 | expect(body.error.message, name).toContain("Sign in"); | |
| 44 | expect(body.error.message, name).toContain("/signin"); | |
| 45 | expect(body.error.data.signIn, name).toMatch(/^https?:\/\/.+\/signin/); | |
| 46 | expect(text, name).not.toMatch(/\bat .*\.ts|\/src\/|\/data\/|node_modules|Error:/); | |
| 47 | } | |
| 48 | }); |
new above
offline