Public: anyone can read this; the crew can change it.

From outside: Token minting at /settings/tokens returns 403 {"error":"forbidden"} for a fresh browser session #13730

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/13713-from-outside-token-minting-at-settings-t → main 1 commit fork

  • Merged. torque/13713-from-outside-token-minting-at-settings-t was merged into main as 69bee83.

Review

Sign in to review this change.
1 file changed +71 −0
test/settings-token-mint-session.test.ts added +71
@@ -0,0 +1,71 @@
1 /**
2 * A signed-in browser session mints an access code from /settings/tokens
3 * (#13713): an ordinary account (no admin, no token) is not refused, whether
4 * the form's Origin is the forge's, absent, or `null` under a strict
5 * referrer policy with the browser's own Sec-Fetch-Site; a cross-site Origin
6 * and a token caller still are.
7 */
8 import { afterAll, beforeAll, expect, test } from "bun:test";
9 import { mkdtempSync, rmSync } from "node:fs";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { createSession } from "../src/core/sessions.ts";
14 import { listTokens } from "../src/core/tokens.ts";
15 import { createUser } from "../src/core/users.ts";
16 import { serve, type Servers } from "../src/http/server.ts";
17 import { quietLogs } from "./helpers/globals.ts";
18
19 quietLogs();
20
21 let root = "";
22 let app: App;
23 let servers: Servers;
24 let cookie = "";
25 let userId = "";
26
27 beforeAll(async () => {
28 root = mkdtempSync(join(tmpdir(), "forge-token-mint-"));
29 app = createApp({ dataDir: root, port: 0 });
30 const first = await createUser(app.db, { origin: "seed", username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
31 if (!first.ok) throw new Error(first.error);
32 const muse = await createUser(app.db, { origin: "seed", username: "muse", email: "[email protected]", password: "a-good-passphrase" });
33 if (!muse.ok) throw new Error(muse.error);
34 userId = muse.value.id;
35 cookie = `forge_session=${createSession(app.db, userId).token}`;
36 servers = await serve(app);
37 });
38 afterAll(() => {
39 servers.stop();
40 app.close();
41 rmSync(root, { recursive: true, force: true });
42 });
43
44 const mint = (name: string, headers: Record<string, string>) =>
45 fetch(`${servers.url}/settings/tokens`, {
46 method: "POST",
47 headers: { cookie, ...headers },
48 body: new URLSearchParams({ name, scope: "rw" }),
49 redirect: "manual",
50 });
51
52 test("a session mints from the form, with the forge's Origin, none, or a null one the browser vouches for", async () => {
53 const cases: Record<string, Record<string, string>> = {
54 "same-origin": { origin: servers.url },
55 "no-origin": {},
56 "json-accept": { origin: servers.url, accept: "application/json" },
57 "null-origin": { origin: "null", "sec-fetch-site": "same-origin" },
58 };
59 for (const [name, headers] of Object.entries(cases)) {
60 const res = await mint(name, headers);
61 expect(res.status).toBe(303);
62 }
63 const names = listTokens(app.db, userId).map((t) => t.name);
64 for (const name of Object.keys(cases)) expect(names).toContain(name);
65 });
66
67 test("a cross-site Origin is refused and mints nothing", async () => {
68 const res = await mint("evil", { origin: "https://evil.example" });
69 expect(res.status).toBe(403);
70 expect(listTokens(app.db, userId).some((t) => t.name === "evil")).toBe(false);
71 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs