Repository navigation
Phase B1: read the __Host- session cookie - #499
Merged
Merged
Conversation
Deploying nemar-website with
|
| Latest commit: |
d7bef20
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://8270178e.nemar-website.pages.dev |
| Branch Preview URL: | https://feature-issue-2015-host-cook.nemar-website.pages.dev |
This was referenced Nov 4, 2026
This was referenced Nov 4, 2026
This was referenced Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, noDomain) behind a flag, and keeps reading the legacynemar_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_NAMEis now__Host-nemar_session,LEGACY_SESSION_COOKIE_NAMEisnemar_session, and one newreadSessionCookie(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 askscookies.hasfor the new name, because Astro'scookies.getanswers 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 throughreadSessionCookie. 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.src/lib/auth-proxy.tsandsrc/pages/api/v1/[...path].tssay the cookie is host-only under either name. The proxies still mirror the API's Set-Cookie unchanged, andauth-dev.tskeeps 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 (
hasis true for an empty cookie,getis undefined for it) and also run against Astro's realAstroCookiesbuilt from a raw header.src/lib/auth.test.tscoversreadSessionCookie.src/middleware.test.tscovers 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.tscovers 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 hintsbun run test:Test Files 161 passed | 2 skipped (163),Tests 4422 passed | 10 skipped (4432)Order of release
This PR must be merged to
stagingand released before the API'sHOST_PREFIXED_SESSION_COOKIEis 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 reachesdev). 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
stagingneeds 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 withorcid_pending_expiredafter the API deploys, and the person signs in with ORCID again.readSessionCookiealso takes the rawCookieheader, so a header that carries more than one__Host-nemar_sessionis 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 realAstroCookies.Gates after round 2:
bun run lintChecked 400 files in 269ms. No fixes applied.;bun run typecheck- 0 errors;bun run testTest Files 162 passed | 2 skipped (164),Tests 4432 passed | 10 skipped (4442).