Skip to content

fix(security)!: harden tokens, sessions and experiment access - #52

Merged
KFilippopolitis merged 14 commits into
masterfrom
fix/owasp-token-session-hardening
Nov 7, 2026
Merged

KFilippopolitis merged 14 commits into
masterfrom
fix/owasp-token-session-hardening

Conversation

@KFilippopolitis

@KFilippopolitis KFilippopolitis commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Security hardening from an audit against the OWASP Authentication, Authorization, Session Management and JWT cheat sheets and ASVS 5 (V6, V7, V9).

Deploy together with platform-ui fix/security-headers: the old UI's sign-out fails on this backend, because /logout now needs its CSRF token.

Changes

  • Tokens: Bearer tokens must have typ=Bearer, so ID tokens are refused. KEYCLOAK_REQUIRED_AUDIENCE optionally also requires aud; it is off by default.
  • Sessions: 30-minute idle timeout, 8-hour maximum, SameSite=Lax, back-channel logout ending all of a user's portal sessions, Clear-Site-Data for cache and storage on sign-out (session and hub login cookies are expired by name, because its cookies directive covers the whole registrable domain), and CSRF on /logout except for the backend's own localhost call.
  • Authorization: reading someone else's shared experiment needs every dataset it ran on, and lists hide the rest in the query. A stored user whose Keycloak subject differs from the token's gets 403. Denials return 403 instead of 401.
  • Exposure: GET /activeUser/token is removed. Swagger and /v3/api-docs are off unless SPRINGDOC_ENABLED is set, and only /actuator/health stays public. Login redirects take their host from config only. 500 responses no longer carry exception text. AUTHENTICATION=0 refuses to start with a non-local portal URL.
  • Logging: failed authentication is logged with its source address, never the token; an expired token logs at DEBUG. Authorities and request bodies move to DEBUG.
  • Exaflow: the backend sends EXAFLOW_API_TOKEN as a Bearer token when set.

API behaviour changes

Request Before After
Access denied (UnauthorizedException) 401 403
GET /activeUser/token Access token 404
Bearer token without typ=Bearer Accepted 401
Shared experiment without its datasets Result returned 403; hidden from lists
Stored user with a different subject Accepted 403
POST /logout from a browser without CSRF token Accepted 403
GET /v3/api-docs, /swagger-ui, /actuator Public Login needed; docs off by default
List containing a stored analysis that is not JSON 500 Listed with analysis: null for its owner; hidden from others

Lists filter in the query, so pages and totals count only what the user can read, and an empty result is 204.

Config and migrations

New optional variables: SESSION_IDLE_TIMEOUT, SESSION_MAX_AGE, KEYCLOAK_REQUIRED_AUDIENCE, EXAFLOW_API_TOKEN, SPRINGDOC_ENABLED. Migration V5__ExperimentDatasetsReadableFunction.sql adds the database function experiment_datasets_readable; no table changes. SESSION_MAX_AGE accepts 8h or PT8H. Session lifetimes are documented in docs/context/session-management.md.

Tests

mvn test: 167 pass, and it passes at every commit.

Also run locally against a Keycloak 26 container, Postgres, the built UI image and a stub Exaflow: token types, other clients' tokens, terms of use, CSRF, redirect host, SameSite, sign-out, and back-channel logout with 3 and 9 sessions. This run predates the review fixes, and has not been repeated since.

After the review fixes, on a postgres:18.6 container: V1 to V5 applied through Flyway, the function gave the expected result on 18 inputs, including broken JSON and null datasets, and the Hibernate list query returned only the readable row. These checks are not committed, because the test suite has no Postgres.

All testing was local. Nothing has run against the EBRAINS realm or a real cluster.

Risks

  • The typ check was confirmed on Keycloak 26 tokens only. If an EBRAINS token lacks the claim, every API call gets 401.
  • If stored subject_id values no longer match Keycloak (for example after a realm rebuild), those users get 403. Clearing subject_id releases a user.
  • The back-channel session registry is in memory, so it works with one replica.
  • Signing out at Keycloak in one browser also ends the portal in the user's other browsers.
  • A shared experiment that names no datasets, such as a legacy row, becomes readable only by its owner and the all-experiments role. Count these rows before deploying.
  • The hub cookie names and the /notebook/hub/ path were not confirmed in a browser. If they are wrong, those cookies survive sign-out until the next token refresh. A notebook server's own cookie is not cleared either way.
  • With AUTHENTICATION=0, a PLATFORM_UI_BASE_URL without a scheme, such as localhost:4200, now refuses to start.
  • Expired tokens are recognised by Spring's "Jwt expired at" message, so an upgrade could move them back to WARN.

Finding 16 (the 180-day laptop refresh token) is an accepted risk and out of scope here.

Rollback

Revert the PR. The V5 function can stay, unused. subject_id values filled in by the new check stay and are harmless.

Keycloak signs ID tokens with the same key and the same azp as access tokens,
so an ID token passed the Bearer checks. Require its typ claim to be Bearer
(ASVS 5 9.2.2). KEYCLOAK_REQUIRED_AUDIENCE, off by default, also requires aud.

BREAKING CHANGE: tokens without typ=Bearer are rejected with 401.
GET /activeUser/token handed the session's access token to browser JavaScript,
which undoes the session cookie's HttpOnly. Nothing in platform-ui, mip-jupyter
or the deployment repository calls it.

BREAKING CHANGE: GET /activeUser/token now answers 404.
Access denials returned 401, which the portal reads as a lost session and
answers with a new login. Unexpected errors returned raw exception text to the
client; they now return a generic message and stay in the log.

BREAKING CHANGE: UnauthorizedException responses change from 401 to 403.
Users are stored by username, and subject_id was never compared, so a reused
preferred_username inherited the previous holder's experiments and accepted
terms. Answer 403 with a message naming the conflict, on every route. Rows with
no real subject get it filled in. A renamed user still gets a new row, as before.

BREAKING CHANGE: a stored user whose subject differs from the token's gets 403.
A realm whose subjects changed needs subject_id cleared first.
Failed Bearer and OIDC authentication left no trace, because the default
publisher sends no event for OAuth2 exceptions. Log them with the source address
and exception type, never the token. An expired token is routine for a polling
client, so it logs at DEBUG. Authorities and request bodies move to DEBUG.
Login redirects keep only the request's path. The host comes from
frontend.base-url, and //host and /\host stay on this site.

BREAKING CHANGE: frontend_redirect can no longer choose the host after login.
Swagger and /v3/api-docs are off unless SPRINGDOC_ENABLED is set, and need a
login. Only /actuator/health stays public.

BREAKING CHANGE: /v3/api-docs and /swagger-ui are off by default.
Every endpoint is open with authentication off, so the backend no longer starts
that way unless the portal is local. A URL whose host cannot be read counts as
not local: it may still be public.
…experiment

Any signed-in user who accepted the terms could read a shared experiment's
results without a role on its datasets. Readers other than the owner and holders
of the all-experiments role now need every dataset it ran on.

Lists apply the same rule in the query, through the database function
experiment_datasets_readable: filtering after paging would let totals and the
name and algorithm filters reveal hidden experiments. Datasets live in the
analysis text, which can hold an error message instead of JSON, so the function
treats anything unreadable as no access.

A stored analysis that is not JSON no longer turns a list into a 500: it lists
as no analysis.

BREAKING CHANGE: users without the datasets get 403 on experiments shared with
them and no longer see them in lists. A shared row naming no datasets is readable
only by its owner and the all-experiments role.
Portal sessions end after SESSION_MAX_AGE (8 hours), and idle after
SESSION_IDLE_TIMEOUT (30 minutes), so roles taken at login do not live on. The
session cookie is SameSite=Lax rather than relying on browser defaults.
…ogout

Lets Keycloak end portal sessions; it needs the client's Backchannel Logout URL.
Keycloak's user logout sends one logout token per client, naming one session, so
the registry matches on the subject to end the user's other sessions too. A
token naming only a session ends that session. Login changes the session id, and
the registry only learns of it through HttpSessionEventPublisher.
Only Spring's back-channel logout, which calls /logout from localhost, may skip
the token. A portal must now sign out with a POST carrying the XSRF-TOKEN
cookie's value.

Sign-out sends Clear-Site-Data for cache and storage. It leaves out the cookies
directive, which browsers apply to the whole registrable domain and would sign
the user out of unrelated services; the session and hub login cookies are expired
by name instead.

BREAKING CHANGE: needs the platform-ui sign-out form; the default logout page's
token is not accepted.
Exaflow's API has no authentication, so the backend's dataset checks can be
bypassed by anything that reaches it. Exaflow can now require this token.
ASVS 5 V7.1 asks for this. Tested with a local Keycloak: disabling a user sends
no logout, so administrators must also use Sign out.
@KFilippopolitis
KFilippopolitis force-pushed the fix/owasp-token-session-hardening branch from 7da1910 to 6baeb00 Compare November 6, 2026 17:12
// ponytail: assumes the hub's default base path; a setting if a deployment moves it. A notebook server's own
// cookie is not cleared, because its path holds the username: it ends at the hub's next token refresh.
private static Cookie expiredHubCookie(String name) {
Cookie cookie = new Cookie(name, null);
// ponytail: assumes the hub's default base path; a setting if a deployment moves it. A notebook server's own
// cookie is not cleared, because its path holds the username: it ends at the hub's next token refresh.
private static Cookie expiredHubCookie(String name) {
Cookie cookie = new Cookie(name, null);
@KFilippopolitis
KFilippopolitis merged commit b254290 into master Nov 7, 2026
14 checks passed
@KFilippopolitis
KFilippopolitis deleted the fix/owasp-token-session-hardening branch November 7, 2026 18:08
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.

2 participants