Skip to content

Commit bb82cf4

Browse files
authored
Merge pull request #3489 from rommapp/copilot/fix-auth-header-handling
Handle malformed Authorization headers in HybridAuthBackend without 500s
2 parents 56f65bb + a2775ca commit bb82cf4

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

backend/handler/auth/hybrid_auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ async def authenticate(
2525

2626
# Check if Authorization header exists
2727
if "Authorization" in conn.headers:
28-
scheme, token = conn.headers["Authorization"].split()
28+
auth_header_parts = conn.headers["Authorization"].split()
29+
if len(auth_header_parts) != 2:
30+
return None
31+
32+
scheme, token = auth_header_parts
2933

3034
# Check if basic auth header is valid
3135
if scheme.lower() == "basic":

backend/tests/endpoints/test_heartbeat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from unittest.mock import AsyncMock, MagicMock, patch
22

3+
import pytest
34
from fastapi import status
45

56
from exceptions.fs_exceptions import PlatformAlreadyExistsException
@@ -51,6 +52,14 @@ def test_heartbeat(client):
5152
assert isinstance(oidc["RP_INITIATED_LOGOUT"], bool)
5253

5354

55+
@pytest.mark.parametrize("authorization_header", ["Bearer ", "Foo", "a b c"])
56+
def test_heartbeat_with_malformed_authorization_header(client, authorization_header: str):
57+
response = client.get(
58+
"/api/heartbeat", headers={"Authorization": authorization_header}
59+
)
60+
assert response.status_code == status.HTTP_200_OK
61+
62+
5463
def test_heartbeat_metadata(client):
5564
response = client.get("/api/heartbeat/metadata/launchbox")
5665
assert response.status_code == status.HTTP_200_OK

backend/tests/handler/auth/test_auth.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,22 @@ def __init__(self):
192192
assert result is None
193193

194194

195+
@pytest.mark.parametrize("authorization_header", ["Bearer ", "Foo", "a b c"])
196+
async def test_hybrid_auth_backend_malformed_authorization_header(
197+
authorization_header: str,
198+
):
199+
class MockConnection(HTTPConnection):
200+
def __init__(self):
201+
self.scope: dict[str, dict] = {"session": {}}
202+
self._headers = {"Authorization": authorization_header}
203+
204+
backend = HybridAuthBackend()
205+
conn = MockConnection()
206+
207+
result = await backend.authenticate(conn)
208+
assert result is None
209+
210+
195211
async def test_hybrid_auth_backend_with_refresh_token(editor_user: User):
196212
refresh_token = oauth_handler.create_refresh_token(
197213
data={

0 commit comments

Comments
 (0)