Repository navigation
feat(security): log each token's client and optionally require DPoP for the laptop client - #53
Merged
Merged
Conversation
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.
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.
Implements B1 and B2 from the jupyter sign-in security plan. The Must items need no backend change (M3 is the
KEYCLOAK_REQUIRED_AUDIENCEchart setting).B1 — Log the client of each token (S6)
AuthenticationEventListener.handleAuthenticationSuccessappendsclient=<azp>to the existing login line forJwtAuthenticationToken(portal sessions log as before). Never the token,sidorjti.AuthenticationSuccessEvent(via the repo'sDefaultAuthenticationEventPublisher), so the line is per-request. Kept atinfo— moving per-request lines todebugis still an open operator decision.B2 — Accept and require DPoP for the laptop client (S1, backend side)
Behind
authentication.cli_require_dpop(envKEYCLOAK_CLI_REQUIRE_DPOP, defaultfalse), so this can ship before Keycloak (K7) and the mip client (J4) switch:Authorization: DPoP <token>+ aDPoPproof header and verifies the proof: signature,htm/htuagainst this request,iat,jtireplay (in-memory cache, per replica), and the access token'scnf.jktagainst the proof's key.cliDpopValidatorbean (Boot composes allOAuth2TokenValidator<Jwt>beans onto the issuer-uri decoder): with the flag on, a laptop-client (azp) token must carrycnf.jktand arrive with theDPoPscheme; a DPoP-bound token used overBeareris refused. Platform-client tokens (portal, notebooks) keep working overBearer. Scheme detection reads the request fromRequestContextHolder(Boot'sRequestContextFilterruns before the security chain) and fails closed without a request scope.Authorization: DPoPrequests are exempt likeBearerones (anAuthorizationheader is never cookie-authenticated).Companion changes needed before enabling the flag (child repos untouched)
nginx.conf.templatedoes not sendX-Forwarded-Proto/X-Forwarded-Hostfor the backend location. The DPoP proof'shtuis the request URL, so without those headers an https proof never matches what the backend sees. The backend already usesserver.forward-headers-strategy: native.KEYCLOAK_CLI_REQUIRE_DPOPknob.typ: "Bearer"claim (only the JOSE header and responsetoken_typechange). If Keycloak changes the claim,accessTokenValidatorneeds a one-line tweak.Tests
41 new/updated tests; full suite 180 green,
mvn verifygreen (no committed Maven wrapper, so plainmvn):DpopSecurityChainTestdrives 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 overBeareris 401, an unbound laptop token is refused, a platform token overBeareris 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).AuthenticationEventListenerTestandTermsOfUseSecurityChainTest: 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 theclient=<azp>suffix in the login log line. No migration, no API change. Remove the flag once every federation has switched.