fix(admin): prevent SQL injection in system query validation#7748
Open
fix-it-felix-sentry[bot] wants to merge 2 commits intomasterfrom
Open
fix(admin): prevent SQL injection in system query validation#7748fix-it-felix-sentry[bot] wants to merge 2 commits intomasterfrom
fix-it-felix-sentry[bot] wants to merge 2 commits intomasterfrom
Conversation
Added sanitization for SQL queries before using them in EXPLAIN statements to prevent SQL injection vulnerabilities during query validation. Changes: - Added _sanitize_query_for_explain() function to validate and sanitize queries before they are used in EXPLAIN QUERY TREE and EXPLAIN AST statements - Checks for unbalanced quotes, multiple statement attempts, and SQL comments - Applied sanitization in is_query_using_only_system_tables() and is_valid_system_query() functions This fix addresses SQL injection vulnerability in: - snuba/admin/clickhouse/system_queries.py:51 The sanitization adds defense-in-depth protection during the validation phase, while maintaining the existing EXPLAIN AST-based validation logic. Fixes: https://linear.app/getsentry/issue/VULN-1029 Fixes: https://linear.app/getsentry/issue/PF-66 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
phacops
approved these changes
Feb 19, 2026
The unquoted `default` clusterAllReplicas query was incorrectly in the invalid test list. It only appeared to fail before because the old semicolon-stripping logic didn't handle trailing whitespace after the semicolon, causing EXPLAIN to error. The new _sanitize_query_for_explain correctly strips whitespace first, revealing this is a valid query. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
Added sanitization for SQL queries before using them in EXPLAIN statements to prevent SQL injection vulnerabilities during query validation.
Changes
_sanitize_query_for_explain()function to validate and sanitize queries before they are used in EXPLAIN QUERY TREE and EXPLAIN AST statementsis_query_using_only_system_tables()andis_valid_system_query()functionsSecurity Impact
This fix addresses a SQL injection vulnerability where user input was directly interpolated into EXPLAIN statements without proper sanitization. The vulnerability could potentially allow an attacker to:
The fix adds defense-in-depth protection during the validation phase while maintaining the existing EXPLAIN AST-based validation logic.
Testing
The fix maintains backward compatibility with existing legitimate queries:
It properly blocks:
SELECT * FROM ...; DROP TABLE ...)References