Skip to content

feat(security): log each token's client and optionally require DPoP for the laptop client - #53

Merged
KFilippopolitis merged 4 commits into
masterfrom
fix/jupyter-signin-security-b1-b2
Nov 9, 2026
Merged

KFilippopolitis merged 4 commits into
masterfrom
fix/jupyter-signin-security-b1-b2

Conversation

@KFilippopolitis

Copy link
Copy Markdown
Contributor

Implements B1 and B2 from the jupyter sign-in security plan. The Must items need no backend change (M3 is the KEYCLOAK_REQUIRED_AUDIENCE chart setting).

B1 — Log the client of each token (S6)

  • AuthenticationEventListener.handleAuthenticationSuccess appends client=<azp> to the existing login line for JwtAuthenticationToken (portal sessions log as before). Never the token, sid or jti.
  • Verified the premise with a chain test: every Bearer authentication publishes an AuthenticationSuccessEvent (via the repo's DefaultAuthenticationEventPublisher), so the line is per-request. Kept at info — moving per-request lines to debug is still an open operator decision.

B2 — Accept and require DPoP for the laptop client (S1, backend side)

Behind authentication.cli_require_dpop (env KEYCLOAK_CLI_REQUIRE_DPOP, default false), so this can ship before Keycloak (K7) and the mip client (J4) switch:

  • Spring Security 7.1's resource-server DPoP support handles Authorization: DPoP <token> + a DPoP proof header and verifies the proof: signature, htm/htu against this request, iat, jti replay (in-memory cache, per replica), and the access token's cnf.jkt against the proof's key.
  • New cliDpopValidator bean (Boot composes all OAuth2TokenValidator<Jwt> beans onto the issuer-uri decoder): with the flag on, a laptop-client (azp) token must carry cnf.jkt and arrive with the DPoP scheme; a DPoP-bound token used over Bearer is refused. Platform-client tokens (portal, notebooks) keep working over Bearer. Scheme detection reads the request from RequestContextHolder (Boot's RequestContextFilter runs before the security chain) and fails closed without a request scope.
  • CSRF: Authorization: DPoP requests are exempt like Bearer ones (an Authorization header is never cookie-authenticated).

Companion changes needed before enabling the flag (child repos untouched)

  • platform-ui: nginx.conf.template does not send X-Forwarded-Proto/X-Forwarded-Host for the backend location. The DPoP proof's htu is the request URL, so without those headers an https proof never matches what the backend sees. The backend already uses server.forward-headers-strategy: native.
  • mip/deployment: add the KEYCLOAK_CLI_REQUIRE_DPOP knob.
  • Assumption to verify against a real DPoP-issuing Keycloak: DPoP-bound access tokens keep the payload typ: "Bearer" claim (only the JOSE header and response token_type change). If Keycloak changes the claim, accessTokenValidator needs a one-line tweak.

Tests

41 new/updated tests; full suite 180 green, mvn verify green (no committed Maven wrapper, so plain mvn):

  • DpopSecurityChainTest drives the real filter chain with a signed throwaway-key proof: a DPoP POST reaches the controller (CSRF does not block), the same proof replayed is 401, a bound token over Bearer is 401, an unbound laptop token is refused, a platform token over Bearer is unaffected.
  • KeycloakClientValidatorTest: DPoP validator cases (no binding refused, DPoP scheme accepted, Bearer refused, platform token accepted, flag off admits everything, no request scope fails closed).
  • AuthenticationEventListenerTest and TermsOfUseSecurityChainTest: client suffix, success event per Bearer request, DPoP-scheme POST exempt from CSRF.

Rollback / config impact

Revert the two commits or set KEYCLOAK_CLI_REQUIRE_DPOP=false (default): with the flag off the only behaviour change is the client=<azp> suffix in the login log line. No migration, no API change. Remove the flag once every federation has switched.

Every Bearer authentication already fires a success event (ProviderManager's
DefaultAuthenticationEventPublisher), so each API request logs a login line at
info with no way to tell the portal from the laptop client apart. A
JwtAuthenticationToken now appends client=<azp> to that line; nothing about the
token, sid or jti is logged. Portal sessions (OIDC login) log as before.

The chain test now records success events and proves the premise: one event
per Bearer request, carrying the subject and never the token value.
With KEYCLOAK_CLI_REQUIRE_DPOP on, Spring's resource-server DPoP support
handles "Authorization: DPoP <token>" plus a DPoP proof header and verifies
the proof (signature, htu/htm, iat, jti replay in an in-memory cache, and the
token's cnf.jkt against the proof's key). A new validator refuses a
laptop-client token without the cnf binding or one arriving over Bearer;
platform-client tokens (portal, notebooks) keep working over Bearer. DPoP
requests are exempt from CSRF like Bearer ones: an Authorization header is
never cookie-authenticated.

htu is the request URL, so nginx must pass X-Forwarded-Proto/Host for the
backend location (platform-ui's nginx.conf.template does not yet); the native
forward-headers strategy already reads them.

The flag lets this ship before Keycloak and the mip client switch; remove it
once every federation has.
Review follow-ups on PR #53:

- Drop the unused authentication.cli_client_id field; both validator beans
  take the value as a parameter.
- accessTokenValidator now also accepts typ "DPoP" when the token carries
  cnf.jkt: Keycloak may name DPoP-bound access tokens that way (RFC 9449),
  and the binding claim is what keeps an ID token out. Still refuses typ
  "ID" and unbound DPoP-typed tokens, with tests.
- Document the DPoP turn-on order (Keycloak, flag and client release in one
  change window) and that the proof replay cache is per backend replica.
- The Bearer refusal is now tested with the request actually in scope: the
  unit test sets an Authorization: Bearer header on a bound laptop token and
  the chain test puts its Bearer request into RequestContextHolder, so the
  tests exercise the scheme check, not the fail-closed fallback.
- The CSRF exemption is scheme-plus-space for both schemes ("DPoP " like
  "Bearer "), so a header that names no token stays CSRF-protected; tested.
- The chain test gains the two turn-on footguns: a proof naming another URL
  (missing nginx forwarded headers) and a DPoP request without a proof are
  both 401.
- The session doc names ath (SHA-256 of the access token) among the proof
  claims Spring requires, so a client without it can be diagnosed.
@KFilippopolitis
KFilippopolitis merged commit cc0ed37 into master Nov 9, 2026
14 checks passed
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