Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions superset/mcp_service/sql_lab/tool/execute_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
)

from superset.errors import SupersetErrorType
from superset.exceptions import OAuth2Error, OAuth2RedirectError
from superset.exceptions import (
OAuth2Error,
OAuth2RedirectError,
SupersetSecurityException,
)
from superset.extensions import event_logger
from superset.mcp_service.sql_lab.schemas import (
ColumnInfo,
Expand Down Expand Up @@ -108,13 +112,28 @@ async def execute_sql(request: ExecuteSqlRequest, ctx: Context) -> ExecuteSqlRes
error_type=SupersetErrorType.DATABASE_NOT_FOUND_ERROR.value,
)

if not security_manager.can_access_database(database):
# Mirror the SQL Lab UI's access check (CanAccessQueryValidatorImpl):
# a blanket `database_access` grant is sufficient, but in its
# absence a user who owns or has `datasource_access` on every
# dataset referenced by the SQL is also allowed through. Using
# the narrower can_access_database() check alone here previously
# rejected MCP callers that the web UI would have let through.
try:
security_manager.raise_for_access(
database=database,
sql=request.sql,
schema=request.schema_name,
catalog=request.catalog,
template_params=request.template_params,
force_dataset_match=True,
)
except SupersetSecurityException as security_exc:
Comment on lines +121 to +130

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The new access pre-check only handles SupersetSecurityException, but raise_for_access(...) can also raise non-security exceptions during SQL/Jinja parsing (for example template/parsing failures). Those exceptions now bypass this block and hit the outer generic except Exception path, which re-raises and turns a user input error into an unhandled tool failure. Catch and map the known access-check parse/template exceptions to a structured ExecuteSqlResponse (as done for other validation failures) instead of letting them escape. [api mismatch]

Severity Level: Major ⚠️
- ❌ MCP execute_sql crashes on malformed Jinja-templated SQL.
- ⚠️ SQL exploration via MCP returns opaque tool failures.
- ⚠️ Inconsistent error handling vs DDL parse validation.
Steps of Reproduction ✅
1. Invoke the MCP `execute_sql` tool as documented in `superset/mcp_service/app.py:81-99`
(e.g. via an MCP client) using the `execute_sql(request={{\"database_id\": <id>, \"sql\":
\"SELECT {{ foo }}\"}})` workflow.

2. Ensure the target database exists and is accessible, then call `execute_sql()` in
`superset/mcp_service/sql_lab/tool/execute_sql.py:72-138` with `request.sql` containing
invalid or unrenderable Jinja (e.g. `SELECT {{ missing_var }}`) and
`request.template_params={}` or mismatched params.

3. Inside `execute_sql`, the code enters the DB validation block at
`execute_sql.py:97-139` and calls `security_manager.raise_for_access(database=database,
sql=request.sql, schema=request.schema_name, catalog=request.catalog,
template_params=request.template_params, force_dataset_match=True)` within the inner
`try:` at lines 121-129.

4. `SupersetSecurityManager.raise_for_access` in `superset/security/manager.py:3115-3225`
constructs a SQL Lab `Query` and calls `process_jinja_sql(sql_for_parse, database,
parse_template_params)` at `manager.py:135-139`, which is implemented in
`superset/sql/parse.py:1744-69` and documented to raise `jinja2.exceptions.TemplateError`
when the Jinja SQL cannot be rendered.

5. When `process_jinja_sql` raises `TemplateError` (or another
non-`SupersetSecurityException` parsing error), this exception is not caught by the inner
`except SupersetSecurityException as security_exc:` at `execute_sql.py:130-138`, so it
bubbles up to the outer `except Exception as e:` at `execute_sql.py:256-259`, which logs
`"SQL execution failed"` and then re-raises, causing the MCP tool call to fail with an
unhandled server error instead of returning a structured `ExecuteSqlResponse` like the DDL
parse block does at `execute_sql.py:145-181`.

Fix in Cursor Fix in VSCode Claude

(Use Cmd/Ctrl + Click for best experience)

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/mcp_service/sql_lab/tool/execute_sql.py
**Line:** 121:130
**Comment:**
	*Api Mismatch: The new access pre-check only handles `SupersetSecurityException`, but `raise_for_access(...)` can also raise non-security exceptions during SQL/Jinja parsing (for example template/parsing failures). Those exceptions now bypass this block and hit the outer generic `except Exception` path, which re-raises and turns a user input error into an unhandled tool failure. Catch and map the known access-check parse/template exceptions to a structured `ExecuteSqlResponse` (as done for other validation failures) instead of letting them escape.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

await ctx.warning(
"Access denied to database: %s" % database.database_name
)
return ExecuteSqlResponse(
success=False,
error=f"Access denied to database {database.database_name}",
error=security_exc.error.message,
error_type=SupersetErrorType.DATABASE_SECURITY_ACCESS_ERROR.value,
)

Expand Down
11 changes: 10 additions & 1 deletion tests/unit_tests/mcp_service/sql_lab/tool/test_execute_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,20 @@ async def test_execute_sql_access_denied(
self, mock_db, mock_security_manager, mcp_server
):
"""Test error when user lacks database access."""
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.exceptions import SupersetSecurityException

mock_database = _mock_database()
mock_db.session.query.return_value.filter_by.return_value.first.return_value = (
mock_database
)
mock_security_manager.can_access_database.return_value = False
mock_security_manager.raise_for_access.side_effect = SupersetSecurityException(
SupersetError(
message="Access denied to database test_db",
error_type=SupersetErrorType.DATABASE_SECURITY_ACCESS_ERROR,
level=ErrorLevel.ERROR,
)
)

request = {
"database_id": 1,
Expand Down