Repository navigation
Expand file tree
/
Copy pathCVE-2022-0391.diff
More file actions
121 lines (110 loc) · 4.46 KB
/
Copy pathCVE-2022-0391.diff
File metadata and controls
121 lines (110 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
From 8c70fb47fee85eb8ea926aa3a2ae57a11c04b006 Mon Sep 17 00:00:00 2001
From: Jeremy Paige <ucodery@gmail.com>
Date: Thu, 23 Jun 2022 13:34:54 -0700
Subject: [PATCH 1/2] Address CVE-2022-0391 for urlparse
---
Lib/test/test_urlparse.py | 30 ++++++++++++++++++++++++++++++
Lib/urlparse.py | 8 ++++++++
2 files changed, 38 insertions(+)
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
index 3c7fc9217f57..9bee4a05d2b2 100644
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -569,6 +569,36 @@ def test_telurl_params(self):
self.assertEqual(p1.params, 'phone-context=+1-914-555')
+ def test_urlsplit_remove_unsafe_bytes(self):
+ # Remove ASCII tabs and newlines from input
+ url = "http://www.python.org/java\nscript:\talert('msg\r\n')/#frag"
+ p = urlparse.urlsplit(url)
+ self.assertEqual(p.scheme, "http")
+ self.assertEqual(p.netloc, "www.python.org")
+ self.assertEqual(p.path, "/javascript:alert('msg')/")
+ self.assertEqual(p.query, "")
+ self.assertEqual(p.fragment, "frag")
+ self.assertEqual(p.username, None)
+ self.assertEqual(p.password, None)
+ self.assertEqual(p.hostname, "www.python.org")
+ self.assertEqual(p.port, None)
+ self.assertEqual(p.geturl(), "http://www.python.org/javascript:alert('msg')/#frag")
+
+ # Remove ASCII tabs and newlines from input as unicode.
+ url = u"http://www.python.org/java\nscript:\talert('msg\r\n')/#frag"
+ p = urlparse.urlsplit(url)
+ self.assertEqual(p.scheme, u"http")
+ self.assertEqual(p.netloc, u"www.python.org")
+ self.assertEqual(p.path, u"/javascript:alert('msg')/")
+ self.assertEqual(p.query, u"")
+ self.assertEqual(p.fragment, u"frag")
+ self.assertEqual(p.username, None)
+ self.assertEqual(p.password, None)
+ self.assertEqual(p.hostname, u"www.python.org")
+ self.assertEqual(p.port, None)
+ self.assertEqual(p.geturl(), u"http://www.python.org/javascript:alert('msg')/#frag")
+
+
def test_attributes_bad_port(self):
"""Check handling of non-integer ports."""
p = urlparse.urlsplit("http://www.example.net:foo")
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
index 316dc3c6ebec..173df1ed18eb 100644
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -62,6 +62,9 @@
'0123456789'
'+-.')
+# Unsafe bytes to be removed per WHATWG spec
+_UNSAFE_URL_BYTES_TO_REMOVE = ['\t', '\r', '\n']
+
MAX_CACHE_SIZE = 20
_parse_cache = {}
@@ -203,6 +206,8 @@ def urlsplit(url, scheme='', allow_fragments=True):
if url[:i] == 'http': # optimize the common case
scheme = url[:i].lower()
url = url[i+1:]
+ for b in _UNSAFE_URL_BYTES_TO_REMOVE:
+ url = url.replace(b, '')
if url[:2] == '//':
netloc, url = _splitnetloc(url, 2)
if (('[' in netloc and ']' not in netloc) or
@@ -227,6 +232,9 @@ def urlsplit(url, scheme='', allow_fragments=True):
# not a port number
scheme, url = url[:i].lower(), rest
+ for b in _UNSAFE_URL_BYTES_TO_REMOVE:
+ url = url.replace(b, '')
+
if url[:2] == '//':
netloc, url = _splitnetloc(url, 2)
if (('[' in netloc and ']' not in netloc) or
From 2bff6bdc57a66b3bec202739c3d5a80696dfcf46 Mon Sep 17 00:00:00 2001
From: Jeremy Paige <ucodery@gmail.com>
Date: Mon, 27 Jun 2022 10:17:42 -0700
Subject: [PATCH 2/2] Add NEWS entry
---
Lib/test/test_urlparse.py | 4 ++++
Lib/urlparse.py | 4 ++++
Misc/NEWS.d/2.7.18.5.rst | 7 +++++++
3 files changed, 15 insertions(+)
create mode 100644 Misc/NEWS.d/2.7.18.5.rst
diff --git a/Lib/test/test_urlparse.py b/Lib/test/test_urlparse.py
index 9bee4a05d2b2..e00fb7af6996 100644
--- a/Lib/test/test_urlparse.py
+++ b/Lib/test/test_urlparse.py
@@ -1,3 +1,7 @@
+# Copyright (C) 2022 ActiveState Software Inc.
+# test_urlparse.py is licensed under the PSFLv2 License.
+# See the file LICENSE for details.
+
from test import test_support
import sys
import unicodedata
diff --git a/Lib/urlparse.py b/Lib/urlparse.py
index 173df1ed18eb..914ccd2c6942 100644
--- a/Lib/urlparse.py
+++ b/Lib/urlparse.py
@@ -1,3 +1,7 @@
+# Copyright (C) 2022 ActiveState Software Inc.
+# urlparse.py is licensed under the PSFLv2 License.
+# See the file LICENSE for details.
+
"""Parse (absolute and relative) URLs.
urlparse module is based upon the following RFC specifications.