Skip to content

Commit 3870032

Browse files
wukathcopybara-github
authored andcommitted
fix: in mcp mtls logic, check for authorization header case insensitively
httpx internally normalizes HTTP header keys to lowercase, so the check should be case-insensitive closes #6200 Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 940719476
1 parent 9d0a5a8 commit 3870032

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/google/adk/tools/mcp_tool/mcp_session_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ async def before_request(
391391
)
392392
return
393393

394-
if 'Authorization' in headers:
394+
if any(k.lower() == 'authorization' for k in headers):
395395
logger.debug('Authorization header already present, not overwriting')
396396
return
397397

tests/unittests/tools/mcp_tool/test_mcp_session_manager.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,28 @@ def mock_refresh(req):
12981298

12991299
assert headers["Authorization"] == "Bearer refreshed_token"
13001300

1301+
@pytest.mark.skipif(not AIO_SUPPORTED, reason="google.auth.aio not supported")
1302+
@pytest.mark.parametrize(
1303+
"existing_header_key",
1304+
["Authorization", "authorization", "AUTHORIZATION", "authORIZATION"],
1305+
)
1306+
@pytest.mark.asyncio
1307+
async def test_before_request_skips_refresh_if_authorization_header_exists_case_insensitive(
1308+
self, existing_header_key
1309+
):
1310+
mock_creds = Mock()
1311+
mock_creds.expired = True
1312+
mock_creds.token = "new_token"
1313+
mock_creds.refresh = Mock()
1314+
1315+
credentials = _RefreshableAsyncCredentials(mock_creds)
1316+
headers = {existing_header_key: "Bearer existing_token"}
1317+
1318+
await credentials.before_request(None, "GET", "http://example.com", headers)
1319+
1320+
mock_creds.refresh.assert_not_called()
1321+
assert headers == {existing_header_key: "Bearer existing_token"}
1322+
13011323

13021324
class TestGoogleAuthAsyncByteStream:
13031325

0 commit comments

Comments
 (0)