From 8c0d187044c94f2b529a2ff7a67d60be7921abfd Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Thu, 27 Oct 2022 11:05:03 +0200 Subject: [PATCH] fix: Stricter query string parsing - no hints or join types in regular fields Fixes #2362 Reference #2475 Signed-off-by: Wolfgang Walther --- CHANGELOG.md | 4 +++- src/PostgREST/ApiRequest/QueryParams.hs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5e77f789..2da11aaa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,12 +22,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #2343, Return status code 200 for PATCH requests which don't affect any rows - @wolfgangwalther - #2481, Treat computed relationships not marked SETOF as M2O/O2O relationship - @wolfgangwalther - #2534, Fix embedding a computed relationship with a normal relationship - @steve-chavez + - #2362, Fix error message when [] is used inside select - @wolfgangwalther + - #2475, Disallow !inner on computed columns - @wolfgangwalther ### Changed - #2444, Removed `db-pool-timeout` option, because this was removed upstream in hasql-pool. - @robx - #2343, PATCH requests that don't affect any rows no longer return 404 - @wolfgangwalther - - #2537, Stricter parsing of query string. Instead of silently ignoring, the parser now throws on invalid syntax like json paths for embeddings etc. - @wolfgangwalther + - #2537, Stricter parsing of query string. Instead of silently ignoring, the parser now throws on invalid syntax like json paths for embeddings, hints for regular columns, etc. - @wolfgangwalther ### Deprecated diff --git a/src/PostgREST/ApiRequest/QueryParams.hs b/src/PostgREST/ApiRequest/QueryParams.hs index 6d9919843..c3f20c6c0 100644 --- a/src/PostgREST/ApiRequest/QueryParams.hs +++ b/src/PostgREST/ApiRequest/QueryParams.hs @@ -313,6 +313,11 @@ pTreePath = do -- -- >>> P.parse pFieldForest "" "*,client(*,nested(*))" -- Right [Node {rootLabel = SelectField {selField = ("*",[]), selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SelectRelation {selRelation = "client", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selCast = Nothing, selAlias = Nothing}, subForest = []},Node {rootLabel = SelectRelation {selRelation = "nested", selAlias = Nothing, selHint = Nothing, selJoinType = Nothing}, subForest = [Node {rootLabel = SelectField {selField = ("*",[]), selCast = Nothing, selAlias = Nothing}, subForest = []}]}]}] +-- +-- >>> P.parse pFieldForest "" "id,clients(name[])" +-- Left (line 1, column 16): +-- unexpected '[' +-- expecting letter, digit, "-", "!", "(", "->>", "->", "::", ")", "," or end of input pFieldForest :: Parser [Tree SelectItem] pFieldForest = pFieldTree `sepBy1` lexeme (char ',') where @@ -451,6 +456,16 @@ pRelationSelect = lexeme $ try ( do -- -- >>> P.parse pFieldSelect "" "*" -- Right (SelectField {selField = ("*",[]), selCast = Nothing, selAlias = Nothing}) +-- +-- >>> P.parse pFieldSelect "" "name!hint" +-- Left (line 1, column 5): +-- unexpected '!' +-- expecting letter, digit, "-", "->>", "->", "::", ")", "," or end of input +-- +-- >>> P.parse pFieldSelect "" "*!hint" +-- Left (line 1, column 2): +-- unexpected '!' +-- expecting ")", "," or end of input pFieldSelect :: Parser SelectItem pFieldSelect = lexeme $ try ( @@ -458,11 +473,17 @@ pFieldSelect = lexeme $ alias <- optionMaybe ( try(pFieldName <* aliasSeparator) ) fld <- pField cast' <- optionMaybe (string "::" *> many pIdentifierChar) + pEnd return $ SelectField fld (toS <$> cast') alias ) <|> do s <- pStar + pEnd return $ SelectField (s, []) Nothing Nothing + where + pEnd = try (void $ lookAhead (string ")")) <|> + try (void $ lookAhead (string ",")) <|> + try eof pOpExpr :: Parser SingleVal -> Parser OpExpr pOpExpr pSVal = try ( string "not" *> pDelimiter *> (OpExpr True <$> pOperation)) <|> OpExpr False <$> pOperation