bito-code-review[bot] commented on code in PR #38407:
URL: https://github.com/apache/superset/pull/38407#discussion_r2908283856
##########
superset/mcp_service/auth.py:
##########
@@ -42,6 +43,90 @@
logger = logging.getLogger(__name__)
+# Constants for RBAC permission attributes (mirrors FAB conventions)
+PERMISSION_PREFIX = "can_"
+CLASS_PERMISSION_ATTR = "_class_permission_name"
+METHOD_PERMISSION_ATTR = "_method_permission_name"
+
+
+class MCPPermissionDeniedError(Exception):
+ """Raised when user lacks required RBAC permission for an MCP tool."""
+
+ def __init__(
+ self,
+ permission_name: str,
+ view_name: str,
+ user: str | None = None,
+ tool_name: str | None = None,
+ ):
+ self.permission_name = permission_name
+ self.view_name = view_name
+ self.user = user
+ self.tool_name = tool_name
+ message = (
+ f"Permission denied: {permission_name} on {view_name}"
+ + (f" for user {user}" if user else "")
+ + (f" (tool: {tool_name})" if tool_name else "")
+ )
+ super().__init__(message)
+
+
+def check_tool_permission(func: Callable[..., Any]) -> bool:
+ """Check if the current user has RBAC permission for an MCP tool.
+
+ Reads permission metadata stored on the function by the @tool decorator
+ and uses Superset's security_manager to verify access.
+
+ Controlled by the ``MCP_RBAC_ENABLED`` config flag (default True).
+ Set to False in superset_config.py to disable RBAC checking.
+
+ Args:
+ func: The tool function with optional permission attributes.
+
+ Returns:
+ True if user has permission or no permission is required.
+ """
+ try:
+ from flask import current_app
+
+ if not current_app.config.get("MCP_RBAC_ENABLED", True):
+ return True
+
+ from superset import security_manager
+
+ if not hasattr(g, "user") or not g.user:
+ logger.warning(
+ "No user context for permission check on tool: %s",
func.__name__
+ )
+ return False
+
+ class_permission_name = getattr(func, CLASS_PERMISSION_ATTR, None)
+ if not class_permission_name:
+ # No RBAC configured for this tool; allow by default.
+ return True
+
+ method_permission_name = getattr(func, METHOD_PERMISSION_ATTR, "read")
+ permission_str = f"{PERMISSION_PREFIX}{method_permission_name}"
+
+ has_permission = security_manager.can_access(
+ permission_str, class_permission_name
+ )
+
+ if not has_permission:
+ logger.warning(
+ "Permission denied for user %s: %s on %s (tool: %s)",
+ g.user.username,
+ permission_str,
+ class_permission_name,
+ func.__name__,
+ )
+
+ return has_permission
+
+ except (AttributeError, ValueError, RuntimeError) as e:
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Exception Handling Too Narrow</b></div>
<div id="fix">
Narrowing exception handling to only AttributeError, ValueError, and
RuntimeError may allow other exceptions (e.g., database errors from
security_manager.can_access) to propagate instead of returning False,
potentially crashing MCP tools on permission check failures.
</div>
<details>
<summary>
<b>Code suggestion</b>
</summary>
<blockquote>Check the AI-generated fix before applying</blockquote>
<div id="code">
````suggestion
except Exception as e:
````
</div>
</details>
</div>
<small><i>Code Review Run #0bd4f6</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]