fix(scanner): use | as LIKE escape char to stop MariaDB syntax error (#260)#279
Merged
Merged
Conversation
#260) LikeEscape (added in #232) used `\` as the SQL LIKE escape character, and ScannerService.SearchGymsAsync passed `\` to EF.Functions.Like via `"\\"`. MariaDB's default mode (`NO_BACKSLASH_ESCAPES=OFF`) also treats `\` as a string-literal escape, so an escaped `\` in the pattern (which LikeEscape itself produces for user-supplied backslashes) left the SQL string literal unbalanced and broke gym search with `near ''\')`. Switch the escape character to `|`, which has no special meaning in MariaDB string literals. The LIKE pattern can no longer interact with quote escaping no matter what the user types. Added a `LikeEscape.EscapeChar` constant so callers stay in sync. Tests updated to match the new escape sequences. Reported by @prof-miles0 in #260.
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
Fixes #260 (reported by @prof-miles0).
The gym picker (
GET /api/scanner/gyms?search=...) returned no results because the underlying query was throwing a MariaDB SQL syntax error:near ''\'). The fix from #232 escaped LIKE wildcards (%,_,\) using\as the escape character. That works in pure ANSI SQL, but MariaDB's default mode (NO_BACKSLASH_ESCAPES=OFF) treats\as a string-literal escape too — so any user input containing a backslash (whichLikeEscapeitself doubles to\\) left the SQL string literal unbalanced.Fix
Switch the LIKE escape character from
\to|. The pipe has no special meaning in MariaDB string literals, so it can't interact with quote escaping no matter what the user types.LikeEscape.EscapeCharis now a public constant ("|") so callers can't driftLikeEscape.Escapenow doubles|(to handle user-supplied pipes), prefixes%and_ScannerService.SearchGymsAsyncpassesLikeEscape.EscapeChartoEF.Functions.Likeinstead of the hardcoded"\\"Joe's \"Cafe\"works exactly like any other textTest plan
EscapeLikePatternEscapesWildcardsAndEscapeChartests pass with new expected values)%or_in the term still escapes correctly (literal match, no wildcard)\in the term no longer errors (was the crash; now a no-op character)