-
Notifications
You must be signed in to change notification settings - Fork 42
Escape candidates search filters #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,18 @@ export function sanitizeSearchParams( | |
| /** | ||
| * Escape user text before interpolating it into a PostgREST filter string. | ||
| * PostgREST uses punctuation such as commas, periods, and parentheses as | ||
| * filter syntax, while SQL LIKE treats % and _ as wildcards. | ||
| * filter syntax, while SQL LIKE treats % and _ as wildcards. PostgREST also | ||
| * accepts * as a % alias in like/ilike filters. | ||
| */ | ||
| export function escapePostgrestSearchValue(value: string): string { | ||
| return value.replace(/[\\%_,().]/g, (char) => `\\${char}`); | ||
| return value.replace(/[\\%*_,().]/g, (char) => `\\${char}`); | ||
| } | ||
|
|
||
| /** | ||
| * Escape user text before placing it inside a quoted PostgREST array literal. | ||
| * Array literals add braces and quotes as syntax, so escape those in addition | ||
| * to the search punctuation handled above. | ||
| */ | ||
| export function escapePostgrestArrayLiteralValue(value: string): string { | ||
| return escapePostgrestSearchValue(value).replace(/["{}]/g, (char) => `\\${char}`); | ||
| } | ||
|
Comment on lines
+61
to
63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new function is exported from Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
escapePostgrestArrayLiteralValuedelegates first toescapePostgrestSearchValue, which escapes%,_, and*as LIKE wildcard characters. Those characters have no special meaning inside a PostgreSQL double-quoted array element used with thecs(contains) operator — PostgreSQL stores them literally and matches them by equality. Escaping them inserts a literal backslash into the stored comparison value, so a tag named100%offwould be queried as100\%offand silently fail to match any row. Only"and\need to be escaped for the PostgreSQL array literal layer;,,.,(,),%,_,*are PostgREST filter-syntax and LIKE concerns that don't apply to thecsvalue.