File CVE-2022-0391-urllib_parse-newline-parsing.patch of Package python (Revision 7c7532457b948cc36e86ff51d95885bc)
Currently displaying revision 7c7532457b948cc36e86ff51d95885bc , Show latest
170
1
---
2
Doc/library/urlparse.rst | 14 ++
3
Doc/whatsnew/2.7.rst | 7 +
4
Lib/test/test_urlparse.py | 49 ++++++++++
5
Lib/urlparse.py | 12 ++
6
Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst | 6 +
7
5 files changed, 88 insertions(+)
8
9
--- a/Doc/library/urlparse.rst
10
+++ b/Doc/library/urlparse.rst
11
12
decomposed before parsing, or is not a Unicode string, no error will be
13
raised.
14
15
+ Following the `WHATWG spec`_ that updates RFC 3986, ASCII
16
+ newline ``\n``, ``\r`` and tab ``\t`` characters are stripped
17
+ from the URL.
18
+
19
.. versionadded:: 2.2
20
21
.. versionchanged:: 2.5
22
23
Characters that affect netloc parsing under NFKC normalization will
24
now raise :exc:`ValueError`.
25
26
+ .. versionchanged:: 3.6.14
27
+ ASCII newline and tab characters are stripped from the URL.
28
+
29
+.. _WHATWG spec: https://url.spec.whatwg.org/#concept-basic-url-parser
30
31
.. function:: urlunsplit(parts)
32
33
34
35
.. seealso::
36
37
+ `WHATWG`_ - URL Living standard
38
+ Working Group for the URL Standard that defines URLs,
39
+ domains, IP addresses, the application/x-www-form-urlencoded format,
40
+ and their API.
41
+
42
:rfc:`3986` - Uniform Resource Identifiers
43
This is the current standard (STD66). Any changes to urlparse module
44
should conform to this. Certain deviations could be observed, which are
45
46
:rfc:`1738` - Uniform Resource Locators (URL)
47
This specifies the formal syntax and semantics of absolute URLs.
48
49
+.. _WHATWG: https://url.spec.whatwg.org/
50
51
.. _urlparse-result-object:
52
53
--- a/Doc/whatsnew/2.7.rst
54
+++ b/Doc/whatsnew/2.7.rst
55
56
requiring the old behavior, set a ``trust_server_pasv_ipv4_address``
57
attribute on your FTP instance to ``True``. (See :issue:`43285`)
58
59
+The presence of newline or tab characters in parts of a URL allows for some
60
+forms of attacks. Following the WHATWG specification that updates RFC 3986,
61
+ASCII newline ``\n``, ``\r`` and tab ``\t`` characters are stripped from the
62
+URL by the parser :func:`urlparse` preventing such attacks. The removal
63
+characters are controlled by a new module level variable
64
+``urlparse._UNSAFE_URL_BYTES_TO_REMOVE``. (See :issue:`43882`)
65
+
66
67
Python 3.1 Features
68
=======================
69
--- a/Lib/test/test_urlparse.py
70
+++ b/Lib/test/test_urlparse.py
71
72
p = urlparse.urlsplit(url)
73
self.assertEqual(p.port, None)
74
75
+ def test_urlsplit_remove_unsafe_bytes(self):
76
+ # Remove ASCII tabs and newlines from input, for http common case scenario.
77
+ url = "h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
78
+ p = urlparse.urlsplit(url)
79
+ self.assertEqual(p.scheme, "http")
80
+ self.assertEqual(p.netloc, "www.python.org")
81
+ self.assertEqual(p.path, "/javascript:alert('msg')/")
82
+ self.assertEqual(p.query, "query=something")
83
+ self.assertEqual(p.fragment, "fragment")
84
+ self.assertEqual(p.username, None)
85
+ self.assertEqual(p.password, None)
86
+ self.assertEqual(p.hostname, "www.python.org")
87
+ self.assertEqual(p.port, None)
88
+ self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/?query=something#fragment")
89
+
90
+ # Remove ASCII tabs and newlines from input as bytes, for http common case scenario.
91
+ url = b"h\nttp://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
92
+ p = urlparse.urlsplit(url)
93
+ self.assertEqual(p.scheme, b"http")
94
+ self.assertEqual(p.netloc, b"www.python.org")
95
+ self.assertEqual(p.path, b"/javascript:alert('msg')/")
96
+ self.assertEqual(p.query, b"query=something")
97
+ self.assertEqual(p.fragment, b"fragment")
98
+ self.assertEqual(p.username, None)
99
+ self.assertEqual(p.password, None)
100
+ self.assertEqual(p.hostname, b"www.python.org")
101
+ self.assertEqual(p.port, None)
102
+ self.assertEqual(p.geturl(), b"http://www.python.org/javascript:alert('msg')/?query=something#fragment")
103
+
104
+ # any scheme
105
+ url = "x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
106
+ p = urlparse.urlsplit(url)
107
+ self.assertEqual(p.geturl(), "x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
108
+
109
+ # Remove ASCII tabs and newlines from input as bytes, any scheme.
110
+ url = b"x-new-scheme\t://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
111
+ p = urlparse.urlsplit(url)
112
+ self.assertEqual(p.geturl(), b"x-new-scheme://www.python.org/javascript:alert('msg')/?query=something#fragment")
113
+
114
+ # Unsafe bytes is not returned from urlparse cache.
115
+ # scheme is stored after parsing, sending an scheme with unsafe bytes *will not* return an unsafe scheme
116
+ url = "https://www.python\n.org\t/java\nscript:\talert('msg\r\n')/?query\n=\tsomething#frag\nment"
117
+ scheme = "htt\nps"
118
+ for _ in range(2):
119
+ p = urlparse.urlsplit(url, scheme=scheme)
120
+ self.assertEqual(p.scheme, "https")
121
+ self.assertEqual(p.geturl(), "https://www.python.org/javascript:alert('msg')/?query=something#fragment")
122
+
123
+
124
def test_issue14072(self):
125
p1 = urlparse.urlsplit('tel:+31-641044153')
126
self.assertEqual(p1.scheme, 'tel')
127
--- a/Lib/urlparse.py
128
+++ b/Lib/urlparse.py
129
130
'0123456789'
131
'+-.')
132
133
+# Unsafe bytes to be removed per WHATWG spec
134
+_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
135
+
136
MAX_CACHE_SIZE = 20
137
_parse_cache = {}
138
139
140
"under NFKC normalization"
141
% netloc)
142
143
+
144
+def _remove_unsafe_bytes_from_url(url):
145
+ for b in _UNSAFE_URL_BYTES_TO_REMOVE:
146
+ url = url.replace(b, "")
147
+ return url
148
+
149
+
150
def urlsplit(url, scheme='', allow_fragments=True):
151
"""Parse a URL into 5 components:
152
<scheme>://<netloc>/<path>?<query>#<fragment>
153
Return a 5-tuple: (scheme, netloc, path, query, fragment).
154
Note that we don't break the components up in smaller bits
155
(e.g. netloc is a single string) and we don't expand % escapes."""
156
+ url = _remove_unsafe_bytes_from_url(url)
157
+ scheme = _remove_unsafe_bytes_from_url(scheme)
158
allow_fragments = bool(allow_fragments)
159
key = url, scheme, allow_fragments, type(url), type(scheme)
160
cached = _parse_cache.get(key, None)
161
--- /dev/null
162
+++ b/Misc/NEWS.d/next/Security/2021-04-25-07-46-37.bpo-43882.Jpwx85.rst
163
164
+The presence of newline or tab characters in parts of a URL could allow
165
+some forms of attacks.
166
+
167
+Following the controlling specification for URLs defined by WHATWG
168
+:func:`urlparse` now removes ASCII newlines and tabs from URLs,
169
+preventing such attacks.
170