Skip to content

fix(auth): refuse the UI session cookie on reads from another origin - #3375

Open
angela-helios wants to merge 3 commits into
mainfrom
fix/3182-session-cookie-sibling-reads
Open

angela-helios wants to merge 3 commits into
mainfrom
fix/3182-session-cookie-sibling-reads

Conversation

@angela-helios

Copy link
Copy Markdown
Contributor

Closes #3182

This branch is stacked on the PR for #3183 (PR #3365). Its diff includes that commit until #3365 merges.

Behaviour change to review

  • The web-UI session cookie now authorizes a FHIR API request only when the request comes from this origin, on every method. Before, a GET or HEAD marked Sec-Fetch-Site: same-site (a page on a sibling subdomain) was authenticated as the signed-in user. It is now answered 401, logged and audited as a cross-origin refusal, the same as a write from another origin.
  • This includes a top-level link from a sibling subdomain straight to a FHIR API URL. It used to show the resource as the signed-in user; it now answers 401, as a link from an unrelated site already did. Typing the URL, or following a link from the HFS UI itself, is unchanged.
  • Under /ui, a GET or HEAD that carries the session cookie from another origin is answered 403 unless it is a navigation (Sec-Fetch-Mode: navigate). A link from another site to a UI page still opens it. The login routes and /ui/assets/ are not affected.
  • Requests with an explicit Authorization header, requests without the session cookie, and non-browser clients are not affected. The UI's own pages are not affected: their browser calls are same-origin and their server-side calls send an explicit bearer.
  • A refused read is now written to the log and the audit trail, so a page polling from a sibling origin adds audit events.

Summary

  • session_bearer in crates/rest/src/middleware/auth.rs now refuses the cookie whenever helios_auth::is_cross_origin says the request came from another origin, instead of only for cross-site requests and foreign writes. Sec-Fetch-Site must be same-origin or none; without that header, Origin must be absent or name the request's Host or HFS_BASE_URL.
  • A refused request does not refresh the session's token or move its idle clock, so a sibling page can no longer keep a session alive through the API.
  • The /ui guard (crates/ui/src/cross_site.rs, renamed refuse_cross_site_requests) also refuses a non-navigation read that carries the session cookie from another origin, before the session gate runs. It applies only when a login is installed.
  • crates/auth/README.md and the work-with-auth skill (both mirrors) describe the rule.

Not covered:

  • A browser on plain HTTP to a non-loopback host sends no Sec-Fetch-Site and no Origin on an <img> or <script> read, so that read cannot be told apart from the page's own and still rides the cookie.
  • A sibling page can still open a /ui page as a navigation, including in a frame.
  • The GET routes were not audited for handlers that change state.

Tests

  • New crates/rest/tests/ui_session_sibling_reads.rs: a same-site read, a same-site navigation and a read naming another Origin are 401 and leave the session's last_seen unchanged; reads from this origin (same-origin, none, no browser headers, the public origin behind a proxy that rewrites Host) still authenticate; an explicit bearer is validated whatever the origin.
  • New crates/ui/tests/cross_site_reads_http.rs: a sub-resource read from a sibling site and a read naming another Origin are 403 and leave the session alone; a navigation from a sibling site, the page's own reads and an asset request are not refused; a sibling read without the cookie is sent to the login.
  • New unit tests in crates/ui/src/cross_site.rs for is_foreign_session_read.
  • crates/rest/tests/ui_session_bearer.rs: removed the assertion that a same-site GET rides the cookie.
  • Existing ui_session_origin_audit, login_http and bulk_import_http suites.

Results:

  • cargo test -p helios-rest --lib: 1131 passed.
  • cargo test -p helios-rest --test ui_session_sibling_reads: 5 passed. --test ui_session_bearer: 8 passed. --test ui_session_origin_audit: 6 passed.
  • cargo test -p helios-ui --lib: 396 passed. --test cross_site_reads_http: 6 passed. --test login_http: 26 passed. --test bulk_import_http: 54 passed.
  • cargo test -p helios-hfs --bin hfs env_names: 7 passed, so the binary builds.
  • I did not run clippy locally; CI runs it.

These ran on a branch that also carried other pending fixes, so the suites were verified in combination, not on this branch alone.

…usal

A request whose web-UI session cookie was refused because the request was
cross-site, or a write from another origin, was audited on the FHIR API as
a missing token with no user, and not recorded at all on /ui.

The REST auth middleware now tells that case apart: it logs a warning,
records an AuditEvent described as a cross-origin refusal that carries the
Origin and Sec-Fetch-Site values and names the session subject, and says
so in the 401 body. The /ui guard logs the refusal and reports it to a
recorder the hfs binary installs, which writes the same AuditEvent.

Which requests are refused does not change.

Closes #3183
A GET or HEAD sent by a page on a sibling subdomain still ran as the
signed-in web-UI user: the session cookie is SameSite=Lax, and only
cross-site requests and writes from another origin were refused. Such a
read could keep the session alive, probe for data and run expensive
searches as the user.

The REST auth middleware now lets the cookie stand in for a bearer only
on a request from this origin, on every method. A refused read is
answered 401, logged and audited as a cross-origin refusal, and does not
touch the session.

Under /ui, a read that carries the session cookie from another origin is
answered 403 unless it is a navigation, so a link from another site to a
UI page still opens it.

Closes #3182
@claude

claude Bot commented Dec 1, 2026

Copy link
Copy Markdown

Code review

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

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.

UI session cookie still authorizes reads sent from a sibling subdomain

2 participants