fix(mcp): Block destructive DDL (DROP, TRUNCATE, ALTER) in execute_sql (#39621)

This commit is contained in:
Alexandru Soare
2026-05-20 14:29:15 +03:00
committed by GitHub
parent 0a3a35018c
commit b98bd2a07a
4 changed files with 310 additions and 3 deletions

View File

@@ -1327,6 +1327,66 @@ def test_is_mutating_anonymous_block(sql: str, expected: bool) -> None:
assert SQLStatement(sql, "postgresql").is_mutating() == expected
@pytest.mark.parametrize(
"sql, expected",
[
("SELECT 1", False),
("INSERT INTO t VALUES (1)", False),
("UPDATE t SET x = 1", False),
("DELETE FROM t", False),
("MERGE INTO t USING s ON t.id = s.id WHEN MATCHED THEN DELETE", False),
("CREATE TABLE t (id INT)", False),
("DROP TABLE t", True),
("DROP TABLE IF EXISTS t", True),
("DROP VIEW v", True),
("TRUNCATE TABLE t", True),
("ALTER TABLE t ADD COLUMN x INT", True),
("ALTER TABLE t DROP COLUMN x", True),
],
)
def test_is_destructive(sql: str, expected: bool) -> None:
"""
Test that ``is_destructive`` detects DROP, TRUNCATE, and ALTER
but not SELECT, INSERT, UPDATE, DELETE, MERGE, or CREATE.
"""
assert SQLStatement(sql, "postgresql").is_destructive() == expected
@pytest.mark.parametrize(
"sql, expected",
[
("SELECT 1; INSERT INTO t VALUES (1)", False),
("SELECT 1; DROP TABLE t", True),
("SELECT 1; TRUNCATE TABLE t", True),
("CREATE TABLE t (id INT); ALTER TABLE t ADD COLUMN x INT", True),
],
)
def test_has_destructive(sql: str, expected: bool) -> None:
"""
Test that ``has_destructive`` on SQLScript detects destructive DDL
across multiple statements.
"""
assert SQLScript(sql, "postgresql").has_destructive() == expected
@pytest.mark.parametrize(
"kql, expected",
[
(".drop table T", True),
(".alter table T (col:string)", True),
(".show tables", False),
("T | count", False),
],
)
def test_kusto_is_destructive(kql: str, expected: bool) -> None:
"""
Test ``is_destructive`` on KustoKQLStatement.
"""
from superset.sql.parse import KustoKQLStatement
assert KustoKQLStatement(kql, "kustokql").is_destructive() == expected
def test_optimize() -> None:
"""
Test that the `optimize` method works as expected.