Skip to content

fix(admin): prevent SQL injection in system query validation#7748

Open
fix-it-felix-sentry[bot] wants to merge 2 commits intomasterfrom
fix/sql-injection-system-queries-pf-66
Open

fix(admin): prevent SQL injection in system query validation#7748
fix-it-felix-sentry[bot] wants to merge 2 commits intomasterfrom
fix/sql-injection-system-queries-pf-66

Conversation

@fix-it-felix-sentry
Copy link
Contributor

Summary

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

Security 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:

  • Execute multiple SQL statements via semicolon injection
  • Bypass query validation using SQL comments
  • Inject malicious SQL code during the validation phase

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:

  • Single SELECT statements with trailing semicolons
  • Queries with string literals containing quotes
  • Queries with datetime functions

It properly blocks:

  • Multiple statement attempts (e.g., SELECT * FROM ...; DROP TABLE ...)
  • SQL comment-based injection attempts
  • Unbalanced quote patterns

References

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>
@fix-it-felix-sentry fix-it-felix-sentry bot requested a review from a team as a code owner February 19, 2026 00:02
@linear
Copy link

linear bot commented 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant