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:
parent
3dcc17152b
commit
0917de6080
5 changed files with 101 additions and 21 deletions
|
|
@ -8,12 +8,11 @@ import { waypointFromYMap } from "~/lib/waypoint-ymap";
|
|||
|
||||
interface SaveToJournalButtonProps {
|
||||
yjs: YjsState;
|
||||
callbackUrl: string;
|
||||
callbackToken: string;
|
||||
sessionId: string;
|
||||
returnUrl?: string;
|
||||
}
|
||||
|
||||
export function SaveToJournalButton({ yjs, callbackUrl, callbackToken, returnUrl }: SaveToJournalButtonProps) {
|
||||
export function SaveToJournalButton({ yjs, sessionId, returnUrl }: SaveToJournalButtonProps) {
|
||||
const { t } = useTranslation("planner");
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saved, setSaved] = useState(false);
|
||||
|
|
@ -47,14 +46,14 @@ export function SaveToJournalButton({ yjs, callbackUrl, callbackToken, returnUrl
|
|||
const notes = yjs.notes.toString() || undefined;
|
||||
const gpx = generateGpx({ name: "trails.cool route", description: notes, waypoints, tracks, noGoAreas });
|
||||
|
||||
// POST to Journal callback
|
||||
const response = await fetch(callbackUrl, {
|
||||
// POST to the planner's server-side proxy. The proxy attaches the
|
||||
// journal Bearer token (stored on the session row) and forwards
|
||||
// the GPX. Token never leaves the planner server — see
|
||||
// routes/api.save-to-journal.ts.
|
||||
const response = await fetch("/api/save-to-journal", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${callbackToken}`,
|
||||
},
|
||||
body: JSON.stringify({ gpx }),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sessionId, gpx }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
|
@ -68,7 +67,7 @@ export function SaveToJournalButton({ yjs, callbackUrl, callbackToken, returnUrl
|
|||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [yjs, callbackUrl, callbackToken]);
|
||||
}, [yjs, sessionId]);
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
|
|
|
|||
|
|
@ -148,15 +148,20 @@ function SidebarTabs({ yjs, routeStats, days, onWaypointHover, onWaypointSelect
|
|||
|
||||
interface SessionViewProps {
|
||||
sessionId: string;
|
||||
callbackUrl?: string;
|
||||
callbackToken?: string;
|
||||
/**
|
||||
* True when the session was created with a journal callback URL +
|
||||
* token (i.e. the user came in from /journal/.../edit-in-planner).
|
||||
* The actual URL + token live server-side; the browser only needs
|
||||
* to know whether to render the Save-to-Journal button.
|
||||
*/
|
||||
hasJournalCallback?: boolean;
|
||||
returnUrl?: string;
|
||||
initialWaypoints?: Array<{ lat: number; lon: number; name?: string; isDayBreak?: boolean }>;
|
||||
initialNoGoAreas?: Array<{ points: Array<{ lat: number; lon: number }> }>;
|
||||
initialNotes?: string;
|
||||
}
|
||||
|
||||
export function SessionView({ sessionId, callbackUrl, callbackToken, returnUrl, initialWaypoints, initialNoGoAreas, initialNotes }: SessionViewProps) {
|
||||
export function SessionView({ sessionId, hasJournalCallback, returnUrl, initialWaypoints, initialNoGoAreas, initialNotes }: SessionViewProps) {
|
||||
const { t } = useTranslation("planner");
|
||||
useEffect(() => { Sentry.setTag("session_id", sessionId); }, [sessionId]);
|
||||
const yjs = useYjs(sessionId, initialWaypoints, initialNoGoAreas, initialNotes);
|
||||
|
|
@ -253,11 +258,10 @@ export function SessionView({ sessionId, callbackUrl, callbackToken, returnUrl,
|
|||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 sm:gap-3 shrink-0">
|
||||
{callbackUrl && callbackToken && (
|
||||
{hasJournalCallback && (
|
||||
<SaveToJournalButton
|
||||
yjs={yjs}
|
||||
callbackUrl={callbackUrl}
|
||||
callbackToken={callbackToken}
|
||||
sessionId={sessionId}
|
||||
returnUrl={returnUrl}
|
||||
/>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue