+54
-0
lines changedFilter options
+54
-0
lines changed Original file line number Diff line number Diff line change
@@ -312,6 +312,9 @@ or on combining URL components into a URL string.
312
312
``#``, ``@``, or ``:`` will raise a :exc:`ValueError`. If the URL is
313
313
decomposed before parsing, no error will be raised.
314
314
315
+
Following the `WHATWG spec`_ that updates RFC 3986, ASCII newline
316
+
``\n``, ``\r`` and tab ``\t`` characters are stripped from the URL.
317
+
315
318
.. versionchanged:: 3.6
316
319
Out-of-range port numbers now raise :exc:`ValueError`, instead of
317
320
returning :const:`None`.
@@ -320,6 +323,10 @@ or on combining URL components into a URL string.
320
323
Characters that affect netloc parsing under NFKC normalization will
321
324
now raise :exc:`ValueError`.
322
325
326
+
.. versionchanged:: 3.10
327
+
ASCII newline and tab characters are stripped from the URL.
328
+
329
+
.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
323
330
324
331
.. function:: urlunsplit(parts)
325
332
@@ -674,6 +681,10 @@ task isn't already covered by the URL parsing functions above.
674
681
675
682
.. seealso::
676
683
684
+
`WHATWG`_ - URL Living standard
685
+
Working Group for the URL Standard that defines URLs, domains, IP addresses, the
686
+
application/x-www-form-urlencoded format, and their API.
687
+
677
688
:rfc:`3986` - Uniform Resource Identifiers
678
689
This is the current standard (STD66). Any changes to urllib.parse module
679
690
should conform to this. Certain deviations could be observed, which are
@@ -697,3 +708,5 @@ task isn't already covered by the URL parsing functions above.
697
708
698
709
:rfc:`1738` - Uniform Resource Locators (URL)
699
710
This specifies the formal syntax and semantics of absolute URLs.
711
+
712
+
.. _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