Repository navigation
fix(security)!: harden tokens, sessions and experiment access - #52
Merged
Merged
Conversation
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
force-pushed
the
fix/owasp-token-session-hardening
branch
from
November 6, 2026 17:12
7da1910 to
6baeb00
Compare
| // 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); |
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.
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/logoutnow needs its CSRF token.Changes
typ=Bearer, so ID tokens are refused.KEYCLOAK_REQUIRED_AUDIENCEoptionally also requiresaud; it is off by default.SameSite=Lax, back-channel logout ending all of a user's portal sessions,Clear-Site-Datafor 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/logoutexcept for the backend's own localhost call.GET /activeUser/tokenis removed. Swagger and/v3/api-docsare off unlessSPRINGDOC_ENABLEDis set, and only/actuator/healthstays public. Login redirects take their host from config only. 500 responses no longer carry exception text.AUTHENTICATION=0refuses to start with a non-local portal URL.EXAFLOW_API_TOKENas a Bearer token when set.API behaviour changes
UnauthorizedException)GET /activeUser/tokentyp=BearerPOST /logoutfrom a browser without CSRF tokenGET /v3/api-docs,/swagger-ui,/actuatoranalysis: nullfor its owner; hidden from othersLists 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. MigrationV5__ExperimentDatasetsReadableFunction.sqladds the database functionexperiment_datasets_readable; no table changes.SESSION_MAX_AGEaccepts8horPT8H. Session lifetimes are documented indocs/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.6container: 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
typcheck was confirmed on Keycloak 26 tokens only. If an EBRAINS token lacks the claim, every API call gets 401.subject_idvalues no longer match Keycloak (for example after a realm rebuild), those users get 403. Clearingsubject_idreleases a user./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.AUTHENTICATION=0, aPLATFORM_UI_BASE_URLwithout a scheme, such aslocalhost:4200, now refuses to start.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_idvalues filled in by the new check stay and are harmless.