Skip to content

Commit

Permalink
cookie sets Path=/ by default again (#2673)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Apr 28, 2023
2 parents 5c32263 + 18616d2 commit 447d4a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Unreleased
- Parse the cookie ``Expires`` attribute correctly in the test client. :issue:`2669`
- ``max_content_length`` can only be enforced on streaming requests if the server
sets ``wsgi.input_terminated``. :issue:`2668`
- The cookie ``Path`` attribute is set to ``/`` by default again, to prevent clients
from falling back to RFC 6265's ``default-path`` behavior. :issue:`2672`


Version 2.3.1
Expand Down
5 changes: 4 additions & 1 deletion src/werkzeug/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ def dump_cookie(
value: str = "",
max_age: timedelta | int | None = None,
expires: str | datetime | int | float | None = None,
path: str | None = None,
path: str | None = "/",
domain: str | None = None,
secure: bool = False,
httponly: bool = False,
Expand Down Expand Up @@ -1388,6 +1388,9 @@ def dump_cookie(
.. _`cookie`: http://browsercookielimits.squawky.net/
.. versionchanged:: 2.3.1
The ``path`` parameter is ``/`` by default.
.. versionchanged:: 2.3
``localhost`` and other names without a dot are allowed for the domain. A
leading dot is ignored.
Expand Down
25 changes: 13 additions & 12 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,11 @@ def test_dump_cookie(self):
assert set(rv.split("; ")) == {
"HttpOnly",
"Max-Age=360",
"Path=/",
'foo="bar baz blub"',
}
assert http.dump_cookie("key", "xxx/") == "key=xxx/"
assert http.dump_cookie("key", "xxx=") == "key=xxx="
assert http.dump_cookie("key", "xxx/") == "key=xxx/; Path=/"
assert http.dump_cookie("key", "xxx=", path=None) == "key=xxx="

def test_bad_cookies(self):
cookies = http.parse_cookie(
Expand All @@ -478,7 +479,7 @@ def test_empty_keys_are_ignored(self):

def test_cookie_quoting(self):
val = http.dump_cookie("foo", "?foo")
assert val == "foo=?foo"
assert val == "foo=?foo; Path=/"
assert http.parse_cookie(val)["foo"] == "?foo"
assert http.parse_cookie(r'foo="foo\054bar"')["foo"] == "foo,bar"

Expand All @@ -488,21 +489,21 @@ def test_parse_set_cookie_directive(self):

def test_cookie_domain_resolving(self):
val = http.dump_cookie("foo", "bar", domain="\N{SNOWMAN}.com")
assert val == "foo=bar; Domain=xn--n3h.com"
assert val == "foo=bar; Domain=xn--n3h.com; Path=/"

def test_cookie_unicode_dumping(self):
val = http.dump_cookie("foo", "\N{SNOWMAN}")
h = datastructures.Headers()
h.add("Set-Cookie", val)
assert h["Set-Cookie"] == 'foo="\\342\\230\\203"'
assert h["Set-Cookie"] == 'foo="\\342\\230\\203"; Path=/'

cookies = http.parse_cookie(h["Set-Cookie"])
assert cookies["foo"] == "\N{SNOWMAN}"

def test_cookie_unicode_keys(self):
# Yes, this is technically against the spec but happens
val = http.dump_cookie("fö", "fö")
assert val == _wsgi_encoding_dance('fö="f\\303\\266"', "utf-8")
assert val == _wsgi_encoding_dance('fö="f\\303\\266"; Path=/')
cookies = http.parse_cookie(val)
assert cookies["fö"] == "fö"

Expand All @@ -513,20 +514,20 @@ def test_cookie_unicode_parsing(self):

def test_cookie_domain_encoding(self):
val = http.dump_cookie("foo", "bar", domain="\N{SNOWMAN}.com")
assert val == "foo=bar; Domain=xn--n3h.com"
assert val == "foo=bar; Domain=xn--n3h.com; Path=/"

val = http.dump_cookie("foo", "bar", domain="foo.com")
assert val == "foo=bar; Domain=foo.com"
assert val == "foo=bar; Domain=foo.com; Path=/"

def test_cookie_maxsize(self):
val = http.dump_cookie("foo", "bar" * 1363)
val = http.dump_cookie("foo", "bar" * 1360 + "b")
assert len(val) == 4093

with pytest.warns(UserWarning, match="cookie is too large"):
http.dump_cookie("foo", "bar" * 1364)
http.dump_cookie("foo", "bar" * 1360 + "ba")

with pytest.warns(UserWarning, match="the limit is 512 bytes"):
http.dump_cookie("foo", "w" * 509, max_size=512)
http.dump_cookie("foo", "w" * 501, max_size=512)

@pytest.mark.parametrize(
("samesite", "expected"),
Expand All @@ -538,7 +539,7 @@ def test_cookie_maxsize(self):
),
)
def test_cookie_samesite_attribute(self, samesite, expected):
value = http.dump_cookie("foo", "bar", samesite=samesite)
value = http.dump_cookie("foo", "bar", samesite=samesite, path=None)
assert value == expected

def test_cookie_samesite_invalid(self):
Expand Down

0 comments on commit 447d4a8

Please sign in to comment.