fix(postgrest): escape reserved characters in array filter values#1126
Open
AndroidPoet wants to merge 1 commit into
Open
fix(postgrest): escape reserved characters in array filter values#1126AndroidPoet wants to merge 1 commit into
AndroidPoet wants to merge 1 commit into
Conversation
Array's PostgrestFilterValue.rawValue joined elements with commas inside
{...} without escaping, so an element containing a comma, brace, quote,
backslash, surrounding whitespace, or the literal NULL corrupted the array
literal — e.g. ["a,b"] became {a,b}, which PostgREST reads as two separate
values. Quote and backslash-escape elements that need it, mirroring the
existing in() filter escaping. Nested array literals ({...}) pass through
unchanged so arrays of arrays keep working.
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.
What
Array'sPostgrestFilterValueconformance builds a PostgreSQL array literal by joining the elements' raw values with commas, with no escaping:So any element that contains a character with structural meaning inside an array literal is silently corrupted. For example, filtering an array/
text[]column with["a,b"]produces{a,b}, which PostgREST parses as the two valuesaandbinstead of the single valuea,b. The same happens for elements containing{,},",\, surrounding whitespace, an empty string, or the literalNULL.This is the array-literal counterpart of the
in()filter escaping fixed in #1061.Fix
Quote and backslash-escape each element that needs it, reusing the same escaping approach as the existing
in()filter helper (added alongside it inHelpers):Elements that are already safe (
admin,9:00, numbers) are emitted verbatim, so existing valid usage is unchanged.Note on nested arrays
An element whose raw value both starts with
{and ends with}is treated as an already-formed nested array literal and passed through unquoted, so arrays of arrays ([[1, 2], [3, 4]]->{{1,2},{3,4}}) keep working. The trade-off is that aStringelement that is literally{...}will not be quoted — this is inherent to the string-basedrawValuedesign, which can't distinguish a nested[Int](whoserawValueis{1,2}) from the string"{1,2}". Elements with partial braces (e.g.a{b) are still quoted.Testing
Added tests covering reserved characters (comma, brace), quote/backslash escaping, whitespace/empty/
NULL, nested array literals, theAnyJSON.arraypath, and that safe/numeric values stay unquoted. They fail onmainand pass with this change.swift test --filter PostgRESTTests— 105 passing (existingin()and snapshot tests unchanged)./scripts/format.sh— clean