Repository navigation
fix(auth): __Host- the Docmost session cookie to stop sibling-origin shadowing (cccvu/wiki-v2#310) - #42
Merged
Conversation
…shadowing (cccvu/wiki-v2#310) With the wiki now under vanderbilt.edu, any same-site *.vanderbilt.edu origin could plant `authToken=<its JWT>; Domain=vanderbilt.edu; Path=/api`, which the browser sends first (longer Path) and @fastify/cookie keeps — the server cannot distinguish it (the Cookie header carries no Path/Domain), so the JWT strategy and the socket.io gateway would run the victim's traffic as the attacker (login-CSRF). Fix: route the session cookie through one CCC helper (authz/session-cookie/) that in production names it `__Host-authToken` + Secure + host-only + Path=/ — browser-enforced un-shadowable by a sibling origin. The posture is gated on NODE_ENV (the repo's security authority), NOT isHttps()/APP_URL, and a boot validator refuses to start on any incoherent posture (fail-fast, not a silent runtime downgrade). Readers (jwt.strategy, ws.gateway) read ONLY the resolved name — no un-prefixed fallback that would reopen the vuln. Logout evicts the legacy cookie; the SPA self-heals a stale-cookie 401 by re-minting once (seamless migration). The fail-closed native-session route scanner is widened + a two-directional classifier self-test. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…at the platform (docmost#310) CI's fork boot smokes caught that the NODE_ENV-gated posture + boot validator refused to boot legitimate http deployments: the documented standalone self-host (native, NODE_ENV=production required for migrations/start:prod, APP_URL=http:// localhost:3000) and both fork CI smokes (the contract smoke boots a remote fork over http and carries a plain cookie across its native->remote flip). Upstream Docmost derives cookie Secure from isHttps() precisely so http self-hosting works. - Posture now gated on isHttps() (restores upstream's `secure: isHttps()` and adds the __Host- prefix over https). Name + secure share the one isHttps() authority, so prefix<=>Secure still holds by construction. Over http -> plain authToken. - Remove the fork boot validator (validate-cookie-posture.ts + session-cookie.module.ts + app.module seam #4 addition): a fork boot check cannot tell the CCC https deployment from a legitimate http self-host/CI config (all are NODE_ENV=production). - Fail-closed protection for the CCC deployment moves to the PLATFORM (super repo): it authoritatively knows it is production and observes the fork's real Set-Cookie, so it rejects a non-__Host- fork session cookie in production posture. - Reword seam comments + service-bridge OpenAPI ("in production" -> "over https"). Fork server jest (session-cookie/route-guard/ws/service-bridge, 33 suites) green; tsc clean. Co-Authored-By: Claude Opus 4.8 <[email protected]>
… review) pr-review-comprehensive Testing lens (L3): the self-heal `catch` in api-client.ts (openDocmostSession rejects → fall to the login wall, no retry) was untested — deleting the try/catch left every test green. Add a case that rejects the re-mint and asserts one attempt, no retry, and the /login redirect (pins the catch path). Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.
TL;DR
__Host--harden the Docmost session cookie so a same-site sibling origin can no longer shadow it. Fork half of cccvu/wiki-v2#310 (login-CSRF). Pairs with the super-repo PR — merge this first, then re-pin the gitlink.Why
With the wiki now sharing a registrable parent domain with sibling institutional sites, any same-site sibling origin (via XSS on / control of it) could set
authToken=<its JWT>; Domain=<parent>; Path=/api. The browser sends the longer-Pathcookie first,@fastify/cookiekeeps the first value, and both readers (jwt.strategy,ws.gateway) prefer the cookie — so the victim's/api+ socket.io traffic runs as the attacker. The server cannot tell the two apart (theCookie:header carries no Path/Domain), so the only robust fix is the browser-enforced__Host-prefix (Secure + host-only + Path=/), mirroring the platform's immune__Host-wiki_session.What changed
apps/server/src/authz/session-cookie/docmost-auth-cookie.ts— the single source of truth for the session cookie name/attributes. Over an https edge the name is__Host-authToken+Secure+ host-only +Path=/; over http it is the un-prefixedauthToken,secure:false. Name andsecureshare ONE authority (isHttps()viauseHostPrefixedCookie, matching upstream'ssecure: isHttps()), soprefix ⟺ Secureholds by construction. Readers usereadDocmostAuthCookiewhich reads only the resolved name (no un-prefixed fallback over https — that would reopen the vuln). Logout evicts the legacy un-prefixed cookie.NODE_ENV; CI's fork boot smokes showed it refuses to boot the documented standalone http self-host + both smokes, and a fork check can't tell the CCC https deployment from a legitimate http config — all areNODE_ENV=production.) Instead the platform (super PR cccvu/wiki-v2#428), which authoritatively knows it is production and observes the fork's real Set-Cookie at mint, rejects a non-__Host-fork session cookie in production posture. See the Implementation plan comment on cccvu/wiki-v2#310.auth.controller(set + logout clear),workspace.controller(invite-set + hostname-clear),jwt.strategy(read via helper, bearer fallback kept),ws.gateway(injectEnvironmentService+ read via helper). Fork-owned:service-bridgeset via helper;static-route-scannative-session-mint regex widened + exported;native-credential-routes.specgains a two-directional classifier self-test;import-boundary.specSEAM_ALLOWLIST +=jwt.strategy,ws.gateway.apps/client/src/lib/api-client.ts— on a remote-mode authed 401, re-mint the Docmost session ONCE and retry before/login(loop-guarded, dynamic import to avoid a cycle). Makes the cookie rename a seamless migration instead of a mass forced re-login.docs/integrations/authorization/service-bridge.openapi.jsonSet-Cookie description.How it works
The
__Host-prefix is a browser guarantee: a UA refuses to store a__Host--named cookie unless it is Secure, has noDomain, andPath=/. So a sibling origin cannot set__Host-authTokenfor the wiki host, and the readers only ever read that name. The collaborative editor WS (/collab) authenticates with a bearer collab-token (not the cookie) and is unaffected. Full rationale, tradeoffs and the fork-first merge order are in the## Implementation plancomment on cccvu/wiki-v2#310.Testing
authz/session-cookie/*+ widenedauthz/route-guard/*specs green (32); adjacentcore/auth/ws/service-bridge/authz/mode/core/workspacegreen. (Pre-existingtoken.service/auth.service/auth.controller/workspace.service"should be defined" /.tsx-email-template suite-load failures are unrelated — verified identical on the base.)lib/api-client.test.ts(4) green; full suite 313 green.tsc -p tsconfig.build.json --noEmitand clienttsc --noEmitboth clean.Risk & rollback
Hard cutover (no dual-read): existing un-prefixed
authTokencookies stop authenticating at deploy; the browser self-heal + platform relay re-open make it seamless (worst case one re-login). The platform__Host-wiki_sessionidentity is unaffected. Rollback = revert this PR (the cookie reverts to the un-prefixed name; sessions re-establish). Fail-closed for the CCC deployment: if the fork'sAPP_URLwere misconfigured to http in production it would mint a plainauthToken, and the platform rejects that session at mint (super PR docmost#428) — login fails loudly rather than shipping a shadowable cookie (prod ecs.tf already setsAPP_URL=https).Checklist
UPSTREAM_MODIFICATIONS.md(super PR); fork import-boundary SEAM_ALLOWLIST updated in lockstep🤖 Generated with Claude Code