Skip to content

Fix 500 from the Keycloak auth manager when a browser session has no Keycloak tokens - #76353

Merged
dabla merged 5 commits into
apache:mainfrom
dabla:fix/keycloak-browser-token-without-cookies
Nov 17, 2026
Merged

dabla merged 5 commits into
apache:mainfrom
dabla:fix/keycloak-browser-token-without-cookies

Conversation

@dabla

@dabla dabla commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Since Airflow 3.3 the browser JWT no longer carries the Keycloak tokens: KeycloakJWTMiddleware reads them from their own cookies. When it cannot complete the session (the Keycloak refresh token expired, or the access token cookie is missing), it clears the Airflow JWT on the response, but the request still reaches the route with that JWT. get_user then falls back to resolve_user_from_token, KeycloakAuthManager.get_user_from_token returns None for a browser token without Keycloak tokens, and the is_authorized_* checks fail on user.access_token: every UI request answers 500 until the user deletes the cookies.

KeycloakJWTMiddleware now also removes the Airflow JWT from the request it passes on once it has ended the session. get_user then answers 401 "Not authenticated", which sends the UI to the login page, and routes that need no user (the login route among them) keep working.

The core logout route is the exception: it needs no user and revokes the JWT it receives, so the middleware leaves the JWT in the request there. The middleware itself revokes nothing: a parallel request that fails on a rotated-out refresh token would otherwise revoke the JWT of the request that just refreshed the session. A rejected JWT (expired, invalid or revoked) now answers 401 "Not authenticated" instead of 403 "Invalid JWT token", since the route no longer sees a credential.

get_user_from_token keeps returning None in that case: the core JWTRefreshMiddleware resolves the Airflow JWT on every request without the Keycloak cookies, and an exception there would end every browser session. A comment now says so.

Tests: a new class runs the middlewares as init_middlewares installs them, in front of a route that needs the user. A session with its Keycloak tokens keeps its cookie (fails with the first version of this PR, which raised instead of returning None); an expired Keycloak refresh token and a missing access token cookie answer 401 and clear the cookie (both fail on main, where the route receives None as user) without revoking the JWT; the same two sessions sent to the core logout route do revoke it; a rejected JWT answers 401 and clears the cookie.

The None path came with #70550, which moved the Keycloak tokens into their own cookies in Airflow 3.3. #70707 reworks the middleware for Airflow 3.4.1+; this change is independent of it and covers 3.3 to 3.4.0.

@stegololz

closes: #76352
related: #70550, #70707


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Code (Opus 5.5)

Generated-by: Claude Code (Opus 5.5) following the guidelines

🤖 Generated with Claude Code

…turning None

Since Airflow 3.3 the browser JWT no longer carries the Keycloak tokens, and
get_user_from_token returned None when they were not supplied from the
cookies either. The core get_user dependency passed that None on to the
is_authorized_* checks, which failed with "'NoneType' object has no attribute
'access_token'" and answered every request with a 500. That happens as soon as
the Keycloak refresh token of a browser session has expired or its access
token cookie is missing, and the user stays stuck because the middleware never
gets to clear the session cookie.

Raise InvalidTokenError instead, as BaseAuthManager.get_user_from_token
promises. The API then answers 403 "Invalid JWT token", which sends the UI to
the login page.

closes: apache#76352
…oken

Raising InvalidTokenError from get_user_from_token broke every browser
session: the core JWTRefreshMiddleware resolves the Airflow JWT on every
request without the Keycloak cookies, and treated the exception as an
invalidated session, clearing the cookie after the first request. The None
it returned before is what lets that middleware pass the request on, so it
stays, now with a comment saying why.

The 500 comes from the other caller. When KeycloakJWTMiddleware cannot attach
the user (the Keycloak refresh token expired, or the access token cookie is
missing), it already ends the session by clearing the Airflow JWT on the
response, but the route still received the JWT and get_user resolved it to
None for the authorization checks. The middleware now also removes the JWT
from the request it passes on, so get_user answers 401 and the UI goes to the
login page, while routes that need no user keep working.

The new tests run the middlewares as the API server installs them in front
of a route that needs the user: a session with its Keycloak tokens keeps its
cookie, and an expired or missing Keycloak session answers 401 and clears it.
@dabla
dabla requested a review from stephen-bracken November 5, 2026 08:00

@stephen-bracken stephen-bracken left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I will include this with #70707

@dabla

dabla commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

LGTM. I will include this with #70707

Thx for the review @stephen-bracken.

Once KeycloakJWTMiddleware hid the Airflow JWT from the request, the core
logout route found no token to revoke: a user whose Keycloak refresh token
had expired and who then logged out kept an Airflow JWT that stayed valid
until it expired. The middleware now revokes the JWT itself before hiding
it, on every path where it ends the session.

A JWT that was rejected itself (expired, invalid or already revoked) is not
revoked again: there is nothing to revoke, and the validator would log a
warning with a traceback on each such request. To tell the cases apart,
_refresh_user chains its 401 and 403 from the JWT error. revoke_token
validates the token and writes to the database, so it runs in the thread
pool, and it is looked up on the auth manager because it only exists from
Airflow 3.2.

A rejected JWT now answers 401 instead of 403, as the route no longer sees
a credential; a test covers it. The tests of an expired Keycloak session and
of a session without Keycloak cookies assert the revocation, and fail
without this change.
Revoking the Airflow JWT in KeycloakJWTMiddleware whenever it ended a
session logged users out on realms that rotate refresh tokens. Two requests
arrive with the same expired access token; the first refreshes and rotates
the refresh token, the second sends the old one, fails the same way as on an
expired refresh token and revoked the JWT both share. The first then found
its JWT revoked and cleared the session instead of setting the refreshed
cookies.

The middleware no longer revokes anything. On the core logout route it
leaves the JWT in the request, so the route revokes it itself; that route
needs no user, so the None user the JWT was hidden for cannot occur there.
On every other route the JWT is still hidden, without revocation.

The tests of an ended session are one parametrized test that also asserts
nothing is revoked, and a new one sends the same sessions through the core
logout route and asserts the revocation.
@dabla
dabla requested review from stegololz and vincbeck November 16, 2026 21:13
@dabla
dabla merged commit 67607c1 into apache:main Nov 17, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants