Skip to content

Commit

Permalink
Merge pull request #1 from citizennet/FBCM-4353/allow-DQUOTEs-in-value
Browse files Browse the repository at this point in the history
FBCM-4353 Allow DQUOTEs in Cookie values
  • Loading branch information
boygao1992 authored Aug 28, 2021
2 parents 9f6ef3f + de06b90 commit b6039b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Biscotti/Cookie/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import Data.Int as Int
import Data.List (List)
import Data.Maybe (Maybe(..))
import Data.String as String
import Text.Parsing.StringParser (ParseError, Parser, fail, runParser)
import Text.Parsing.StringParser.CodePoints (eof, noneOf, string)
import Text.Parsing.StringParser (ParseError, Parser, fail, runParser, try)
import Text.Parsing.StringParser.CodePoints (char, eof, noneOf, string)
import Text.Parsing.StringParser.Combinators (many, sepBy)

type Attribute
Expand Down Expand Up @@ -100,8 +100,15 @@ parseAttribute =
parseSimpleCookie :: Parser Cookie
parseSimpleCookie = do
name <- stringWithout ([ ';', ',', '=' ] <> whitespaceChars) <* string "="
value <- stringWithout ([ ';', ',' ] <> whitespaceChars)
value <- try stringLiteral <|> stringWithout ([ ';', ',' ] <> whitespaceChars)
pure $ Cookie.new name value
where
stringLiteral :: Parser String
stringLiteral = do
_ <- char '"'
x <- stringWithout ['"']
_ <- char '"'
pure ("\"" <> x <> "\"")

parseCookie :: Parser Cookie
parseCookie = do
Expand Down
29 changes: 29 additions & 0 deletions test/Test/Biscotti/CookieTest.purs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ testSuite = do
Cookie.parse "key=val" `shouldEqual` Right expected

suite "parseMany" do
test "parse value wrapped in double-quotes" do
let
expected =
List.fromFoldable
[ Cookie.fromFields
{ name: "key1"
, value: "val1"
, domain: Nothing
, path: Nothing
, expires: Nothing
, maxAge: Nothing
, sameSite: Nothing
, secure: false
, httpOnly: false
}
, Cookie.fromFields
{ name: "key2"
, value: "\"Token 123\""
, domain: Nothing
, path: Nothing
, expires: Nothing
, maxAge: Nothing
, sameSite: Nothing
, secure: false
, httpOnly: false
}
]

Cookie.parseMany "key1=val1; key2=\"Token 123\"" `shouldEqual` Right expected
test "parses multiple name/value pairs only" do
let
expected =
Expand Down

0 comments on commit b6039b8

Please sign in to comment.