Skip to content

Same-origin /api/v1 proxy for dashboard mutations (#59) - #60

Merged
neuromechanist merged 2 commits into
mainfrom
feature/issue-59-dashboard-proxy
May 23, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
feature/issue-59-dashboard-proxy

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #59. Depends on nemarOrg/nemar-cli#603 (cookie auth on /datasets/*, /admin/*).

Why

The session cookie is deliberately scoped Domain=app.nemar.org (host-only against siblings). Browser-side fetches from dashboard JS to api.nemar.org therefore don't carry the cookie even with credentials: "include" (RFC 6265 forbids it). Broadening to Domain=nemar.org would leak the cookie to every *.nemar.org host and was explicitly designed away.

This PR ships the same-origin proxy as the proper fix.

How

  • New Astro catch-all src/pages/api/v1/[...path].ts forwards anything under /api/v1/... to ${apiBase()}/..., preserving method, body (as arrayBuffer so binary uploads survive), and the Cookie + Origin headers. Rejects path-traversal / scheme-injection attempts before the upstream fetch. Set-Cookie passthrough on the response so sliding sessions from /auth/me keep working if they ever flow through the proxy.

  • New dashboardApiBase(cookieHeader?) helper in src/lib/api-base.ts:

    • SSR (cookieHeader passed) → returns apiBase() (direct one-hop to api.nemar.org with the caller's explicit Cookie header).
    • Browser (no cookieHeader) → returns /api/v1 (same-origin; browser auto-attaches the cookie; the worker forwards it).
  • 4 client libs refactored from apiBase() to dashboardApiBase(init.cookieHeader) (SSR-capable) or dashboardApiBase() (client-only): dashboard-api.ts, admin-api.ts, collaborators-api.ts, upload-client.ts. 11 callsites in total.

  • Public-data clients (api.ts, data-api.ts) deliberately unchanged — those reads are anonymous and edge-cacheable; going through the worker would just add latency.

Test plan

  • bun run typecheck — 0 errors
  • bun run test — 364/364 (+14 new: proxy path-safety, dashboardApiBase split-resolution, plus updated lib tests that now assert the /api/v1/... URL on the browser path)
  • bun run lint — clean
  • After deploy + nemar-cli#603: click "Delete draft" on the dashboard → 200, not 401. Cookie attaches via same-origin.
  • Smoke: curl -i https://app.nemar.org/api/v1/datasets → either 200 (with cookie) or 401 (without) — but not the previous "Origin not allowed" 403 path.
  • Browser DevTools network tab: app.nemar.org/api/v1/datasets?mine=true Request shows Cookie: nemar_session=... while the same fetch to api.nemar.org/datasets?mine=true would not.

Scope notes

  • Cookie domain stays app.nemar.org (no env-var change). Sibling subdomains never see it.
  • Catch-all is path-restricted to /api/v1/...; per-endpoint same-origin routes under /api/auth/* and /api/dataset/* keep their dedicated handlers and win over the catch-all by Astro's most-specific-route resolution.
  • S3 presigned PUT uploads from upload-client.ts::runUploadQueue continue to go client → S3 directly (the presigned URL points at S3, not at our backend); only createDraftDataset and finalizeDataset go through the proxy.

@neuromechanist neuromechanist linked an issue May 23, 2026 that may be closed by this pull request
5 tasks
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 23, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8470bed
Status:🚫  Deploy failed.

View logs

@neuromechanist

Copy link
Copy Markdown
Contributor Author

/review-pr outcomes

Two reviewers ran in parallel: code-reviewer + silent-failure-hunter. Three findings each, five real (some overlapped between reviewers).

Addressed in 8470bed

  • SSR plumbing on 5 browser-only mutation fns (code-reviewer Epic: Redesign nemar.org on Cloudflare Pages + Astro, reusing data.nemar.org #1). requestPublication, deleteDraftDataset, approvePublicationRequest, denyPublicationRequest, inviteCollaborator now accept cookieHeader?: string in their init and thread it through dashboardApiBase(init.cookieHeader) plus a Cookie header on the upstream request. Avoids the worker → /api/v1 → worker loopback if anyone ever calls these from an SSR context (today they're imported only inside <script> blocks, but the type contract is now consistent with the read-side fns).
  • isSafeProxyPath rejects @ and // (code-reviewer Phase 1: Foundation + Discover page #2 + silent-failure Phase 3: Data quality + HED visualization (website side) #4). @ is a userinfo-style fragment some URL parsers reinterpret as authority; // produces malformed upstream URLs like https://api.nemar.org//datasets//. Both are belt-and-braces rejections with no legitimate dashboard path containing them. Two new test cases added.
  • Cache-Control: no-store always on /api/v1/ responses* (code-reviewer Phase 2: Dataset detail page #3). Dropped the upstream-Cache-Control passthrough. Even a misconfigured upstream returning public, max-age=60 on /datasets?mine=true could otherwise let the edge cache replay an authenticated body to a different anonymous visitor. Hardcoded.
  • upstream_unreachable error code mapped to friendly copy (silent-failure Epic: Redesign nemar.org on Cloudflare Pages + Astro, reusing data.nemar.org #1). Dashboard's ERROR_MESSAGES now distinguishes "Can't reach the dataset service" from a backend 502 — the former is the worker → backend hop dying, the latter is the backend itself.

Non-findings explicitly confirmed clean (no action)

  • copySetCookies return-value ignored — correctly so; response is well-formed whether or not cookies are present.
  • DELETE-with-body via arrayBuffer — works correctly; the hasBody check covers it.
  • upload-client.ts no-cookieHeader on dashboardApiBase() — createDraftDataset / finalizeDataset are imported only inside upload.astro's <script> (browser-only), so the /api/v1 resolution is correct. Documented in the file's JSDoc.
  • SSR-test rewrite from https://api.nemar.org/ → /api/v1/ — the bulk replace did not touch any test asserting the cookieHeader-set path; verified listMyDatasets's SSR test (line 213-224 of dashboard-api.test.ts) makes no URL assertion, so it correctly exercises the direct path.

@neuromechanist
neuromechanist merged commit 74275c1 into main May 23, 2026
1 check failed
@neuromechanist
neuromechanist deleted the feature/issue-59-dashboard-proxy branch June 21, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proxy dashboard mutations through /api/v1/* so client-side cookies attach

1 participant