Repository navigation
fix(db): reject nested and trailing writes in read-only SQL check - #1544
Open
pcbeingused33 wants to merge 1 commit into
Open
pcbeingused33 wants to merge 1 commit into
pcbeingused33 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1542
check_sql_read()decided whether SQL writes by looking only at the type of each top-level statement and at the first keyword of the whole string. That missed:WITH d AS (DELETE ... RETURNING id) SELECT * FROM d), which parse as a top-levelSelect;SELECT ... INTO new_table, also aSelect;SELECT 1; TRUNCATE TABLE t,SET,GRANT,REVOKE), or when sqlglot falls back to a genericCommand(SELECT 1; CALL proc()).Fix
stmt.find(*write_types)) instead of checking only its root type.TruncateTable,Set,Grant,RevokeandIntoto the write node types.Command, check that statement's own keyword against the allowed read commands, soCALLis rejected whileSHOW/EXPLAINstill followSQLBOT_ALLOW_METADATA_QUERIES.Multi-statement reads (
SELECT ...; SELECT ...) are still accepted; the dangerous-function denylist (points 4-5 of the issue) is unchanged.Tests
New
backend/tests/test_check_sql_read_writes.pycovers the bypasses from the issue (CTE DELETE/UPDATE/INSERT,SELECT ... INTO, trailing TRUNCATE/SET/GRANT/REVOKE/CALL), ordinary reads with CTEs and subqueries, and metadata commands with the setting enabled. Before the fix 10 cases fail; after it all pass, together withtests/test_postgres_denylist.py: