Skip to content

fix(auth): __Host- the Docmost session cookie to stop sibling-origin shadowing (cccvu/wiki-v2#310) - #42

Merged
PMQ9 merged 4 commits into
wiki-v2from
fix/310-host-authtoken-cookie
Sep 20, 2026
Merged

PMQ9 merged 4 commits into
wiki-v2from
fix/310-host-authtoken-cookie

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 19, 2026 •

Copy link
Copy Markdown

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-Path cookie first, @fastify/cookie keeps 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 (the Cookie: 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

  • New CCC helper 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-prefixed authToken, secure:false. Name and secure share ONE authority (isHttps() via useHostPrefixedCookie, matching upstream's secure: isHttps()), so prefix ⟺ Secure holds by construction. Readers use readDocmostAuthCookie which reads only the resolved name (no un-prefixed fallback over https — that would reopen the vuln). Logout evicts the legacy un-prefixed cookie.
  • Fail-closed enforcement lives at the platform, not fork boot. (An earlier draft added a fork boot validator gating on 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 are NODE_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.
  • Upstream seams (policy stays in the helper): auth.controller (set + logout clear), workspace.controller (invite-set + hostname-clear), jwt.strategy (read via helper, bearer fallback kept), ws.gateway (inject EnvironmentService + read via helper). Fork-owned: service-bridge set via helper; static-route-scan native-session-mint regex widened + exported; native-credential-routes.spec gains a two-directional classifier self-test; import-boundary.spec SEAM_ALLOWLIST += jwt.strategy, ws.gateway.
  • Browser self-heal 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 — docs/integrations/authorization/service-bridge.openapi.json Set-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 no Domain, and Path=/. So a sibling origin cannot set __Host-authToken for 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 plan comment on cccvu/wiki-v2#310.

Testing

  • Server jest: new authz/session-cookie/* + widened authz/route-guard/* specs green (32); adjacent core/auth / ws / service-bridge / authz/mode / core/workspace green. (Pre-existing token.service/auth.service/auth.controller/workspace.service "should be defined" / .tsx-email-template suite-load failures are unrelated — verified identical on the base.)
  • Client vitest: new lib/api-client.test.ts (4) green; full suite 313 green.
  • Typecheck: server tsc -p tsconfig.build.json --noEmit and client tsc --noEmit both clean.
  • Fitness: fork import-boundary + native-credential-routes green.

Risk & rollback

Hard cutover (no dual-read): existing un-prefixed authToken cookies stop authenticating at deploy; the browser self-heal + platform relay re-open make it seamless (worst case one re-login). The platform __Host-wiki_session identity 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's APP_URL were misconfigured to http in production it would mint a plain authToken, 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 sets APP_URL=https).

Checklist

  • Scoped to one logical concern (session-cookie hardening + its seamless migration)
  • New/changed behavior has tests (unit + regression + fitness)
  • Existing tests pass
  • Upstream boundary respected — every divergent upstream file documented in UPSTREAM_MODIFICATIONS.md (super PR); fork import-boundary SEAM_ALLOWLIST updated in lockstep
  • No brand/institution strings added to the public fork (boundary brand guard green)

🤖 Generated with Claude Code

PMQ9 and others added 4 commits September 19, 2026 11:11
…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]>
@PMQ9
PMQ9 merged commit 1767bad into wiki-v2 Sep 20, 2026
@PMQ9
PMQ9 deleted the fix/310-host-authtoken-cookie branch September 20, 2026 00:29
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