bito-code-review[bot] commented on code in PR #42014:
URL: https://github.com/apache/superset/pull/42014#discussion_r3574833491
##########
superset/mcp_service/sql_lab/tool/execute_sql.py:
##########
@@ -55,6 +55,52 @@
logger = logging.getLogger(__name__)
+async def _validate_non_destructive_sql(
+ request: ExecuteSqlRequest,
+ ctx: Context,
+ database: Any,
+ sql_preview: str,
+) -> ExecuteSqlResponse | None:
+ """Return an error response when SQL cannot safely be executed."""
+ with event_logger.log_context(action="mcp.execute_sql.ddl_check"):
+ try:
+ sql_to_check = request.sql
+ if request.template_params:
+ from superset.jinja_context import get_template_processor
+
+ tp = get_template_processor(database=database)
+ sql_to_check = tp.process_template(
+ request.sql, **request.template_params
+ )
+
+ script = SQLScript(sql_to_check, database.db_engine_spec.engine)
+ if script.has_destructive():
+ await ctx.error("Destructive DDL blocked: sql_preview=%r" %
sql_preview)
+ return ExecuteSqlResponse(
+ success=False,
+ error=(
+ "Destructive DDL statements (DROP, TRUNCATE, ALTER) "
+ "are not allowed through MCP. Use the Superset SQL "
+ "Lab UI for administrative database operations."
+ ),
+ error_type=SupersetErrorType.DML_NOT_ALLOWED_ERROR.value,
+ )
+ except Exception as parse_err:
+ await ctx.error(
+ "DDL pre-check failed to parse SQL, blocking query: %s" %
str(parse_err)
+ )
+ return ExecuteSqlResponse(
+ success=False,
Review Comment:
<!-- Bito Reply -->
The decision to retain the broad exception catch is appropriate given the
requirement for a fail-closed security boundary. Since the block encompasses
multiple operations—Jinja rendering, engine lookup, and SQL parsing—that can
each raise distinct, backend-specific exceptions, a broad catch ensures that
any unexpected validation failure results in the documented blocked-query
response rather than an unhandled exception.
**superset/mcp_service/sql_lab/tool/execute_sql.py**
```
except (SyntaxError, ValueError) as parse_err:
await ctx.error(
f"DDL pre-check failed to parse SQL, blocking query:
{parse_err}"
)
await ctx.error(
f"DDL pre-check failed to parse SQL, blocking query:
{parse_err}",
)
return ExecuteSqlResponse(
success=False,
```
--
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]