+54
-0
lines changedFilter options
+54
-0
lines changed Original file line number Diff line number Diff line change
@@ -311,6 +311,9 @@ or on combining URL components into a URL string.
311
311
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
312
312
decomposed before parsing, no error will be raised.
313
313
314
+
Following the `WHATWG spec`_ that updates RFC 3986, ASCII newline
315
+
``\n``, ``\r`` and tab ``\t`` characters are stripped from the URL.
316
+
314
317
.. versionchanged:: 3.6
315
318
Out-of-range port numbers now raise :exc:`ValueError`, instead of
316
319
returning :const:`None`.
@@ -319,6 +322,10 @@ or on combining URL components into a URL string.
319
322
Characters that affect netloc parsing under NFKC normalization will
320
323
now raise :exc:`ValueError`.
321
324
325
+
.. versionchanged:: 3.9.5
326
+
ASCII newline and tab characters are stripped from the URL.
327
+
328
+
.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
322
329
323
330
.. function:: urlunsplit(parts)
324
331
@@ -673,6 +680,10 @@ task isn't already covered by the URL parsing functions above.
673
680
674
681
.. seealso::
675
682
683
+
`WHATWG`_ - URL Living standard
684
+
Working Group for the URL Standard that defines URLs, domains, IP addresses, the
685
+
application/x-www-form-urlencoded format, and their API.
686
+
676
687
:rfc:`3986` - Uniform Resource Identifiers
677
688
This is the current standard (STD66). Any changes to urllib.parse module
678
689
should conform to this. Certain deviations could be observed, which are
@@ -696,3 +707,5 @@ task isn't already covered by the URL parsing functions above.
696
707
697
708
:rfc:`1738` - Uniform Resource Locators (URL)
698
709
This specifies the formal syntax and semantics of absolute URLs.
710
+
711
+
.. _WHATWG: https://url.spec.whatwg.org/
Original file line number Diff line number Diff line change
@@ -612,6 +612,35 @@ def test_urlsplit_attributes(self):
612
612
with self.assertRaisesRegex(ValueError, "out of range"):
613
613
p.port
614
614
615
+
def test_urlsplit_remove_unsafe_bytes(self):
616
+
# Remove ASCII tabs and newlines from input
617
+
url = "http://www.python.org/java\nscript:\talert('msg\r\n')/#frag"
618
+
p = urllib.parse.urlsplit(url)
619
+
self.assertEqual(p.scheme, "http")
620
+
self.assertEqual(p.netloc, "www.python.org")
621
+
self.assertEqual(p.path, "/javascript:alert('msg')/")
622
+
self.assertEqual(p.query, "")
623
+
self.assertEqual(p.fragment, "frag")
624
+
self.assertEqual(p.username, None)
625
+
self.assertEqual(p.password, None)
626
+
self.assertEqual(p.hostname, "www.python.org")
627
+
self.assertEqual(p.port, None)
628
+
self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/#frag")
629
+
630
+
# Remove ASCII tabs and newlines from input as bytes.
631
+
url = b"http://www.python.org/java\nscript:\talert('msg\r\n')/#frag"
632
+
p = urllib.parse.urlsplit(url)
633
+
self.assertEqual(p.scheme, b"http")
634
+
self.assertEqual(p.netloc, b"www.python.org")
635
+
self.assertEqual(p.path, b"/javascript:alert('msg')/")
636
+
self.assertEqual(p.query, b"")
637
+
self.assertEqual(p.fragment, b"frag")
638
+
self.assertEqual(p.username, None)
639
+
self.assertEqual(p.password, None)
640
+
self.assertEqual(p.hostname, b"www.python.org")
641
+
self.assertEqual(p.port, None)
642
+
self.assertEqual(p.geturl(), b"http://www.python.org/javascript:alert('msg')/#frag")
643
+
615
644
def test_attributes_bad_port(self):
616
645
"""Check handling of invalid ports."""
617
646
for bytes in (False, True):
Original file line number Diff line number Diff line change
@@ -78,6 +78,9 @@
78
78
'0123456789'
79
79
'+-.')
80
80
81
+
# Unsafe bytes to be removed per WHATWG spec
82
+
_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
83
+
81
84
# XXX: Consider replacing with functools.lru_cache
82
85
MAX_CACHE_SIZE = 20
83
86
_parse_cache = {}
@@ -469,6 +472,9 @@ def urlsplit(url, scheme='', allow_fragments=True):
469
472
else:
470
473
scheme, url = url[:i].lower(), url[i+1:]
471
474
475
+
for b in _UNSAFE_URL_BYTES_TO_REMOVE:
476
+
url = url.replace(b, "")
477
+
472
478
if url[:2] == '//':
473
479
netloc, url = _splitnetloc(url, 2)
474
480
if (('[' in netloc and ']' not in netloc) or
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1
+
The presence of newline or tab characters in parts of a URL could allow
2
+
some forms of attacks.
3
+
4
+
Following the controlling specification for URLs defined by WHATWG
5
+
:func:`urllib.parse` now removes ASCII newlines and tabs from URLs,
6
+
preventing such attacks.
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4