Skip to content

Commit

Permalink
feat: Allow backslash escaping inside double quotes
Browse files Browse the repository at this point in the history
Examples:

- ?col=in.("Double\"Quote")
- ?col=in.("Back\\slash")

Backslashed chars get passed as is

- ?col=in.("\a\b\c") = ?col=in.(abc)
  • Loading branch information
steve-chavez committed Sep 14, 2021
1 parent 4a594d5 commit 72a28a1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
+ Enables calling a function with a single json parameter without using `Prefer: params=single-object`
+ Enables uploading bytea to a function with `Content-Type: application/octet-stream`
+ Enables uploading raw text to a function with `Content-Type: text/plain`
- #1938, Allow escaping inside double quotes with a backslash, e.g. `?col=in.("Double\"Quote")`, `?col=in.("Back\\slash")` - @steve-chavez

### Fixed

Expand Down
4 changes: 3 additions & 1 deletion src/PostgREST/Request/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ pListElement :: Parser Text
pListElement = try (pQuotedValue <* notFollowedBy (noneOf ",)")) <|> (toS <$> many (noneOf ",)"))

pQuotedValue :: Parser Text
pQuotedValue = toS <$> (char '"' *> many (noneOf "\"") <* char '"')
pQuotedValue = toS <$> (char '"' *> many pCharsOrSlashed <* char '"')
where
pCharsOrSlashed = noneOf "\\\"" <|> (char '\\' *> anyChar)

pDelimiter :: Parser Char
pDelimiter = char '.' <?> "delimiter (.)"
Expand Down
19 changes: 19 additions & 0 deletions test/Feature/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,25 @@ spec actualPgVersion = do
[json| [{"name":"Double O Seven(007)"}] |]
{ matchHeaders = [matchContentTypeJson] }

context "escaped chars" $ do
it "accepts escaped double quotes" $
get "/w_or_wo_comma_names?name=in.(\"Double\\\"Quote\\\"McGraw\\\"\")" `shouldRespondWith`
[json| [ { "name": "Double\"Quote\"McGraw\"" } ] |]
{ matchHeaders = [matchContentTypeJson] }

it "accepts escaped backslashes" $ do
get "/w_or_wo_comma_names?name=in.(\"\\\\\")" `shouldRespondWith`
[json| [{ "name": "\\" }] |]
{ matchHeaders = [matchContentTypeJson] }
get "/w_or_wo_comma_names?name=in.(\"/\\\\Slash/\\\\Beast/\\\\\")" `shouldRespondWith`
[json| [ { "name": "/\\Slash/\\Beast/\\" } ] |]
{ matchHeaders = [matchContentTypeJson] }

it "passes any escaped char as the same char" $
get "/w_or_wo_comma_names?name=in.(\"D\\a\\vid W\\h\\ite\")" `shouldRespondWith`
[json| [{ "name": "David White" }] |]
{ matchHeaders = [matchContentTypeJson] }

describe "IN values without quotes" $ do
it "accepts single double quotes as values" $ do
get "/w_or_wo_comma_names?name=in.(\")" `shouldRespondWith`
Expand Down

0 comments on commit 72a28a1

Please sign in to comment.