fix(planner): keep journal callback token off the client (#2 Phase A)

The Save-to-Journal flow had the browser fetch the journal with a
\`Bearer \${callbackToken}\` header. The JWT was visible in DevTools,
exfiltratable via any XSS or browser extension, and the planner's
\`loader\` shipped it down to the client as part of the page payload.

Now:

- **New action**: \`POST /api/save-to-journal\` (\`routes/api.save-to-journal.ts\`).
  Body: \`{ sessionId, gpx }\`. The action loads \`callbackUrl\` +
  \`callbackToken\` from \`planner.sessions\` (set at /new time when the
  user came from the journal), POSTs to the journal server-to-server
  with the Bearer, and forwards the response.

- **\`SaveToJournalButton\`**: drops the \`callbackUrl\` + \`callbackToken\`
  props. Takes \`sessionId\` only and POSTs to the planner action.

- **\`session.\$id.tsx\` loader**: stops returning \`callbackUrl\` /
  \`callbackToken\` to the client. Returns a single \`hasJournalCallback\`
  boolean so the button still knows whether to render.

- **\`SessionView\`**: same prop simplification.

Trust model is unchanged: the same \`sessionId\` that grants Yjs
membership grants save authority. Knowing the URL = ability to act.
The action only adds a server-side hop so the JWT never reaches
browser JS.

Phase B (jti single-use enforcement on the journal side) follows in
a separate PR — needs a journal DB column + verifier change.

Full repo: pnpm typecheck / lint / test all green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-05-26 00:29:24 +02:00
parent 3dcc17152b
commit 0917de6080
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
5 changed files with 101 additions and 21 deletions

View file

@ -0,0 +1,74 @@
// Server-side proxy for "Save to Journal". Looks up the session's
// callbackUrl + callbackToken (stored at /new time when the user came
// from the journal) and POSTs the GPX to the journal as a Bearer.
//
// Why this exists (planner-audit #2, Phase A): the previous flow had
// the browser fetch with the bearer token directly, exposing it in
// DevTools / to any XSS / browser extension. Now the token never
// leaves the planner's server-side trust boundary.
//
// Trust model: the same sessionId that grants Yjs membership grants
// save authority. Knowing the URL = ability to act. This matches the
// existing model — we're not strengthening or weakening it, just
// keeping the JWT off the wire to the browser.
import { data } from "react-router";
import type { Route } from "./+types/api.save-to-journal";
import { getSession } from "~/lib/sessions";
import { fetchWithTimeout } from "~/lib/http.server";
interface SaveRequestBody {
sessionId?: unknown;
gpx?: unknown;
}
const MAX_GPX_BYTES = 5 * 1024 * 1024; // 5 MB — same ceiling as the Yjs doc cap
export async function action({ request }: Route.ActionArgs) {
if (request.method !== "POST") {
return data({ error: "Method not allowed" }, { status: 405 });
}
let body: SaveRequestBody;
try {
body = (await request.json()) as SaveRequestBody;
} catch {
return data({ error: "Invalid JSON" }, { status: 400 });
}
const sessionId = typeof body.sessionId === "string" ? body.sessionId : "";
const gpx = typeof body.gpx === "string" ? body.gpx : "";
if (!sessionId) return data({ error: "sessionId required" }, { status: 400 });
if (!gpx) return data({ error: "gpx required" }, { status: 400 });
if (gpx.length > MAX_GPX_BYTES) {
return data({ error: "gpx too large" }, { status: 413 });
}
const session = await getSession(sessionId);
if (!session) return data({ error: "session not found" }, { status: 404 });
if (!session.callbackUrl || !session.callbackToken) {
return data({ error: "session has no journal callback" }, { status: 400 });
}
let resp: Response;
try {
resp = await fetchWithTimeout(session.callbackUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${session.callbackToken}`,
},
body: JSON.stringify({ gpx }),
});
} catch {
return data({ error: "journal unreachable" }, { status: 502 });
}
// Forward the journal's response (status + body) so the client UI
// can render the same error/success it would have before.
const text = await resp.text();
let payload: unknown;
try { payload = JSON.parse(text); } catch { payload = { raw: text }; }
return data(payload, { status: resp.status });
}

View file

@ -20,10 +20,13 @@ export async function loader({ params }: Route.LoaderArgs) {
if (!session) {
throw data({ error: "Session not found" }, { status: 404 });
}
// Don't leak the JWT token to the client. The save flow uses
// /api/save-to-journal, which loads token + URL from the DB
// server-side. The browser only needs to know whether the button
// should render.
return data({
sessionId: session.id,
callbackUrl: session.callbackUrl ?? null,
callbackToken: session.callbackToken ?? null,
hasJournalCallback: Boolean(session.callbackUrl && session.callbackToken),
});
});
}
@ -89,8 +92,7 @@ export default function SessionPage({ loaderData }: Route.ComponentProps) {
>
<SessionView
sessionId={id!}
callbackUrl={loaderData.callbackUrl ?? undefined}
callbackToken={loaderData.callbackToken ?? undefined}
hasJournalCallback={loaderData.hasJournalCallback}
returnUrl={returnUrl}
initialWaypoints={initialWaypoints}
initialNoGoAreas={initialNoGoAreas}