-
Notifications
You must be signed in to change notification settings - Fork 16.6k
Fix OAuth session race condition causing false 401 errors during login #61287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vincbeck
merged 7 commits into
apache:main
from
Jgprog117:fix-oauth-session-race-condition-57981
Feb 2, 2026
+136
−3
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c505dd6
Fix OAuth session race condition causing false 401 errors during login
Jgprog117 228a29d
Fix logging to use %-formatting instead of f-strings
Jgprog117 4480437
Add tests for CustomAuthOAuthView
Jgprog117 3eada20
Fix linting and formatting issues in OAuth session race condition fix
Jgprog117 be734e3
Address PR review feedback from SameerMesiah97
Jgprog117 f7a8fef
Fix test RuntimeError by avoiding Flask session LocalProxy access
Jgprog117 65509fe
Merge branch 'main' into fix-oauth-session-race-condition-57981
Jgprog117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
providers/fab/src/airflow/providers/fab/auth_manager/views/auth_oauth.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Custom OAuth authentication view to fix session timing race condition.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from flask import session | ||
| from flask_appbuilder.security.views import AuthOAuthView | ||
|
|
||
| from airflow.configuration import conf | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class CustomAuthOAuthView(AuthOAuthView): | ||
| """ | ||
| Custom OAuth authentication view that ensures session is committed before redirect. | ||
|
|
||
| Fixes issue #57981 where UI requests fail with 401 during OAuth flow because | ||
| the Flask session is not yet committed when the redirect response is sent. | ||
| """ | ||
|
|
||
| def oauth_authorized(self, provider): | ||
| """ | ||
| OAuth callback handler that explicitly commits session before redirect. | ||
|
|
||
| This method overrides the parent's oauth_authorized to ensure that the | ||
| Flask session (containing OAuth tokens and user authentication) is fully | ||
| committed to the session backend (cookie or database) before redirecting | ||
| the user to the UI. | ||
|
|
||
| This prevents the race condition where the UI makes API requests before | ||
| the session is available, causing temporary 401 errors during login. | ||
|
|
||
| Args: | ||
| provider: OAuth provider name (e.g., 'azure', 'google', 'github') | ||
|
|
||
| Returns: | ||
| Flask response object (redirect to original URL or home page) | ||
| """ | ||
| log.debug("OAuth callback received for provider: %s", provider) | ||
|
|
||
| # Call parent's OAuth callback handling | ||
| # This processes the OAuth response, authenticates the user, and sets session data | ||
| response = super().oauth_authorized(provider) | ||
|
|
||
| # Explicitly mark the Flask session as modified to ensure it's persisted | ||
| # before the redirect response is sent to the client | ||
| session.modified = True | ||
| session_backend = conf.get("fab", "SESSION_BACKEND", fallback="securecookie") | ||
| log.debug("Marked session as modified for %s backend", session_backend) | ||
|
|
||
| log.debug("OAuth callback handling completed for provider: %s", provider) | ||
|
|
||
| return response | ||
61 changes: 61 additions & 0 deletions
61
providers/fab/tests/unit/fab/auth_manager/views/test_auth_oauth.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from flask_appbuilder.security.views import AuthOAuthView | ||
|
|
||
| from airflow.providers.fab.auth_manager.views.auth_oauth import CustomAuthOAuthView | ||
|
|
||
|
|
||
| class TestCustomAuthOAuthView: | ||
| """Test CustomAuthOAuthView.""" | ||
|
|
||
| @pytest.mark.parametrize("backend", ["database", "securecookie"]) | ||
| @mock.patch("airflow.providers.fab.auth_manager.views.auth_oauth.conf") | ||
| def test_oauth_authorized_marks_session_modified(self, mock_conf, backend): | ||
| """Test that oauth_authorized marks session as modified regardless of backend.""" | ||
| mock_conf.get.return_value = backend | ||
| mock_session = mock.MagicMock() | ||
| view = CustomAuthOAuthView() | ||
|
|
||
| with ( | ||
| mock.patch("airflow.providers.fab.auth_manager.views.auth_oauth.session", new=mock_session), | ||
| mock.patch.object(AuthOAuthView, "oauth_authorized", return_value=mock.Mock()) as mock_parent, | ||
| ): | ||
| view.oauth_authorized("test_provider") | ||
|
|
||
| mock_parent.assert_called_once_with("test_provider") | ||
| assert mock_session.modified is True | ||
| mock_conf.get.assert_called_once_with("fab", "SESSION_BACKEND", fallback="securecookie") | ||
|
|
||
| @mock.patch("airflow.providers.fab.auth_manager.views.auth_oauth.conf") | ||
| def test_oauth_authorized_returns_parent_response(self, mock_conf): | ||
| """Test that oauth_authorized returns the response from parent method.""" | ||
| mock_conf.get.return_value = "database" | ||
| view = CustomAuthOAuthView() | ||
| mock_response = mock.Mock() | ||
|
|
||
| with ( | ||
| mock.patch("airflow.providers.fab.auth_manager.views.auth_oauth.session", new=mock.MagicMock()), | ||
| mock.patch.object(AuthOAuthView, "oauth_authorized", return_value=mock_response), | ||
| ): | ||
| result = view.oauth_authorized("google") | ||
| assert result == mock_response |
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.
Uh oh!
There was an error while loading. Please reload this page.