fix(api): require a signed-in user for the BRouter route proxy #43

Merged
jmz merged 2 commits from fix/brouter-auth into deploy 2026-11-01 09:14:04 +00:00
Member

Problem

POST /api/v1/brouter/route answers anonymous callers (found by the M4 review, docs/fork/m4-brouter-vs-valhalla.md in #42). That includes the rawProfile and params paths, which upload a profile into BRouter's custom-profile store. wanderer.bearoverse.com is LAN/tailnet only, so the exposure is internal, but anonymous profile upload is still wrong.

The "upload paths" are not separate endpoints. They are branches of the same route handler, which posts to BRouter's /brouter/profile from the server. BRouter itself is a ClusterIP service and the browser never reaches it.

Fix

  • The handler checks the wanderer session first, before the BROUTER_URL config and before parsing the body. Without a session it returns 401 {"message":"Unauthorized"} and never calls BRouter.
  • It uses the same check as the trail-analyze proxy: locals.user.id plus locals.pb.authStore.isValid === true. hooks.server.ts fills both from the pb_auth cookie or a wanderer_key API token, after authRefresh. The check moves into $lib/server/session.ts (verifiedUserId). authorizeTrailAnalyze now calls it; its behaviour does not change, and the 63 trail-analyze tests are unchanged and pass.
  • The swagger block documents the 401.

Why gate the whole endpoint, not only the upload paths

  • Callers. The only callers are BRouterAdapter, reached through getRouter() from valhalla_store (calculateRouteBetween), route_editor.svelte and loop_generator.svelte. All three are used only by /trail/edit/[id]. No public trail page, list or profile view requests routes, so an anonymous route has no user to serve.
  • Upload is the normal path. translateBRouter turns every non-neutral preference (technicality, flat, avoid roads) into params, and params means substitute and upload. Gating only uploads would leave just "route with a named profile" open.
  • Cost. Every BRouter request costs CPU (about 1 s floor, pod request 15m; see the M4 doc), so leaving anonymous routing open is a cheap way to load it.

Not changed: the upstream Valhalla proxy (/api/v1/valhalla/*) stays anonymous as in upstream. It is upstream code, and it cannot upload anything.

Tests (spl-vm03, node 22.23.3, niced)

  • New session.test.ts (8 tests): valid session, plus null for no user, empty locals, an invalid auth store, missing pb, an empty or non-string id, and a truthy non-true isValid.
  • New "authentication" block in brouter-route.test.ts (9 tests):
    • Anonymous requests get 401 for the default profile, a named profile, a params upload and a rawProfile upload, with no BRouter fetch.
    • A record with an invalid auth store gets 401, and so do empty locals.
    • The auth check runs before the BROUTER_URL check and before body parsing.
    • A signed-in user still uploads and routes (200).
    • The existing 18 tests now run with a signed-in locals.
  • Mutation check: with the gate removed, all 8 negative auth tests fail and the rest pass.
  • npm run check: 0 errors. The 2 warnings are already in an untouched planner file.
  • Full npm run test:unit -- --run: 69 files passed, 2 skipped; 810 tests passed, 13 skipped.

For review

  • Base is deploy (same commit as deploy-v0.21.2, ca06282e, as in #41/#42). Production runs deploy-v0.21.2, so after merging, fast-forward deploy-v0.21.2 (or build from deploy) and roll out an image.
  • After rollout, in an anonymous browser or with curl: POST /api/v1/brouter/route should return 401. Signed in, BRouter routing and the loop generator in the trail editor should work as before.
  • Anyone on the editor page while signed out (session expired) now gets an "Unauthorized" API error when routing, where before the route succeeded.
  • Not in scope, possible follow-up: the in-process upload cache (uploadedProfileIds) has no size limit. A signed-in user can still grow it with many distinct rawProfile texts, and BRouter's own profile store grows with them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XMf4EKLnjYyjFp8QGWgFFq

## Problem `POST /api/v1/brouter/route` answers anonymous callers (found by the M4 review, `docs/fork/m4-brouter-vs-valhalla.md` in #42). That includes the `rawProfile` and `params` paths, which upload a profile into BRouter's custom-profile store. wanderer.bearoverse.com is LAN/tailnet only, so the exposure is internal, but anonymous profile upload is still wrong. The "upload paths" are not separate endpoints. They are branches of the same route handler, which posts to BRouter's `/brouter/profile` from the server. BRouter itself is a ClusterIP service and the browser never reaches it. ## Fix - The handler checks the wanderer session **first**, before the `BROUTER_URL` config and before parsing the body. Without a session it returns `401 {"message":"Unauthorized"}` and never calls BRouter. - It uses the same check as the trail-analyze proxy: `locals.user.id` plus `locals.pb.authStore.isValid === true`. `hooks.server.ts` fills both from the `pb_auth` cookie or a `wanderer_key` API token, after `authRefresh`. The check moves into `$lib/server/session.ts` (`verifiedUserId`). `authorizeTrailAnalyze` now calls it; its behaviour does not change, and the 63 trail-analyze tests are unchanged and pass. - The swagger block documents the 401. ## Why gate the whole endpoint, not only the upload paths - **Callers.** The only callers are `BRouterAdapter`, reached through `getRouter()` from `valhalla_store` (`calculateRouteBetween`), `route_editor.svelte` and `loop_generator.svelte`. All three are used only by `/trail/edit/[id]`. No public trail page, list or profile view requests routes, so an anonymous route has no user to serve. - **Upload is the normal path.** `translateBRouter` turns every non-neutral preference (technicality, flat, avoid roads) into `params`, and `params` means substitute and upload. Gating only uploads would leave just "route with a named profile" open. - **Cost.** Every BRouter request costs CPU (about 1 s floor, pod request 15m; see the M4 doc), so leaving anonymous routing open is a cheap way to load it. Not changed: the upstream Valhalla proxy (`/api/v1/valhalla/*`) stays anonymous as in upstream. It is upstream code, and it cannot upload anything. ## Tests (spl-vm03, node 22.23.3, niced) - New `session.test.ts` (8 tests): valid session, plus null for no user, empty locals, an invalid auth store, missing `pb`, an empty or non-string id, and a truthy non-`true` `isValid`. - New "authentication" block in `brouter-route.test.ts` (9 tests): - Anonymous requests get 401 for the default profile, a named profile, a `params` upload and a `rawProfile` upload, with no BRouter fetch. - A record with an invalid auth store gets 401, and so do empty locals. - The auth check runs before the `BROUTER_URL` check and before body parsing. - A signed-in user still uploads and routes (200). - The existing 18 tests now run with a signed-in `locals`. - **Mutation check:** with the gate removed, all 8 negative auth tests fail and the rest pass. - `npm run check`: 0 errors. The 2 warnings are already in an untouched planner file. - Full `npm run test:unit -- --run`: 69 files passed, 2 skipped; 810 tests passed, 13 skipped. ## For review - **Base is `deploy`** (same commit as `deploy-v0.21.2`, ca06282e, as in #41/#42). Production runs `deploy-v0.21.2`, so after merging, fast-forward `deploy-v0.21.2` (or build from `deploy`) and roll out an image. - After rollout, in an anonymous browser or with curl: `POST /api/v1/brouter/route` should return 401. Signed in, BRouter routing and the loop generator in the trail editor should work as before. - Anyone on the editor page while signed out (session expired) now gets an "Unauthorized" API error when routing, where before the route succeeded. - Not in scope, possible follow-up: the in-process upload cache (`uploadedProfileIds`) has no size limit. A signed-in user can still grow it with many distinct `rawProfile` texts, and BRouter's own profile store grows with them. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01XMf4EKLnjYyjFp8QGWgFFq
fix(api): require a signed-in user for the BRouter route proxy
All checks were successful
CI / go (push) Successful in 24m43s
CI / web (push) Successful in 1m40s
21a7f39e3f
POST /api/v1/brouter/route answered anonymous callers, including the
rawProfile/params paths that upload a profile into BRouter's
custom-profile store (found by the M4 review, PR #42).

The handler now checks the wanderer session first (before config and
body parsing) and returns 401 without one. The check is the one the
trail-analyze proxy already uses (locals.user.id plus a valid
PocketBase auth store, as set by hooks.server.ts for a cookie or a
wanderer_key API token), moved to $lib/server/session so both proxies
share it.

The whole endpoint is gated, not only the upload paths: its only
callers are the trail editor's route planner and loop generator, the
editor's normal BRouter request already goes through the params
upload path, and no public trail page routes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKLnjYyjFp8QGWgFFq
ci: re-run on the new CI VM spl-vm06 (instance-scope check)
All checks were successful
CI / web (push) Successful in 1m46s
CI / go (push) Successful in 11m49s
9b3bc183fa
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKLnjYyjFp8QGWgFFq
jmz merged commit 9b3bc183fa into deploy 2026-11-01 09:14:04 +00:00
jmz deleted branch fix/brouter-auth 2026-11-01 09:14:05 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/wanderer!43
No description provided.