Skip to content

Commit

Permalink
Strip leading dot as suggested by @d-maurer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed May 18, 2022
1 parent 3aed3a2 commit 3961778
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Expand Up @@ -28,7 +28,7 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
to the complete matrix view when more than 30 roles are defined.
(`#1039 <https://github.com/zopefoundation/Zope/pull/1039>`_)

- Allow leading ``.`` in cookie domain names.
- Strip leading ``.`` in cookie domain names.
(`#1041 <https://github.com/zopefoundation/Zope/pull/1041>`_)


Expand Down
13 changes: 5 additions & 8 deletions src/ZPublisher/cookie.py
Expand Up @@ -242,14 +242,11 @@ def domain_converter(value):
return value
from encodings.idna import ToASCII
from encodings.idna import nameprep
add_leading_dot = False
if u_value.startswith("."):
u_value = u_value[1:]
add_leading_dot = True
value = ".".join(to_str(ToASCII(nameprep(c))) for c in u_value.split("."))
if add_leading_dot:
value = f".{value}"
return value
# According to https://www.rfc-editor.org/rfc/rfc6265#section-4.1.2.3 a
# leading dot is ignored. If it is there `ToASCII` breaks on the empty
# string:
u_value = u_value.lstrip('.')
return ".".join(to_str(ToASCII(nameprep(c))) for c in u_value.split("."))


registerCookieParameter("Domain", domain_converter)
Expand Down
4 changes: 2 additions & 2 deletions src/ZPublisher/tests/test_cookie.py
Expand Up @@ -145,10 +145,10 @@ def test_domain(self):
_, v = convertCookieParameter("domain",
"Fußball.example".encode())
self.assertEqual(v, "fussball.example")
# allow leading dot according to
# a leading dot is stripped as it is ignored according to
# https://www.rfc-editor.org/rfc/rfc6265#section-4.1.2.3
_, v = convertCookieParameter("domain", ".zope.dev")
self.assertEqual(v, ".zope.dev")
self.assertEqual(v, "zope.dev")

def test_path(self):
# test object
Expand Down

0 comments on commit 3961778

Please sign in to comment.