From a2024352832dfdf54e52aba125ec01a7986e8c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Sat, 14 Oct 2023 20:03:43 +0100 Subject: [PATCH] Skip empty cookie records. Add tests. --- sanic/cookies/request.py | 4 +++- tests/test_cookies.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sanic/cookies/request.py b/sanic/cookies/request.py index 7b23e096..a2705dcf 100644 --- a/sanic/cookies/request.py +++ b/sanic/cookies/request.py @@ -80,8 +80,10 @@ def parse_cookie(raw: str) -> Dict[str, List[str]]: # Support cookies =value or plain value with no name # https://github.com/httpwg/http-extensions/issues/159 if not sep: + if not name: + continue # Empty value like ;; or a cookie header with no value name, value = "", name - + if COOKIE_NAME_RESERVED_CHARS.search(name): # no cov continue diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 547cdd42..03f34544 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -11,6 +11,20 @@ from sanic.cookies.request import CookieRequestParameters from sanic.exceptions import ServerError from sanic.response import text from sanic.response.convenience import json +from sanic.cookies.request import parse_cookie + +def test_request_cookies(): + cdict = parse_cookie("foo=one; foo=two; abc = xyz;;bare;=bare2") + assert cdict == { + "foo": ["one", "two"], + "abc": ["xyz"], + "": ["bare", "bare2"], + } + c = CookieRequestParameters(cdict) + assert c.getlist("foo") == ["one", "two"] + assert c.getlist("abc") == ["xyz"] + assert c.getlist("") == ["bare", "bare2"] + assert c.getlist("bare") == None # [] might be sensible but we got None for now # ------------------------------------------------------------ #