Today both proxies are effectively open to anyone who can set an Origin header for trails.cool — a third party can use us as a free BRouter/Overpass relay. Require a live planner session on every call so abuse traffic costs the scraper a session row (observable, revocable) before they can issue a single query. Server: - New `requireSession(id)` helper — returns the session row or a 401 Response. Reused by both route handlers. - `/api/route`: `sessionId` in body is now required and verified; rate-limit key always falls back to the session id. - `/api/overpass`: new `X-Trails-Session` header, verified. Header keeps the session out of the request body so the body-keyed cache is unaffected. Client plumbing: - `useRouting(yjs, sessionId)` — sessionId goes into the /api/route body. - `usePois(sessionId)` → `queryPois(..., sessionId)` → `X-Trails-Session` on the proxy call. - `PlannerMap` + `YjsDebugPanel` gain a `sessionId` prop from `SessionView`. Journal server-to-server: - Demo-bot and `/api/v1/routes/compute` now POST `/api/sessions` to mint a throwaway planner session, then cite it on the forwarded `/api/route` call. Planner's `expire-sessions` cron cleans these up (7d window) so nothing needs explicit teardown. Tests: - 5 unit tests for `requireSession` covering missing / empty / non-string / unknown-session / valid-session cases. - Two integration E2E tests document the 401 for missing session on each proxy. - Pre-existing `/api/route` integration tests updated to mint a session first. Caveat: existing browser tabs lose their /api/route ability until reload (the old JS doesn't know to send sessionId). Acceptable for an anonymous planner. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import type { Route } from "./+types/api.v1.routes.compute";
|
|
import { requireApiUser, apiError } from "~/lib/api-guard.server";
|
|
import { ComputeRouteRequestSchema, ERROR_CODES, zodIssuesToFieldErrors } from "@trails-cool/api";
|
|
|
|
const PLANNER_URL = process.env.PLANNER_URL ?? "http://localhost:3001";
|
|
|
|
/** POST /api/v1/routes/compute — server-to-server proxy through the planner. */
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") return new Response(null, { status: 405 });
|
|
await requireApiUser(request);
|
|
|
|
const body = await request.json().catch(() => null);
|
|
const parsed = ComputeRouteRequestSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
|
|
zodIssuesToFieldErrors(parsed.error));
|
|
}
|
|
|
|
// The planner session-binds its /api/route proxy. Mint a throwaway
|
|
// session we can cite as the auth on the forwarded call; planner's
|
|
// expire-sessions cron tidies it up.
|
|
let sessionId: string | null = null;
|
|
try {
|
|
const sessResp = await fetch(`${PLANNER_URL}/api/sessions`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: "{}",
|
|
});
|
|
if (sessResp.ok) {
|
|
const payload = (await sessResp.json()) as { sessionId?: string };
|
|
sessionId = payload.sessionId ?? null;
|
|
}
|
|
} catch {
|
|
/* fall through — sessionId stays null and the fetch below will 401 */
|
|
}
|
|
|
|
try {
|
|
const resp = await fetch(`${PLANNER_URL}/api/route`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ ...parsed.data, sessionId }),
|
|
});
|
|
if (!resp.ok) {
|
|
return apiError(resp.status === 422 ? 422 : 502, ERROR_CODES.INTERNAL_ERROR, `Planner returned ${resp.status}`);
|
|
}
|
|
const payload = await resp.json();
|
|
return Response.json(payload);
|
|
} catch {
|
|
return apiError(502, ERROR_CODES.INTERNAL_ERROR, "Planner unavailable");
|
|
}
|
|
}
|