Skip to content

fix(rpc): escape dangling backslashes in LIKE patterns - #8215

Open
phacops wants to merge 4 commits into
masterfrom
claude/clickhouse-like-escape-fix-uabg2i
Open

fix(rpc): escape dangling backslashes in LIKE patterns#8215
phacops wants to merge 4 commits into
masterfrom
claude/clickhouse-like-escape-fix-uabg2i

Conversation

@phacops

@phacops phacops commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

User-supplied LIKE patterns forwarded from Sentry to EndpointTraceItemTable can contain a backslash that does not begin a valid ClickHouse LIKE escape sequence. ClickHouse only accepts \%, \_ and \\; anything else — most commonly a trailing backslash — is rejected server-side with CANNOT_PARSE_ESCAPE_SEQUENCE, failing the entire query.

Observed failures (Sentry issues SNUBA-BVN, SNUBA-BTQ):

DB::Exception: Invalid escape sequence at the end of LIKE pattern '%Background\'
DB::Exception: Invalid escape sequence at the end of LIKE pattern '%Received\'

Both originate in the map-backed string LIKE path built by trace_item_filters_to_expression in snuba/web/rpc/common/common.py.

Fix

  • Add _escape_dangling_like_backslashes(), which escapes each backslash that is not part of a valid escape sequence (\%, \_, \\) into a literal \\, so ClickHouse matches it as a literal backslash instead of erroring. Well-formed escape sequences and wildcard-only patterns are left untouched.
  • Add _sanitize_like_pattern_expression(), which applies that escaping to a string LIKE-pattern literal and passes non-string literals through unchanged.
  • Route the pattern through the sanitizer at every LIKE/NOT_LIKE site: string keys, array keys (_typed_array_like_expression), and the any-attribute filter path — covering both ilike/like and their negations.

Since a backslash in a user's search text is meant literally, escaping preserves the user's intent while making the pattern valid ClickHouse.

Tests

Added TestLikePatternEscaping in tests/web/rpc/test_common.py:

  • Parametrized unit tests for _escape_dangling_like_backslashes (trailing backslash, mid-pattern dangling backslash, already-valid escapes, and no-op cases).
  • End-to-end assertions that the sanitized pattern reaches the built expression for string-key LIKE / NOT_LIKE, array-key LIKE, and any-attribute LIKE.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QTo7u8JAEBC4he6hvX9vJu


Generated by Claude Code

User-supplied LIKE patterns forwarded from Sentry can contain a backslash
that does not begin a valid ClickHouse escape sequence (\%, \_ or \\) —
most commonly a trailing backslash such as '%Background\'. ClickHouse
rejects these with CANNOT_PARSE_ESCAPE_SEQUENCE, failing the whole query.

Escape each dangling backslash to a literal backslash before building the
LIKE/NOT_LIKE expression, so it matches literally instead of erroring.
Well-formed escape sequences are left untouched. Applied to string keys,
array keys, and any-attribute filters.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QTo7u8JAEBC4he6hvX9vJu
@phacops
phacops requested review from a team as code owners July 23, 2026 18:34
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.

2 participants