Skip to content

fix(auth): give the web-UI cookies the __Host- prefix and refuse duplicates - #3338

Open
angela-helios wants to merge 2 commits into
mainfrom
fix/3181-host-prefixed-session-cookies
Open

angela-helios wants to merge 2 commits into
mainfrom
fix/3181-host-prefixed-session-cookies

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3181

Behaviour change to review

  • With HFS_UI_LOGIN_COOKIE_SECURE=true (the default) the web-UI cookies are renamed to __Host-hfs_session and __Host-hfs_login. Existing sessions are not carried over: every signed-in user signs in again once after the upgrade. The old name is deliberately not read as a fallback, because a sibling subdomain could plant it for a browser that has no prefixed cookie yet.
  • With HFS_UI_LOGIN_COOKIE_SECURE=false (plain-HTTP local development) the names stay hfs_session and hfs_login, since a browser rejects a __Host- cookie that is not Secure. That mode keeps the weakness and the README says so.
  • A request that carries two session cookies, or two pending-login cookies, of the same name is treated as carrying none: the UI redirects to /ui/login, the FHIR API answers 401, and /ui/callback answers 400.
  • Anything outside this repository that matches the cookie by name (a proxy rule, an external test harness) needs the new name.

Summary

The session and pending-login cookies had no __Host- prefix, and cookie_value returned the first cookie of a given name. A page on a sibling subdomain could set a same-named cookie for the parent domain with a longer Path and have its value win, putting the victim in the attacker's session or completing a login the attacker started.

  • LoginConfig::session_cookie() and LoginConfig::pending_cookie() choose the name from cookie_secure. The Set-Cookie attributes already met the prefix rules (Path=/, no Domain, Secure).
  • The REST auth middleware (session_bearer) and the UI login module (gate, login, callback, logout, session_authorization) take the name from the store's config instead of a constant.
  • helios_auth::cookie_value returns None when the name appears more than once, in one Cookie header or across several.
  • Docs: crates/auth/README.md, the Okta page of the book, and the work-with-auth / work-with-ui skills (both mirrors).

No environment variable is added or changed. AuthConfig::from_env is not touched.

Tests

New:

  • helios-auth unit tests: a name sent twice is refused (same header, separate headers, equal values); hfs_session and __Host-hfs_session are not confused with each other; the names carry the prefix only when cookie_secure is on.
  • helios-rest ui_session_bearer: the unprefixed name is not a session when the cookie is Secure; an unprefixed cookie beside the real one does not displace it; two session cookies give 401.
  • helios-ui login_http: the unprefixed cookie does not pass the gate; two session cookies are signed out; a callback with two pending cookies is rejected; the pending cookie has Path=/ and no Domain.
  • helios-ui unit test for the Set-Cookie string in both modes.

Existing tests in those files now send the prefixed name, since their stores are configured with cookie_secure: true.

Results:

  • cargo test -p helios-auth --lib: 120 passed.
  • cargo test -p helios-rest --test ui_session_bearer: 11 passed.
  • cargo test -p helios-ui --lib: 385 passed. --test login_http: 25 passed.
  • I did not check the cookie in a real browser or run the Playwright suite locally; CI runs it.
  • I did not run clippy locally; CI runs it.

These ran on a branch that also carried seven other pending fixes, so the suites were verified in combination, not on this branch alone. That branch also compiled with the postgres, mongodb, s3 and elasticsearch features.

…icates

The session and pending-login cookies could be replaced by a page on a
sibling subdomain setting a same-named cookie for the parent domain.

When the cookie is Secure it is now named __Host-hfs_session /
__Host-hfs_login, which a browser accepts only from the host itself.
Plain-HTTP local development (HFS_UI_LOGIN_COOKIE_SECURE=false) keeps
the unprefixed names, because a browser rejects __Host- without Secure.
A request carrying the same cookie name twice is treated as carrying
none.

Existing sessions are not carried over: users sign in again once after
the upgrade.

Closes #3181
@claude

claude Bot commented Dec 1, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
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.

Web-UI session and login cookies have no __Host- prefix, so a sibling subdomain can replace them (cookie tossing)

2 participants