Skip to content

Phase B1: read the __Host- session cookie - #499

Merged
neuromechanist merged 6 commits into
stagingfrom
feature/issue-2015-host-cookie-reader
Nov 4, 2026
Merged

neuromechanist merged 6 commits into
stagingfrom
feature/issue-2015-host-cookie-reader

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The website half of nemarOrg/nemar-cli#2015 (Phase B1 of the OIDC provider epic, nemarOrg/nemar-cli#2014). The API is about to issue the app session cookie as __Host-nemar_session (host-only, no Domain) behind a flag, and keeps reading the legacy nemar_session. This PR teaches the website to read both, so the API's flag can be turned on without signing anyone out.

What changed

  • src/lib/auth.ts: SESSION_COOKIE_NAME is now __Host-nemar_session, LEGACY_SESSION_COOKIE_NAME is nemar_session, and one new readSessionCookie(cookies) returns the cookie and the name it was read under. The new name wins; the legacy one is used only when the new one is absent, and an empty new cookie is not rescued by a legacy one (the API's rule). The reader asks cookies.has for the new name, because Astro's cookies.get answers undefined for an empty value and would otherwise fall through to the legacy cookie.
  • src/middleware.ts: the presence check before the cross-host redirect, applySession, and the forwarded-cookie fallback all go through readSessionCookie. The fallback builds <name>=<value> with the name it actually read.
  • src/pages/api/auth/logout.ts: the defensive clear (when the API is unreachable or sends no Set-Cookie) now clears both names.
  • Comments in src/lib/auth-proxy.ts and src/pages/api/v1/[...path].ts say the cookie is host-only under either name. The proxies still mirror the API's Set-Cookie unchanged, and auth-dev.ts keeps the legacy name (the dev mock is local).

Tests

New tests name the cookie literally (not through the constant), use a fake jar that mirrors Astro (has is true for an empty cookie, get is undefined for it) and also run against Astro's real AstroCookies built from a raw header. src/lib/auth.test.ts covers readSessionCookie. src/middleware.test.ts covers presence (both names, neither, an unrelated cookie, an empty new cookie beside a legacy one), the Cookie header forwarded to /auth/me, and the fallback name. test/routes/api-auth-logout.test.ts covers the defensive clear and that a mirrored API Set-Cookie is passed through untouched. Each was shown to fail by mutating the line it guards (swapped read order, rescuing an empty new cookie, ignoring either name, a hardcoded fallback name, clearing only one name on sign-out).

Gates

  • bun run lint: Checked 400 files in 513ms. No fixes applied.
  • bun run typecheck: Result (458 files): 0 errors, 0 warnings, 14 hints
  • bun run test: Test Files 161 passed | 2 skipped (163), Tests 4422 passed | 10 skipped (4432)

Order of release

This PR must be merged to staging and released before the API's HOST_PREFIXED_SESSION_COOKIE is set anywhere that serves this site (the nemar-cli PR sets it in the dev Worker's vars only, so it takes effect when that epic reaches dev). Until then the website only ever sees the legacy name, and behaves as before. Production's flag is the owner's, after both releases.

Not merged by the implementer: merging into staging needs the owner's approval.

Round 2

The same principle as the API (nemar-cli ADR 0119 rule 10): every cookie that authenticates or links a session is host-only and __Host- prefixed. The pending-signup page (complete.astro) now checks presence of the pending cookie under either name through one helper, hasOrcidPendingCookie, so the page does not bounce a signup that was in flight when the API moved the cookie, and a wiring test pins that the page asks the helper. That is all it buys: the API reads the pending cookie only under the new name, so such a signup fails once at finalize with orcid_pending_expired after the API deploys, and the person signs in with ORCID again. readSessionCookie also takes the raw Cookie header, so a header that carries more than one __Host-nemar_session is no session, as for the API (a cookie jar keeps only the first value of a repeated name). The tests for both use Astro's real AstroCookies.

Gates after round 2: bun run lint Checked 400 files in 269ms. No fixes applied.; bun run typecheck - 0 errors; bun run test Test Files 162 passed | 2 skipped (164), Tests 4432 passed | 10 skipped (4442).

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

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.

1 participant