fix(mcp): correct method name in API key auth (extract_api_key_from_request)#39437
Conversation
…equest) The hasattr check and call in _resolve_user_from_api_key() referenced sm._extract_api_key_from_request (private, with underscore) but FAB defines the method as sm.extract_api_key_from_request (public). The hasattr always returned False, so MCP never authenticated via API key and silently fell through to MCP_DEV_USERNAME / g.user instead. Also update test_auth_api_key.py to use the correct public method name throughout, and add a regression test that asserts both method names referenced in auth.py actually exist on the real SecurityManager object so CI catches future mismatches instead of silently failing at runtime. Fixes SC-99414. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Code Review Agent Run #4ddf1bActionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #39437 +/- ##
==========================================
- Coverage 64.49% 63.98% -0.52%
==========================================
Files 2557 2562 +5
Lines 133097 137608 +4511
Branches 30910 31981 +1071
==========================================
+ Hits 85846 88042 +2196
- Misses 45761 47970 +2209
- Partials 1490 1596 +106
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
eschutho
left a comment
There was a problem hiding this comment.
Review: fix(mcp): correct method name in API key auth
Verdict: Approve with minor notes. The fix is correct, minimal, and addresses a real auth-bypass bug. The method name extract_api_key_from_request is confirmed correct against the installed FAB 5.2.1 (manager.py:2525, @staticmethod; also called identically in FAB's own decorators.py:105). The regression test concept is excellent. A few things worth noting below.
What works well
- Correct fix, minimal diff. Exactly the two call sites in
_resolve_user_from_api_keyneeded to change — nothing more. - All mock references updated consistently. Six test cases updated across
test_auth_api_key.py; none missed. - Comment improved accurately. Removing "internal" from the comment is right —
extract_api_key_from_requestis a public@staticmethodonBaseSecurityManager, not an internal method. - Regression test idea is solid. Checking that method names referenced in
auth.pyexist on the realSecurityManageris exactly the right kind of guard against future silent failures. - Security impact was severe.
hasattr(sm, "_extract_api_key_from_request")always returnedFalse, so API key auth was completely bypassed on every request — valid keys couldn't authenticate as themselves, and requests fell through toMCP_DEV_USERNAME(potentially a high-privilege admin user) or staleg.userfrom middleware. This fix closes that gap.
Issues
1. [Minor] Regression test may not exercise the fully-initialized SecurityManager
test_security_manager_has_expected_api_key_methods has no app fixture and does not push an app context:
# tests/unit_tests/mcp_service/test_auth_api_key.py (new test, bottom of file)
def test_security_manager_has_expected_api_key_methods() -> None:
from superset import security_manager
sm = security_manager
assert hasattr(sm, "extract_api_key_from_request"), ...
assert hasattr(sm, "validate_api_key"), ...In Superset, security_manager is a module-level proxy that may not be bound to the AppBuilder-initialized instance without an active app context. The hasattr check might pass trivially against the proxy class itself rather than the real initialized SM. Adding the app fixture (as every other test in this file does) ensures this test exercises the actual runtime object:
def test_security_manager_has_expected_api_key_methods(app) -> None:
with app.app_context():
from superset import security_manager
sm = security_manager
...2. [Nit] Internal ticket reference in docstring
"""...This catches future renames before they silently break API key auth
at runtime (SC-99414: _extract_api_key_from_request vs
extract_api_key_from_request)."""\n```
`SC-99414` is an internal ticket that's opaque to external contributors. Replace with a reference to this GitHub PR number (or the issue it fixes) so the history is navigable from the open-source repo.
**3. [Nit] Pre-existing gap in test coverage (not introduced here, but worth noting)**
There is no test for the path where `extract_api_key_from_request()` returns `None` (i.e., the Bearer token is present but doesn't match any `FAB_API_KEY_PREFIXES`). The code correctly handles it with `if api_key_string is None: return None`, but a test covering that path would make the suite complete.
---
### Questions
1. Is `extract_api_key_from_request` available in all FAB versions that Superset supports, or only from a certain version? The existing `hasattr` guard implies it might not always be present — it would be helpful to document which minimum FAB version introduced this method (or point to the FAB release notes).
2. Was `FAB_API_KEY_ENABLED` ever set to `True` in any Superset deployment configs, or was this essentially dead code until now? Knowing the blast radius helps assess whether a CHANGELOG/UPDATING.md entry is warranted.
---
Overall this is a clear, correct fix for a silent security regression. The two minor points above are optional improvements; they shouldn't block merge.
eschutho
left a comment
There was a problem hiding this comment.
Review: fix(mcp): correct method name in API key auth
Verdict: Approve with minor notes. The fix is correct, minimal, and addresses a real auth-bypass bug. The method name extract_api_key_from_request is confirmed correct against the installed FAB 5.2.1 (manager.py:2525, defined as @staticmethod; also called identically in FAB's own decorators.py:105). A few things worth noting below.
What works well
- Correct fix, minimal diff. Exactly the two call sites in
_resolve_user_from_api_keyneeded to change — nothing more. - All mock references updated consistently. Six test cases in
test_auth_api_key.pyupdated; none missed. - Comment improved accurately. Removing "internal" from the comment is right —
extract_api_key_from_requestis a public@staticmethodonBaseSecurityManager, not an internal method. - Regression test idea is solid. Checking that method names referenced in
auth.pyexist on the realSecurityManageris exactly the right guard against future silent failures. - Security impact was severe.
hasattr(sm, "_extract_api_key_from_request")always returnedFalse, so API key auth was completely bypassed on every request — valid keys couldn't authenticate as themselves, and requests fell through toMCP_DEV_USERNAME(potentially a high-privilege admin user) or staleg.userfrom middleware. This fix closes that gap.
Issues
1. [Minor] Regression test may not exercise the fully-initialized SecurityManager
test_security_manager_has_expected_api_key_methods has no app fixture and does not push an app context:
# tests/unit_tests/mcp_service/test_auth_api_key.py (new test, bottom of file)
def test_security_manager_has_expected_api_key_methods() -> None:
from superset import security_manager
sm = security_manager
assert hasattr(sm, "extract_api_key_from_request"), ...In Superset, security_manager is a module-level proxy that may not be bound to the AppBuilder-initialized instance without an active app context. The hasattr check might pass trivially against the proxy class rather than the real initialized SM. Adding the app fixture (as every other test in this file does) and pushing an app context ensures this test exercises the actual runtime object.
2. [Nit] Internal ticket reference in docstring
The SC-99414 reference in the new test docstring is an internal ticket opaque to external contributors. Replace with a GitHub PR or issue number so the history is navigable from the public repo.
3. [Nit] Pre-existing gap in test coverage (not introduced here)
There is no test for the path where extract_api_key_from_request() returns None (Bearer token present but doesn't match any FAB_API_KEY_PREFIXES). The code handles it correctly; a test for that path would complete the suite.
Questions
-
Is
extract_api_key_from_requestavailable in all FAB versions Superset supports, or only from a certain version? Thehasattrguard implies it may not always be present — documenting the minimum FAB version that introduced this method would clarify the guard's intent. -
Was
FAB_API_KEY_ENABLED=Trueused in any real Superset deployments before this fix? Knowing the blast radius helps assess whether aCHANGELOG/UPDATING.mdentry is warranted.
Overall this is a clear, correct fix for a silent security regression. The minor points above are optional improvements and should not block merge.
|
Thanks for the thorough review @eschutho! All three items addressed in the latest commit: Issue 1 (regression test needs app context): Updated Issue 2 (SC-99414 internal reference): Replaced with a reference to this PR ( Issue 3 (missing test for non-matching prefix): Added Q1 (minimum FAB version): Q2 (blast radius): |
SUMMARY
Fixes a method name mismatch in
superset/mcp_service/auth.pythat caused MCP API key authentication to silently fail._resolve_user_from_api_key()referencedsm._extract_api_key_from_request(private, underscore prefix), but FAB defines it assm.extract_api_key_from_request(public, no underscore). Thehasattrcheck always returnedFalse, so the function returnedNoneimmediately and MCP fell through toMCP_DEV_USERNAME/g.userinstead of authenticating via API key.Changes:
superset/mcp_service/auth.py: Rename_extract_api_key_from_request→extract_api_key_from_requestin thehasattrguard and the call site (2 lines)tests/unit_tests/mcp_service/test_auth_api_key.py: Update all existing mock references to the correct method name, and add a regression test (test_security_manager_has_expected_api_key_methods) that asserts both method names referenced inauth.pyactually exist on the realSecurityManagerobject — so CI catches future name mismatches instead of silently failing at runtimeBEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A — auth bug fix with no UI changes.
TESTING INSTRUCTIONS
pytest tests/unit_tests/mcp_service/test_auth_api_key.py— all tests should pass/api/v1/security/api_keys/, then call an MCP tool withAuthorization: Bearer sst_<key>— should authenticate correctly instead of falling through to dev userADDITIONAL INFORMATION
Relates to #37973 (the PR that added API key auth — this fixes the MCP integration that failed QA 0/6).
🤖 Generated with Claude Code