Skip to content

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

Closed

Description

@neuromechanist

Background

nemar-cli#603 (closes #572, #595) makes the backend accept the nemar_session cookie on /datasets/*, /admin/*, etc. The website's SSR dashboard call (dashboard.astro:32 listMyDatasets({..., cookieHeader})) will work after that PR ships because the worker explicitly forwards the cookie.

But the cookie is intentionally scoped Domain=app.nemar.org (host-only against siblings like data.nemar.org, api.nemar.org, docs.nemar.org). That means browser-side fetches from dashboard JS to api.nemar.org will NOT include the cookie, even with credentials: "include". Per RFC 6265, the cookie's Domain attribute confines it to app.nemar.org and its subdomains; api.nemar.org is not a subdomain.

Broadening the cookie to Domain=nemar.org would fix this trivially but reintroduces the "cookie leaks to every nemar.org subdomain" problem we explicitly designed away. The right fix is a same-origin proxy.

Affected callsites

Browser-side imports of the cookie-auth API clients:

  • dashboard.astro:262 <script> — deleteDraftDataset, requestPublication
  • InviteCollaboratorForm.astro:97 <script> — inviteCollaborator
  • admin/publication-requests.astro:210 <script> — approvePublicationRequest, denyPublicationRequest
  • UploadDropzone.astro:94 <script> — upload routines
  • 11 total apiBase() callsites across dashboard-api.ts, admin-api.ts, collaborators-api.ts, upload-client.ts

Design

Same-origin proxy at src/pages/api/v1/[...path].ts (Astro catch-all route):

```
Browser → app.nemar.org/api/v1/datasets?mine=true (cookie attaches; same-origin)
Worker → api.nemar.org/datasets?mine=true (Cookie header forwarded server-side)
```

The catch-all:

  • Accepts any HTTP method (GET/POST/PUT/DELETE/PATCH)
  • Forwards the raw body, query string, and the Cookie / Origin / Content-Type / Accept headers
  • Copies upstream status code, Content-Type, Cache-Control, and any Set-Cookie back to the browser
  • Refuses any path that would let a caller redirect the proxy off api.nemar.org (no .., no :// in the captured path)

Refactor in lib clients: each function already takes an optional init.cookieHeader. New rule:

```ts
const base = init.cookieHeader ? apiBase() : "/api/v1";
const url = ${base}/datasets?${params.toString()};
```

SSR keeps apiBase() direct (worker → backend, one hop). Browser uses /api/v1 (browser → worker → backend, same-origin from browser POV).

No changes to src/lib/api.ts and src/lib/data-api.ts — those serve public/anonymous reads from the browser and benefit from CDN caching at api.nemar.org / data.nemar.org directly.

Acceptance

  • src/pages/api/v1/[...path].ts catch-all proxy exists and forwards GET/POST/PUT/DELETE/PATCH.
  • All 4 cookie-auth client libs use /api/v1 when cookieHeader is absent.
  • Unit tests cover URL building, path-traversal rejection, and method passthrough.
  • After deploy: clicking "Delete draft" on the dashboard succeeds (200) instead of 401-ing.
  • Cookie Domain=app.nemar.org stays narrow — verified via curl -i https://api.nemar.org/auth/logout -H "Origin: https://app.nemar.org" after a session is issued.

Out of scope

  • Changing the cookie domain (deliberately staying host-only on app.nemar.org).
  • Migrating public reads to the proxy (they should remain direct for CDN caching).

Activity

  1. added a commit that references this issue on May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions