]> gitweb.factorcode.org Git - factor.git/commitdiff
http.parsers: Allow a lot more characters in the cookie key.
authorDoug Coleman <doug.coleman@gmail.com>
Sun, 27 Dec 2020 02:17:11 +0000 (20:17 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 27 Dec 2020 02:44:30 +0000 (20:44 -0600)
The spec says one thing, but in practice we just disallow ; , and whitespace.

Add more unit tests.

basis/http/parsers/parsers-tests.factor
basis/http/parsers/parsers.factor

index 6be33002fbb1c59bdeb49fe6fccc3f3a4496fdc2..3f4312a72d6b6758bf6b01756fe1ec93397198ef 100644 (file)
@@ -18,3 +18,48 @@ unit-test
 { { T{ cookie { name "__s" } { value "12345567" } } } }
 [ "__s=12345567;" parse-cookie ]
 unit-test
+
+{ { T{ cookie { name "a:b" } { value "c" } } } }
+[ "a:b=c;" parse-cookie ]
+unit-test
+
+{ { T{ cookie { name "d" } { value "[e]" } } } }
+[ "d=[e];" parse-cookie ]
+unit-test
+
+! Don't stop parsing on just one bad cookie
+{
+    {
+        T{ cookie { name "d" } { value "[e]" } }
+        T{ cookie { name "g" } { value "h" } }
+    }
+} [ "d=[e]; a: ; g=h;" parse-cookie ] unit-test
+
+! Don't stop parsing on just one bad cookie
+{
+    {
+        T{ cookie { name "d" } { value "[e]" } }
+        T{ cookie { name "g" } { value "h" } }
+    }
+} [ "d=[e]; a: ; g=h;" parse-cookie ] unit-test
+
+! Add some cookies with extra features
+{
+    V{ "set-cookie" "mykey=myvalue; SameSite=Strict" }
+}
+[ "Set-Cookie: mykey=myvalue; SameSite=Strict" parse-header-line ] unit-test
+
+{
+    V{
+        "set-cookie"
+        "id=a3fWa; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly"
+    }
+}
+[ "Set-Cookie: id=a3fWa; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly" parse-header-line ] unit-test
+
+! python allowed characters in key name
+{
+    { T{ cookie { name "!#$%&'*+-.^_`|~:abc" } { value "def" } } }
+} [
+    "!#$%&'*+-.^_`|~:abc=def;" parse-cookie
+] unit-test
\ No newline at end of file
index df6ccbede9861696e45443f39d34121ad42de206..dab80d9f211bca4f2974f7dd5127ec45cd61227b 100644 (file)
@@ -11,9 +11,15 @@ IN: http.parsers
 : except-these ( quots -- parser )
     [ 1|| ] curry except ; inline
 
+: cookie-key-disallow? ( ch -- ? )
+    " \t,;=" member? ;
+
 : tspecial? ( ch -- ? )
     "()<>@,;:\\\"/[]?={} \t" member? ;
 
+: cookie-key-parser ( -- parser )
+    { [ control? ] [ cookie-key-disallow? ] } except-these repeat1 ;
+
 : token-parser ( -- parser )
     { [ control? ] [ tspecial? ] } except-these repeat1 ;
 
@@ -144,7 +150,7 @@ PEG: parse-header-line ( string -- pair )
     2choice case-sensitive ;
 
 : attr-parser ( -- parser )
-    token-parser case-sensitive ;
+    cookie-key-parser case-sensitive ;
 
 : av-pair-parser ( -- parser )
     [