You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/06/01 23:47:08 UTC

(superset) 01/01: fix(mcp): deny deactivated user accounts in MCP auth

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

rusackas pushed a commit to branch fix/mcp-auth-reject-disabled-users
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 081851bc8c2b5ea32666cfc68e1adef87e2b08b7
Author: Claude Code <no...@anthropic.com>
AuthorDate: Mon Jun 1 16:46:45 2026 -0700

    fix(mcp): deny deactivated user accounts in MCP auth
    
    The MCP service authenticates via JWT/API key but, unlike Flask-Login's web
    session flow, never checked User.active. A user whose account had been disabled
    could keep making MCP tool calls (queries, chart/dashboard ops) as long as they
    held a valid token.
    
    Add an is_active check in the user-setup path: a resolved-but-inactive user now
    raises ValueError, which the existing fail-closed handler turns into a denied
    request. Extracted into a small _reject_if_inactive helper to keep the setup
    function within complexity limits. Tests cover disabled rejection and active
    acceptance.
    
    Co-Authored-By: Claude Opus 4.8 <no...@anthropic.com>
---
 superset/mcp_service/auth.py                       | 12 +++++++
 .../mcp_service/test_auth_user_resolution.py       | 38 ++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/superset/mcp_service/auth.py b/superset/mcp_service/auth.py
index 583b721b9ea..7c3b38f5d77 100644
--- a/superset/mcp_service/auth.py
+++ b/superset/mcp_service/auth.py
@@ -430,6 +430,17 @@ def check_chart_data_access(chart: Any) -> "DatasetValidationResult":
     return validate_chart_dataset(chart, check_access=True)
 
 
+def _reject_if_inactive(user: User | None) -> None:
+    """Raise ``ValueError`` if the resolved user account is deactivated.
+
+    A still-valid JWT or API key must not grant MCP access to a user whose
+    account has been disabled. This mirrors Flask-Login's ``is_active`` check
+    for web sessions, which the MCP auth path does not otherwise go through.
+    """
+    if user is not None and not getattr(user, "is_active", True):
+        raise ValueError("User account is disabled")
+
+
 def _setup_user_context() -> User | None:
     """
     Set up user context for MCP tool execution.
@@ -457,6 +468,7 @@ def _setup_user_context() -> User | None:
     for attempt in range(2):
         try:
             user = get_user_from_request()
+            _reject_if_inactive(user)
 
             # Validate user has necessary relationships loaded.
             # Force access to ensure they're loaded if lazy.
diff --git a/tests/unit_tests/mcp_service/test_auth_user_resolution.py b/tests/unit_tests/mcp_service/test_auth_user_resolution.py
index 34669e51d1e..e9d9a02a03a 100644
--- a/tests/unit_tests/mcp_service/test_auth_user_resolution.py
+++ b/tests/unit_tests/mcp_service/test_auth_user_resolution.py
@@ -535,3 +535,41 @@ def test_setup_user_context_propagates_valueerror(app) -> None:
                 _setup_user_context()
             # g.user should be cleared after ValueError (no misleading audit)
             assert not hasattr(g, "user") or g.user is None
+
+
+def test_setup_user_context_rejects_disabled_user(app) -> None:
+    """A deactivated account must be denied even with a valid token.
+
+    The MCP auth path does not go through Flask-Login's is_active check, so
+    this guards that a disabled user (active=False) cannot authenticate.
+    """
+    from superset.mcp_service.auth import _setup_user_context
+
+    disabled_user = _make_mock_user("disabled_user")
+    disabled_user.is_active = False
+
+    with app.test_request_context():
+        with patch(
+            "superset.mcp_service.auth.get_user_from_request",
+            return_value=disabled_user,
+        ):
+            with pytest.raises(ValueError, match="disabled"):
+                _setup_user_context()
+            assert not hasattr(g, "user") or g.user is None
+
+
+def test_setup_user_context_allows_active_user(app) -> None:
+    """An active account authenticates normally."""
+    from superset.mcp_service.auth import _setup_user_context
+
+    active_user = _make_mock_user("active_user")
+    active_user.is_active = True
+
+    with app.test_request_context():
+        with patch(
+            "superset.mcp_service.auth.get_user_from_request",
+            return_value=active_user,
+        ):
+            result = _setup_user_context()
+            assert result is active_user
+            assert g.user is active_user