Skip to content

Commit

Permalink
Strip leading . from cookie domain names. (#1041)
Browse files Browse the repository at this point in the history
* Allow leading `.` in cookie domain names.

* Add PR reference.

* Strip leading dot as suggested by @d-maurer.

* Make linter happy.

* - small cleanups

Co-authored-by: Jens Vagelpohl <[email protected]>
  • Loading branch information
Michael Howitz and dataflake committed May 20, 2022
1 parent c6a8e02 commit bc81ab4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst

- Quote all components of a redirect URL (not only the path component)
(`#1027 <https://github.com/zopefoundation/Zope/issues/1027>`_)

- Drop the convenience script generation from the buildout configuration
in order to get rid of a lot of dependency version pins.
These were only needed for maintainers who can install them manually.
Expand All @@ -32,6 +32,9 @@ 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>`_)

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


5.5.1 (2022-04-05)
------------------
Expand Down
9 changes: 7 additions & 2 deletions src/ZPublisher/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
``normalizeCookieParameterName`` and ``convertCookieParameter``.
"""
import datetime
from encodings.idna import ToASCII
from encodings.idna import nameprep
from itertools import chain
from re import compile
from time import time
Expand Down Expand Up @@ -240,8 +242,11 @@ def domain_converter(value):
u_value = value.decode("utf-8") if isinstance(value, bytes) else value
if "xn--" in u_value: # already encoded
return value
from encodings.idna import ToASCII
from encodings.idna import nameprep

# 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("."))


Expand Down
2 changes: 1 addition & 1 deletion src/ZPublisher/tests/testHTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def test_redirect_nonascii(self):
self._redirectURLCheck(exc, expected=ENC_URL)

def test_redirect_nonascii_everywhere(self):
URL = u"http://uä:pä@sä:80/pä?qä#fä"
URL = "http://uä:pä@sä:80/pä?qä#fä"
ENC_URL = "http://u%C3%A4:p%C3%A4@s%C3%A4:80/p%C3%A4?q%C3%A4#f%C3%A4"
self._redirectURLCheck(URL, ENC_URL)

Expand Down
4 changes: 4 additions & 0 deletions src/ZPublisher/tests/test_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def test_domain(self):
_, v = convertCookieParameter("domain",
"Fußball.example".encode())
self.assertEqual(v, "fussball.example")
# 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")

def test_path(self):
# test object
Expand Down

0 comments on commit bc81ab4

Please sign in to comment.