This is an automated email from the ASF dual-hosted git repository.

dabla pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 67607c15ca7 Fix 500 from the Keycloak auth manager when a browser 
session has no Keycloak tokens (#76353)
67607c15ca7 is described below

commit 67607c15ca7eef8f630bae65506c4d2b48895a8e
Author: David Blain <[email protected]>
AuthorDate: Tue Nov 17 07:59:00 2026 +0100

    Fix 500 from the Keycloak auth manager when a browser session has no 
Keycloak tokens (#76353)
    
    * Reject a Keycloak browser token without Keycloak tokens instead of 
returning 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: #76352
    
    * Hide an ended session's JWT from the route instead of rejecting the token
    
    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.
    
    * Revoke an ended session's JWT before hiding it from the route
    
    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.
    
    * Leave the revocation of an ended session's JWT to the logout route
    
    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.
---
 .../keycloak/auth_manager/keycloak_auth_manager.py |   4 +-
 .../providers/keycloak/auth_manager/middleware.py  |  30 ++++
 .../unit/keycloak/auth_manager/test_middleware.py  | 157 ++++++++++++++++++++-
 3 files changed, 187 insertions(+), 4 deletions(-)

diff --git 
a/providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py
 
b/providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py
index d7dbef4398f..f5bd0742262 100644
--- 
a/providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py
+++ 
b/providers/keycloak/src/airflow/providers/keycloak/auth_manager/keycloak_auth_manager.py
@@ -224,7 +224,9 @@ class 
KeycloakAuthManager(BaseAuthManager[KeycloakAuthManagerUser]):
         # the cookie path those claims are covered by the Airflow JWT 
signature, so they
         # need no separate subject check. A browser token does not carry them 
and can
         # only be completed by KeycloakJWTMiddleware from the cookies; without 
them
-        # there is nothing to authorize against.
+        # there is nothing to authorize against. Return None rather than 
raise: the
+        # core JWTRefreshMiddleware resolves this token on every request 
without the
+        # cookies, and an exception there would end every browser session.
         return user if user.access_token else None
 
     def get_url_login(self, **kwargs) -> str:
diff --git 
a/providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py 
b/providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py
index abe0db1e0fb..d81eaf2f450 100644
--- 
a/providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py
+++ 
b/providers/keycloak/src/airflow/providers/keycloak/auth_manager/middleware.py
@@ -52,6 +52,9 @@ else:
         return "/"
 
 
+# The core route that revokes the Airflow JWT sent with the request, relative 
to the root path.
+LOGOUT_PATH = "/api/v2/auth/logout"
+
 if TYPE_CHECKING:
     from fastapi import Request, Response
 
@@ -82,6 +85,13 @@ class KeycloakJWTMiddleware(BaseHTTPMiddleware):
                 HTTPException,
             ):
                 new_token = ""
+                # The session ends here and the response clears the Airflow 
JWT. Hide it
+                # from the inner middlewares and the route too: resolving it 
again without
+                # the Keycloak tokens yields no user, which ``get_user`` would 
hand to the
+                # authorization checks instead of answering 401. The logout 
route needs no
+                # user and revokes the JWT it receives, so it keeps it.
+                if not self._is_logout(request):
+                    self._drop_cookie(request, COOKIE_NAME_JWT_TOKEN)
 
             if user is not None:
                 request.state.user = user
@@ -235,6 +245,26 @@ class KeycloakJWTMiddleware(BaseHTTPMiddleware):
             )
         return response
 
+    @staticmethod
+    def _is_logout(request: Request) -> bool:
+        """Tell whether the request is for the core logout route."""
+        scope = request.scope
+        return scope["path"].removeprefix(scope.get("root_path", "")) == 
LOGOUT_PATH
+
+    @staticmethod
+    def _drop_cookie(request: Request, name: str) -> None:
+        """Remove a cookie from the request that the inner middlewares and the 
route receive."""
+        prefix = f"{name}=".encode("latin-1")
+        headers = []
+        for key, value in request.scope["headers"]:
+            if key != b"cookie":
+                headers.append((key, value))
+                continue
+            kept = b";".join(part for part in value.split(b";") if not 
part.lstrip().startswith(prefix))
+            if kept.strip():
+                headers.append((key, kept.strip()))
+        request.scope["headers"] = headers
+
     @staticmethod
     async def _is_jwt_still_valid(request: Request) -> bool:
         """
diff --git 
a/providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py 
b/providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py
index 31fa4ae456a..3de2b6b893e 100644
--- a/providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py
+++ b/providers/keycloak/tests/unit/keycloak/auth_manager/test_middleware.py
@@ -16,22 +16,35 @@
 # under the License.
 from __future__ import annotations
 
-from unittest.mock import AsyncMock, MagicMock, Mock, call, patch
+import base64
+import json
+import time
+from unittest.mock import ANY, AsyncMock, MagicMock, Mock, call, 
create_autospec, patch
 
 import pytest
-from fastapi import Request
+from fastapi import Depends, FastAPI, Request
+from fastapi.testclient import TestClient
 from jwt import ExpiredSignatureError, InvalidTokenError
 
+from airflow.api_fastapi import app as api_fastapi_app
 from airflow.api_fastapi.auth.managers.base_auth_manager import 
COOKIE_NAME_JWT_TOKEN
+from airflow.api_fastapi.auth.tokens import JWTValidator
 from airflow.api_fastapi.core_api import security as core_api_security
 from airflow.providers.keycloak.auth_manager.constants import (
+    CONF_CLIENT_ID_KEY,
+    CONF_CLIENT_SECRET_KEY,
+    CONF_REALM_KEY,
+    CONF_SECTION_NAME,
+    CONF_SERVER_URL_KEY,
     COOKIE_NAME_ACCESS_TOKEN,
     COOKIE_NAME_REFRESH_TOKEN,
 )
+from airflow.providers.keycloak.auth_manager.keycloak_auth_manager import 
KeycloakAuthManager
 from airflow.providers.keycloak.auth_manager.middleware import 
KeycloakJWTMiddleware
 from airflow.providers.keycloak.auth_manager.user import 
KeycloakAuthManagerUser
 
-from tests_common.test_utils.version_compat import AIRFLOW_V_3_1_7_PLUS
+from tests_common.test_utils.config import conf_vars
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_1_7_PLUS, 
AIRFLOW_V_3_3_PLUS
 
 if AIRFLOW_V_3_1_7_PLUS:
     from airflow.api_fastapi.auth.managers.exceptions import 
AuthManagerRefreshTokenExpiredException
@@ -56,6 +69,7 @@ class TestKeycloakJWTMiddleware:
         request.base_url.scheme = "https" if secure else "http"
         request.cookies = {}
         request.headers = {}
+        request.scope = {"type": "http", "path": "/", "headers": []}
         request.state = MagicMock(name="state", spec=[])
         request.state.user = None
         del request.state.user_authenticated_via
@@ -496,3 +510,140 @@ class TestKeycloakJWTMiddleware:
         response.set_cookie.assert_any_call(
             "_token", "", path="/", secure=secure, samesite="lax", 
httponly=True, max_age=0
         )
+
+
+def keycloak_access_token(*, expires_in: int) -> str:
+    """Build a JWT-shaped Keycloak access token for ``user_id``; only its 
payload is read."""
+    payload = json.dumps({"sub": "user_id", "exp": int(time.time()) + 
expires_in})
+    return 
f"header.{base64.urlsafe_b64encode(payload.encode()).decode().rstrip('=')}.signature"
+
+
+ENDED_KEYCLOAK_SESSIONS = [
+    pytest.param(
+        {
+            COOKIE_NAME_ACCESS_TOKEN: keycloak_access_token(expires_in=-300),
+            COOKIE_NAME_REFRESH_TOKEN: "refresh_token",
+        },
+        id="expired_refresh_token",
+    ),
+    pytest.param({}, id="no_keycloak_cookies"),
+]
+
+
[email protected](not AIRFLOW_V_3_3_PLUS, reason="The Keycloak JWTs travel 
in their own cookies since 3.3")
+class TestKeycloakJWTMiddlewareInTheApiServer:
+    """The middlewares as the API server installs them, in front of a route 
that needs the user."""
+
+    @pytest.fixture
+    def token_validator(self):
+        token_validator = Mock(spec=JWTValidator)
+        token_validator.avalidated_claims.return_value = {"user_id": 
"user_id", "name": "name"}
+        return token_validator
+
+    @pytest.fixture
+    def revoke_token(self):
+        with patch.object(KeycloakAuthManager, "revoke_token", autospec=True) 
as revoke_token:
+            yield revoke_token
+
+    @pytest.fixture
+    def client(self, token_validator, revoke_token):
+        from airflow.api_fastapi.core_api.app import init_middlewares
+        from airflow.api_fastapi.core_api.routes.public.auth import auth_router
+        from airflow.api_fastapi.core_api.security import get_user
+
+        with conf_vars(
+            {
+                (CONF_SECTION_NAME, CONF_CLIENT_ID_KEY): "client_id",
+                (CONF_SECTION_NAME, CONF_CLIENT_SECRET_KEY): "client_secret",
+                (CONF_SECTION_NAME, CONF_REALM_KEY): "realm",
+                (CONF_SECTION_NAME, CONF_SERVER_URL_KEY): "server_url",
+            }
+        ):
+            auth_manager = KeycloakAuthManager()
+
+        app = FastAPI()
+        app.state.auth_manager = auth_manager
+        app.include_router(auth_router, prefix="/api/v2")
+
+        @app.get("/user")
+        def read_user(user=Depends(get_user)):
+            return {"name": user.get_name()}
+
+        get_auth_manager = create_autospec(api_fastapi_app.get_auth_manager, 
return_value=auth_manager)
+        with (
+            patch("airflow.api_fastapi.app.get_auth_manager", 
get_auth_manager),
+            
patch("airflow.api_fastapi.auth.middlewares.refresh_token.get_auth_manager", 
get_auth_manager),
+            patch("airflow.api_fastapi.core_api.security.get_auth_manager", 
get_auth_manager),
+            
patch("airflow.providers.keycloak.auth_manager.middleware.get_auth_manager", 
get_auth_manager),
+            patch.object(KeycloakAuthManager, "_get_token_validator", 
return_value=token_validator),
+            patch.object(
+                KeycloakAuthManager,
+                "refresh_tokens",
+                autospec=True,
+                side_effect=AuthManagerRefreshTokenExpiredException("Refresh 
token expired"),
+            ),
+        ):
+            init_middlewares(app)
+            yield TestClient(app)
+
+    @staticmethod
+    def cleared_jwt_cookies(response) -> list[str]:
+        return [
+            cookie
+            for cookie in response.headers.get_list("set-cookie")
+            if cookie.startswith(f"{COOKIE_NAME_JWT_TOKEN}=") and "Max-Age=0" 
in cookie
+        ]
+
+    def test_session_with_keycloak_tokens_keeps_its_cookie(self, client, 
revoke_token):
+        client.cookies.set(COOKIE_NAME_JWT_TOKEN, "token")
+        client.cookies.set(COOKIE_NAME_ACCESS_TOKEN, 
keycloak_access_token(expires_in=300))
+        client.cookies.set(COOKIE_NAME_REFRESH_TOKEN, "refresh_token")
+
+        response = client.get("/user")
+
+        assert response.status_code == 200
+        assert response.json() == {"name": "name"}
+        assert self.cleared_jwt_cookies(response) == []
+        revoke_token.assert_not_called()
+
+    @pytest.mark.parametrize("keycloak_cookies", ENDED_KEYCLOAK_SESSIONS)
+    def test_ended_keycloak_session_answers_401_and_clears_the_cookie(
+        self, client, revoke_token, keycloak_cookies
+    ):
+        client.cookies.set(COOKIE_NAME_JWT_TOKEN, "token")
+        for name, value in keycloak_cookies.items():
+            client.cookies.set(name, value)
+
+        response = client.get("/user")
+
+        assert response.status_code == 401
+        assert self.cleared_jwt_cookies(response) != []
+        revoke_token.assert_not_called()
+
+    @pytest.mark.parametrize("keycloak_cookies", ENDED_KEYCLOAK_SESSIONS)
+    def test_logout_of_an_ended_keycloak_session_revokes_the_token(
+        self, client, revoke_token, keycloak_cookies
+    ):
+        client.cookies.set(COOKIE_NAME_JWT_TOKEN, "token")
+        for name, value in keycloak_cookies.items():
+            client.cookies.set(name, value)
+
+        response = client.get("/api/v2/auth/logout", follow_redirects=False)
+
+        assert response.status_code == 307
+        assert self.cleared_jwt_cookies(response) != []
+        revoke_token.assert_called_once_with(ANY, "token")
+
+    def test_rejected_jwt_answers_401_and_clears_the_cookie_without_revoking(
+        self, client, token_validator, revoke_token
+    ):
+        token_validator.avalidated_claims.side_effect = 
InvalidTokenError("Token has been revoked")
+        client.cookies.set(COOKIE_NAME_JWT_TOKEN, "token")
+        client.cookies.set(COOKIE_NAME_ACCESS_TOKEN, 
keycloak_access_token(expires_in=300))
+        client.cookies.set(COOKIE_NAME_REFRESH_TOKEN, "refresh_token")
+
+        response = client.get("/user")
+
+        assert response.status_code == 401
+        assert self.cleared_jwt_cookies(response) != []
+        revoke_token.assert_not_called()

Reply via email to