reorganization

This commit is contained in:
Roberto Treviño Cervantes
2026-10-31 12:47:59 -06:00
parent ae1ee4633f
commit fce6ccd40a
9 changed files with 185 additions and 23 deletions
+38 -4
View File
@@ -65,12 +65,44 @@ def test_only_single_select_queries_pass(query):
SqlSourcePolicy.check_query(query)
@pytest.mark.parametrize("query", ["SELECT * FROM t;", " with x as (select 1 as a) select a from x", "/* c */ SELECT 1"])
@pytest.mark.parametrize(
"query",
["SELECT * FROM t;", " with x as (select 1 as a) select a from x", "/* c */ SELECT 1", "SELECT 'a;b' AS literal"],
)
def test_select_queries_pass(query):
assert SqlSourcePolicy.check_query(query)
def test_connection_is_read_only_even_for_a_select_shaped_write(tmp_path):
def test_a_writable_cte_is_rejected_by_parsing_the_query_not_matching_keywords():
"""`WITH d AS (DELETE FROM t RETURNING a) SELECT a FROM d` is a syntactic
SELECT at the top level, so a check that only looks at the first keyword
lets it through; parsing the query and inspecting the tree catches the
write hiding inside the CTE."""
with pytest.raises(ConnectorError, match="no puede escribir"):
SqlSourcePolicy.check_query("WITH d AS (DELETE FROM t RETURNING a) SELECT a FROM d")
def test_select_into_a_new_table_is_rejected():
"""SQL Server's `SELECT ... INTO` creates a table — a write a keyword
check would miss entirely, since the statement still starts with SELECT."""
with pytest.raises(ConnectorError, match="no puede escribir"):
SqlSourcePolicy.check_query("SELECT * INTO nueva_tabla FROM t", dialect="mssql")
def test_mysql_select_into_outfile_is_rejected():
"""MySQL's `SELECT ... INTO OUTFILE` writes a file to the server's own
filesystem. sqlglot's MySQL grammar doesn't model this clause at all, so
it fails to parse — rejected as invalid SQL, which is the safe default
for anything that can't be positively confirmed to be a plain read."""
with pytest.raises(ConnectorError):
SqlSourcePolicy.check_query("SELECT * FROM t INTO OUTFILE '/tmp/x.csv'", dialect="mysql")
def test_connection_is_read_only_even_for_a_write_the_query_parser_could_miss(tmp_path):
"""Defense in depth: even if a future sqlglot version or an edge-case
dialect quirk let a write-shaped query past `check_query`, the
connection itself is read-only and the transaction is always rolled
back, so the underlying database still refuses to execute it."""
db = tmp_path / "src.db"
engine = sa.create_engine(f"sqlite:///{db}")
with engine.begin() as conn:
@@ -78,9 +110,11 @@ def test_connection_is_read_only_even_for_a_select_shaped_write(tmp_path):
conn.execute(sa.text("INSERT INTO t VALUES (1), (2)"))
connector = SqlConnector(f"sqlite:///{db}", policy=SqlSourcePolicy(frozenset({"sqlite"}), None))
# Bypass check_query directly to isolate the read-only connection itself,
# rather than testing the parser twice.
connector._query, connector._table = "WITH d AS (DELETE FROM t RETURNING a) SELECT a FROM d", None
with pytest.raises(ConnectorError):
# Passes the keyword check, but the read-only connection refuses it.
connector.select_query("WITH d AS (DELETE FROM t RETURNING a) SELECT a FROM d").preview()
connector.preview()
with engine.connect() as conn:
assert conn.execute(sa.text("SELECT COUNT(*) FROM t")).scalar_one() == 2