Skip to content

Fix SQL injection vulnerability in cluster_name_override parameter#7830

Open
fix-it-felix-sentry[bot] wants to merge 1 commit intomasterfrom
fix-sql-injection-cluster-name
Open

Fix SQL injection vulnerability in cluster_name_override parameter#7830
fix-it-felix-sentry[bot] wants to merge 1 commit intomasterfrom
fix-sql-injection-cluster-name

Conversation

@fix-it-felix-sentry
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a critical SQL injection vulnerability in the cluster_name_override parameter used by the /run_copy_table_query endpoint.

Issue

The cluster_name_override parameter was being passed directly from user input without validation and used in SQL queries, creating a SQL injection vulnerability at:

  • Line 62 in copy_tables.py: f"{db_table} ON CLUSTER '{cluster_name}'"
  • Line 91 in copy_tables.py: f"FROM clusterAllReplicas('{cluster_name}', system.tables)"
  • Lines 180, 183: SQL statements containing the cluster name were executed

Solution

Added strict input validation to only allow alphanumeric characters, underscores, and hyphens in cluster names. This prevents SQL injection attacks while still allowing legitimate cluster naming conventions.

Changes

  • Added regex validation for cluster_name_override parameter
  • Raises ValueError if invalid characters are detected
  • Added comprehensive test coverage for SQL injection prevention

Testing

  • ✅ Validated that legitimate cluster names (alphanumeric, underscores, hyphens) are accepted
  • ✅ Verified that malicious SQL injection attempts are properly rejected
  • ✅ Added new test: test_copy_tables_cluster_name_override_sql_injection_prevention()

References


Co-Authored-By: Claude Sonnet 4.5 noreply@anthropic.com

Validates cluster_name_override to only allow alphanumeric characters,
underscores, and hyphens, preventing SQL injection attacks through
malicious cluster names.

The cluster_name_override parameter is passed directly from user input
via the Flask API endpoint /run_copy_table_query and was previously used
without validation in SQL queries, creating a critical SQL injection
vulnerability at:
- Line 62: f"{db_table} ON CLUSTER '{cluster_name}'"
- Line 91: f"FROM clusterAllReplicas('{cluster_name}', system.tables)"

This fix adds strict input validation using a regex pattern to reject
any cluster names containing special characters that could be used for
SQL injection attacks.

Also added comprehensive test coverage to verify that malicious input
is properly rejected.

Fixes: https://linear.app/getsentry/issue/VULN-1340
Fixes: https://linear.app/getsentry/issue/PF-75

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 March 23, 2026 16:50
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

if not re.match(r"^[a-zA-Z0-9_-]+$", cluster_name_override):
raise ValueError(
"Invalid cluster name: only alphanumeric characters, underscores, and hyphens are allowed"
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError caught with misleading "Target host is invalid" message

Medium Severity

The new ValueError raised for invalid cluster names gets caught by the existing except ValueError handler in views.py line 489, which formats the response as "Target host is invalid: {err.args[0]}". Users attempting an invalid cluster name will see the confusing message "Target host is invalid: Invalid cluster name: only alphanumeric characters…" — misattributing the error to the target host instead of the cluster name.

Fix in Cursor Fix in Web

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.

0 participants