Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirections to URLS with host given as IP-litteral #1192

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
- Officially support Python 3.12.1.
(`#1188 <https://github.com/zopefoundation/Zope/issues/1188>`_)

- Fix redirections to URLs with host given as IP-literal with brackets.
Fixes `#1191 <https://github.com/zopefoundation/Zope/issues/1191>`_.


5.9 (2023-11-24)
----------------
Expand Down
14 changes: 7 additions & 7 deletions src/ZPublisher/HTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,24 @@ def redirect(self, location, status=302, lock=0):
# To be entirely correct, we must make sure that all non-ASCII
# characters are quoted correctly.
parsed = list(urlparse(location))
rfc2396_unreserved = "-_.!~*'()" # RFC 2396 section 2.3
rfc3986_unreserved = "-_.!~*'()" # RFC 3986 section 2.3
for idx, idx_safe in (
# authority
(1, ";:@?/&=+$,"), # RFC 2396 section 3.2, 3.2.1, 3.2.3
(1, "[];:@?/&=+$,"), # RFC 3986 section 3.2, 3.2.1, 3.2.3
# path
(2, "/;:@&=+$,"), # RFC 2396 section 3.3
(2, "/;:@&=+$,"), # RFC 3986 section 3.3
# params - actually part of path; empty in Python 3
(3, "/;:@&=+$,"), # RFC 2396 section 3.3
(3, "/;:@&=+$,"), # RFC 3986 section 3.3
# query
(4, ";/?:@&=+,$"), # RFC 2396 section 3.4
(4, ";/?:@&=+,$"), # RFC 3986 section 3.4
# fragment
(5, ";/?:@&=+$,"), # RFC 2396 section 4
(5, ";/?:@&=+$,"), # RFC 3986 section 4
):
# Make a hacky guess whether the component is already
# URL-encoded by checking for %. If it is, we don't touch it.
if '%' not in parsed[idx]:
parsed[idx] = quote(parsed[idx],
safe=rfc2396_unreserved + idx_safe)
safe=rfc3986_unreserved + idx_safe)
location = urlunparse(parsed)

self.setStatus(status, lock=lock)
Expand Down
8 changes: 6 additions & 2 deletions src/ZPublisher/tests/testHTTPResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,15 +802,19 @@ def test_redirect_alreadyquoted(self):
self._redirectURLCheck(ENC_URL)

def test_redirect_unreserved_chars(self):
# RFC 2396 section 2.3, characters that should not be encoded
# RFC 3986 section 2.3, characters that should not be encoded
url = "http://example.com/-_.!~*'()"
self._redirectURLCheck(url)

def test_redirect_reserved_chars(self):
# RFC 2396 section 3.3, characters with reserved meaning in a path
# RFC 3986 section 3.3, characters with reserved meaning in a path
url = 'http://example.com/+/$/;/,/=/?/&/@@index.html'
self._redirectURLCheck(url)

def test_redirect_ipv6(self):
url = "http://[fe80::1ff:fe23:4567:890a]:1234"
self._redirectURLCheck(url)

def test__encode_unicode_no_content_type_uses_default_encoding(self):
UNICODE = '<h1>Tr\u0039s Bien</h1>'
response = self._makeOne()
Expand Down