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>
70 lines
2.5 KiB
TypeScript
70 lines
2.5 KiB
TypeScript
import { data } from "react-router";
|
|
import type { Route } from "./+types/api.route";
|
|
import { computeRoute, computeSegmentGpx, BRouterError } from "~/lib/brouter";
|
|
import { checkRateLimit } from "~/lib/rate-limit";
|
|
import { requireSession } from "~/lib/require-session";
|
|
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") {
|
|
return data({ error: "Method not allowed" }, { status: 405 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { waypoints, profile, sessionId, noGoAreas, format } = body as {
|
|
waypoints: Array<{ lat: number; lon: number }>;
|
|
profile?: string;
|
|
sessionId?: string;
|
|
noGoAreas?: Array<{ points: Array<{ lat: number; lon: number }> }>;
|
|
format?: "geojson" | "gpx";
|
|
};
|
|
|
|
if (!waypoints || waypoints.length < 2) {
|
|
return data({ error: "At least 2 waypoints are required" }, { status: 400 });
|
|
}
|
|
|
|
// Session-bind: only live planner sessions can compute routes through
|
|
// us, so we don't act as an anonymous BRouter proxy for scrapers.
|
|
const session = await requireSession(sessionId);
|
|
if (session instanceof Response) return session;
|
|
|
|
// Rate limit by session ID (always present after requireSession)
|
|
const limit = checkRateLimit(`route:${session.id}`);
|
|
|
|
if (!limit.allowed) {
|
|
return data(
|
|
{ error: "Rate limit exceeded" },
|
|
{
|
|
status: 429,
|
|
headers: { "Retry-After": String(limit.retryAfterSeconds) },
|
|
},
|
|
);
|
|
}
|
|
|
|
try {
|
|
// `format: "gpx"` returns BRouter's raw GPX without the way-tag
|
|
// enrichment the interactive planner needs. Used by server-to-server
|
|
// callers (e.g. the Journal's demo-bot) that just want the track.
|
|
if (format === "gpx") {
|
|
const gpx = await computeSegmentGpx({ waypoints, profile, noGoAreas });
|
|
return new Response(gpx, {
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "application/gpx+xml; charset=utf-8",
|
|
"x-ratelimit-remaining": String(limit.remaining),
|
|
},
|
|
});
|
|
}
|
|
|
|
const route = await computeRoute({ waypoints, profile, noGoAreas });
|
|
return data(route, {
|
|
headers: { "X-RateLimit-Remaining": String(limit.remaining) },
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof BRouterError && e.statusCode >= 400 && e.statusCode < 500) {
|
|
// BRouter client error: unroutable waypoints, missing tiles, etc.
|
|
return data({ error: e.message, code: "no_route" }, { status: 422 });
|
|
}
|
|
const message = e instanceof Error ? e.message : "Route computation failed";
|
|
return data({ error: message, code: "server_error" }, { status: 502 });
|
|
}
|
|
}
|