bito-code-review[bot] commented on code in PR #42014:
URL: https://github.com/apache/superset/pull/42014#discussion_r3574745433


##########
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:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Catch specific exceptions instead</b></div>
   <div id="fix">
   
   Replace blind `Exception` catch with specific exception types (e.g., 
`SQLParsingException`) or catch a narrower set of expected exceptions.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ````suggestion
           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,
   ````
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #92ef27</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]

Reply via email to