fix(rpc): escape dangling backslashes in LIKE patterns - #8215
Open
phacops wants to merge 4 commits into
Open
Conversation
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
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QTo7u8JAEBC4he6hvX9vJu
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QTo7u8JAEBC4he6hvX9vJu
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.
User-supplied LIKE patterns forwarded from Sentry to
EndpointTraceItemTablecan 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 withCANNOT_PARSE_ESCAPE_SEQUENCE, failing the entire query.Observed failures (Sentry issues SNUBA-BVN, SNUBA-BTQ):
Both originate in the map-backed string
LIKEpath built bytrace_item_filters_to_expressioninsnuba/web/rpc/common/common.py.Fix
_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._sanitize_like_pattern_expression(), which applies that escaping to a string LIKE-pattern literal and passes non-string literals through unchanged.LIKE/NOT_LIKEsite: string keys, array keys (_typed_array_like_expression), and the any-attribute filter path — covering bothilike/likeand 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
TestLikePatternEscapingintests/web/rpc/test_common.py:_escape_dangling_like_backslashes(trailing backslash, mid-pattern dangling backslash, already-valid escapes, and no-op cases).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