fix(journal): single-use JWT enforcement for route callback tokens (#2 Phase B)
After Phase A (#442) moved the journal callback token off the browser, the token was still replayable on the wire until \`exp\` (7 days). This PR makes each token strictly single-use. Changes: - **\`journal.consumed_jwt_jti\` table** — \`jti TEXT PRIMARY KEY, consumed_at TIMESTAMPTZ, expires_at TIMESTAMPTZ\`. Picked up by drizzle-kit push on deploy. - **\`createRouteToken\` now sets a \`jti\` claim** (\`randomUUID()\`). - **\`verifyRouteToken\` atomically consumes the jti** via \`INSERT … ON CONFLICT DO NOTHING RETURNING jti\`. Postgres serializes the insert, so exactly one concurrent caller wins; the rest see an empty result and throw \`TokenAlreadyConsumedError\`. Tokens without a \`jti\` claim (i.e. minted before this PR) are also rejected — the right call: any in-flight legacy token sitting in a planner session is replayable, and we'd rather fail-loud than silently grandfather them in. - **\`consumed-jti-sweep\` job** — daily 03:45 UTC cron that \`DELETE WHERE expires_at < now()\`. Keeps the table tiny; offset from the other purge jobs to spread load. - **e2e replay test** — \`integration.test.ts\` now exercises a same-token double-submit and asserts the second returns 401 with \`/consumed|already/i\`. UX implication worth flagging: a user who clicks \"Save\" twice (or whose network retries a failed POST) sees an error on the second attempt. They go back to the journal for a fresh \"Edit in Planner\" link. Full repo: pnpm typecheck / lint / test all green (177 + 31 integration). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
c525240d0e
commit
b4c64a40e7
5 changed files with 115 additions and 1 deletions
|
|
@ -71,6 +71,28 @@ test.describe("Integration: Planner callback → geometry stored", () => {
|
|||
});
|
||||
expect(resp.status()).toBe(401);
|
||||
});
|
||||
|
||||
test("token is single-use — second submit is rejected (Phase B replay guard)", async ({ request }) => {
|
||||
const seedResp = await request.post(`${JOURNAL}/api/e2e/seed`);
|
||||
const { routeId, token } = await seedResp.json() as { routeId: string; token: string };
|
||||
|
||||
// First save succeeds.
|
||||
const first = await request.post(`${JOURNAL}/api/routes/${routeId}/callback`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
data: { gpx: VALID_GPX },
|
||||
});
|
||||
expect(first.status()).toBe(200);
|
||||
|
||||
// Second attempt with the *same* token must fail — the jti has
|
||||
// been consumed.
|
||||
const second = await request.post(`${JOURNAL}/api/routes/${routeId}/callback`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
data: { gpx: VALID_GPX },
|
||||
});
|
||||
expect(second.status()).toBe(401);
|
||||
const body = await second.json() as { error: string };
|
||||
expect(body.error).toMatch(/consumed|already/i);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe("Integration: Journal ↔ Planner handoff", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue