|
|
75b047 |
From 6c472d3a1d334d4eeb4a25eba7bf3b01611bf667 Mon Sep 17 00:00:00 2001
|
|
|
75b047 |
From: "Miss Islington (bot)"
|
|
|
75b047 |
<31488909+miss-islington@users.noreply.github.com>
|
|
|
75b047 |
Date: Thu, 6 May 2021 09:56:01 -0700
|
|
|
75b047 |
Subject: [PATCH] [3.6] bpo-43882 - urllib.parse should sanitize urls
|
|
|
75b047 |
containing ASCII newline and tabs (GH-25924)
|
|
|
75b047 |
|
|
|
75b047 |
Co-authored-by: Gregory P. Smith <greg@krypto.org>
|
|
|
75b047 |
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
|
|
|
75b047 |
(cherry picked from commit 76cd81d60310d65d01f9d7b48a8985d8ab89c8b4)
|
|
|
75b047 |
Co-authored-by: Senthil Kumaran <senthil@uthcode.com>
|
|
|
75b047 |
(cherry picked from commit 515a7bc4e13645d0945b46a8e1d9102b918cd407)
|
|
|
75b047 |
|
|
|
75b047 |
Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
|
|
|
75b047 |
---
|
|
|
75b047 |
Doc/library/urllib.parse.rst | 13 +++++
|
|
|
75b047 |
Lib/test/test_urlparse.py | 48 +++++++++++++++++++
|
|
|
75b047 |
Lib/urllib/parse.py | 10 ++++
|
|
|
75b047 |
.../2021-04-25-07-46-37.bpo-43882.Jpwx85.rst | 6 +++
|
|
|
75b047 |
4 files changed, 77 insertions(+)
|
|
|
75b047 |
create mode 100644 Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
|
|
|
75b047 |
|
|
|
75b047 |
diff --git a/Doc/library/urllib.parse.rst b/Doc/library/urllib.parse.rst
|
|
|
75b047 |
index 3c2e37ef2093a..b717d7cc05b2e 100644
|
|
|
75b047 |
--- a/Doc/library/urllib.parse.rst
|
|
|
75b047 |
+++ b/Doc/library/urllib.parse.rst
|
|
|
75b047 |
@@ -288,6 +288,9 @@ or on combining URL components into a URL string.
|
|
|
75b047 |
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
|
|
|
75b047 |
decomposed before parsing, no error will be raised.
|
|
|
75b047 |
|
|
|
75b047 |
+ Following the `WHATWG spec`_ that updates RFC 3986, ASCII newline
|
|
|
75b047 |
+ ``\n``, ``\r`` and tab ``\t`` characters are stripped from the URL.
|
|
|
75b047 |
+
|
|
|
75b047 |
.. versionchanged:: 3.6
|
|
|
75b047 |
Out-of-range port numbers now raise :exc:`ValueError`, instead of
|
|
|
75b047 |
returning :const:`None`.
|
|
|
75b047 |
@@ -296,6 +299,10 @@ or on combining URL components into a URL string.
|
|
|
75b047 |
Characters that affect netloc parsing under NFKC normalization will
|
|
|
75b047 |
now raise :exc:`ValueError`.
|
|
|
75b047 |
|
|
|
75b047 |
+ .. versionchanged:: 3.6.14
|
|
|
75b047 |
+ ASCII newline and tab characters are stripped from the URL.
|
|
|
75b047 |
+
|
|
|
75b047 |
+.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
|
|
|
75b047 |
|
|
|
75b047 |
.. function:: urlunsplit(parts)
|
|
|
75b047 |
|
|
|
75b047 |
@@ -633,6 +640,10 @@ task isn't already covered by the URL parsing functions above.
|
|
|
75b047 |
|
|
|
75b047 |
.. seealso::
|
|
|
75b047 |
|
|
|
75b047 |
+ `WHATWG`_ - URL Living standard
|
|
|
75b047 |
+ Working Group for the URL Standard that defines URLs, domains, IP addresses, the
|
|
|
75b047 |
+ application/x-www-form-urlencoded format, and their API.
|
|
|
75b047 |
+
|
|
|
75b047 |
:rfc:`3986` - Uniform Resource Identifiers
|
|
|
75b047 |
This is the current standard (STD66). Any changes to urllib.parse module
|
|
|
75b047 |
should conform to this. Certain deviations could be observed, which are
|
|
|
75b047 |
@@ -656,3 +667,5 @@ task isn't already covered by the URL parsing functions above.
|
|
|
75b047 |
|
|
|
75b047 |
:rfc:`1738` - Uniform Resource Locators (URL)
|
|
|
75b047 |
This specifies the formal syntax and semantics of absolute URLs.
|
|
|
75b047 |
+
|
|
|
75b047 |
+.. _WHATWG: https://url.spec.whatwg.org/
|
|
|
75b047 |
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
|
|
|
75b047 |
index e3088b2f39bd7..3509278a01694 100644
|
|
|
75b047 |
--- a/Lib/test/test_urlparse.py
|
|
|
75b047 |
+++ b/Lib/test/test_urlparse.py
|
|
|
75b047 |
@@ -612,6 +612,54 @@ def test_urlsplit_attributes(self):
|
|
|
75b047 |
with self.assertRaisesRegex(ValueError, "out of range"):
|
|
|
75b047 |
p.port
|
|
|
75b047 |
|
|
|
75b047 |
+ def test_urlsplit_remove_unsafe_bytes(self):
|
|
|
75b047 |
+ # Remove ASCII tabs and newlines from input, for http common case scenario.
|
|
|
75b047 |
+ url = "h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
|
|
|
75b047 |
+ p = urllib.parse.urlsplit(url)
|
|
|
75b047 |
+ self.assertEqual(p.scheme, "http")
|
|
|
75b047 |
+ self.assertEqual(p.netloc, "www.python.org")
|
|
|
75b047 |
+ self.assertEqual(p.path, "/javascript:alert('msg')/")
|
|
|
75b047 |
+ self.assertEqual(p.query, "query=something")
|
|
|
75b047 |
+ self.assertEqual(p.fragment, "fragment")
|
|
|
75b047 |
+ self.assertEqual(p.username, None)
|
|
|
75b047 |
+ self.assertEqual(p.password, None)
|
|
|
75b047 |
+ self.assertEqual(p.hostname, "www.python.org")
|
|
|
75b047 |
+ self.assertEqual(p.port, None)
|
|
|
75b047 |
+ self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/?query=something#fragment")
|
|
|
75b047 |
+
|
|
|
75b047 |
+ # Remove ASCII tabs and newlines from input as bytes, for http common case scenario.
|
|
|
75b047 |
+ url = b"h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
|
|
|
75b047 |
+ p = urllib.parse.urlsplit(url)
|
|
|
75b047 |
+ self.assertEqual(p.scheme, b"http")
|
|
|
75b047 |
+ self.assertEqual(p.netloc, b"www.python.org")
|
|
|
75b047 |
+ self.assertEqual(p.path, b"/javascript:alert('msg')/")
|
|
|
75b047 |
+ self.assertEqual(p.query, b"query=something")
|
|
|
75b047 |
+ self.assertEqual(p.fragment, b"fragment")
|
|
|
75b047 |
+ self.assertEqual(p.username, None)
|
|
|
75b047 |
+ self.assertEqual(p.password, None)
|
|
|
75b047 |
+ self.assertEqual(p.hostname, b"www.python.org")
|
|
|
75b047 |
+ self.assertEqual(p.port, None)
|
|
|
75b047 |
+ self.assertEqual(p.geturl(), b"http://www.python.org/javascript:alert('msg')/?query=something#fragment")
|
|
|
75b047 |
+
|
|
|
75b047 |
+ # any scheme
|
|
|
75b047 |
+ url = "x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
|
|
|
75b047 |
+ p = urllib.parse.urlsplit(url)
|
|
|
75b047 |
+ self.assertEqual(p.geturl(), "x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
|
|
|
75b047 |
+
|
|
|
75b047 |
+ # Remove ASCII tabs and newlines from input as bytes, any scheme.
|
|
|
75b047 |
+ url = b"x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
|
|
|
75b047 |
+ p = urllib.parse.urlsplit(url)
|
|
|
75b047 |
+ self.assertEqual(p.geturl(), b"x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
|
|
|
75b047 |
+
|
|
|
75b047 |
+ # Unsafe bytes is not returned from urlparse cache.
|
|
|
75b047 |
+ # scheme is stored after parsing, sending an scheme with unsafe bytes *will not* return an unsafe scheme
|
|
|
75b047 |
+ url = "https://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
|
|
|
75b047 |
+ scheme = "htt\nps"
|
|
|
75b047 |
+ for _ in range(2):
|
|
|
75b047 |
+ p = urllib.parse.urlsplit(url, scheme=scheme)
|
|
|
75b047 |
+ self.assertEqual(p.scheme, "https")
|
|
|
75b047 |
+ self.assertEqual(p.geturl(), "https://www.python.org/javascript:alert('msg')/?query=something#fragment")
|
|
|
75b047 |
+
|
|
|
75b047 |
def test_attributes_bad_port(self):
|
|
|
75b047 |
"""Check handling of invalid ports."""
|
|
|
75b047 |
for bytes in (False, True):
|
|
|
75b047 |
diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py
|
|
|
75b047 |
index 66056bf589bf6..ac6e7a9cee0b9 100644
|
|
|
75b047 |
--- a/Lib/urllib/parse.py
|
|
|
75b047 |
+++ b/Lib/urllib/parse.py
|
|
|
75b047 |
@@ -76,6 +76,9 @@
|
|
|
75b047 |
'0123456789'
|
|
|
75b047 |
'+-.')
|
|
|
75b047 |
|
|
|
75b047 |
+# Unsafe bytes to be removed per WHATWG spec
|
|
|
75b047 |
+_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
|
|
|
75b047 |
+
|
|
|
75b047 |
# XXX: Consider replacing with functools.lru_cache
|
|
|
75b047 |
MAX_CACHE_SIZE = 20
|
|
|
75b047 |
_parse_cache = {}
|
|
|
75b047 |
@@ -409,6 +412,11 @@ def _checknetloc(netloc):
|
|
|
75b047 |
raise ValueError("netloc '" + netloc + "' contains invalid " +
|
|
|
75b047 |
"characters under NFKC normalization")
|
|
|
75b047 |
|
|
|
75b047 |
+def _remove_unsafe_bytes_from_url(url):
|
|
|
75b047 |
+ for b in _UNSAFE_URL_BYTES_TO_REMOVE:
|
|
|
75b047 |
+ url = url.replace(b, "")
|
|
|
75b047 |
+ return url
|
|
|
75b047 |
+
|
|
|
75b047 |
def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
75b047 |
"""Parse a URL into 5 components:
|
|
|
75b047 |
<scheme>://<netloc>/<path>?<query>#<fragment>
|
|
|
75b047 |
@@ -416,6 +424,8 @@ def urlsplit(url, scheme='', allow_fragments=True):
|
|
|
75b047 |
Note that we don't break the components up in smaller bits
|
|
|
75b047 |
(e.g. netloc is a single string) and we don't expand % escapes."""
|
|
|
75b047 |
url, scheme, _coerce_result = _coerce_args(url, scheme)
|
|
|
75b047 |
+ url = _remove_unsafe_bytes_from_url(url)
|
|
|
75b047 |
+ scheme = _remove_unsafe_bytes_from_url(scheme)
|
|
|
75b047 |
allow_fragments = bool(allow_fragments)
|
|
|
75b047 |
key = url, scheme, allow_fragments, type(url), type(scheme)
|
|
|
75b047 |
cached = _parse_cache.get(key, None)
|
|
|
75b047 |
diff --git a/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst b/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
|
|
|
75b047 |
new file mode 100644
|
|
|
75b047 |
index 0000000000000..a326d079dff4a
|
|
|
75b047 |
--- /dev/null
|
|
|
75b047 |
+++ b/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
|
|
|
75b047 |
@@ -0,0 +1,6 @@
|
|
|
75b047 |
+The presence of newline or tab characters in parts of a URL could allow
|
|
|
75b047 |
+some forms of attacks.
|
|
|
75b047 |
+
|
|
|
75b047 |
+Following the controlling specification for URLs defined by WHATWG
|
|
|
75b047 |
+:func:`urllib.parse` now removes ASCII newlines and tabs from URLs,
|
|
|
75b047 |
+preventing such attacks.
|