File CVE-2020-26116-httplib-header-injection.patch of Package python (Revision 381d91ea0ab10fc5235c75951d42564a)
Currently displaying revision 381d91ea0ab10fc5235c75951d42564a , Show latest
78
1
---
2
Lib/httplib.py | 15 +++++++++++++++
3
Lib/test/test_httplib.py | 22 +++++++++++++++++++++-
4
2 files changed, 36 insertions(+), 1 deletion(-)
5
6
--- a/Lib/httplib.py
7
+++ b/Lib/httplib.py
8
9
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
10
11
12
+# These characters are not allowed within HTTP method names
13
+# to prevent http header injection.
14
+_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
15
+
16
class HTTPMessage(mimetools.Message):
17
18
def addheader(self, key, value):
19
20
else:
21
raise CannotSendRequest()
22
23
+ self._validate_method(method)
24
+
25
# Save the method for use later in the response phase
26
self._method = method
27
28
29
response.close()
30
raise
31
32
+ def _validate_method(self, method):
33
+ """Validate a method name for putrequest."""
34
+ # prevent http header injection
35
+ match = _contains_disallowed_method_pchar_re.search(method)
36
+ if match:
37
+ raise ValueError(
38
+ "method can't contain control characters. %r (found at "
39
+ "least %r)" % (method, match.group()))
40
+
41
42
class HTTP:
43
"Compatibility class with httplib.py from 1.5."
44
--- a/Lib/test/test_httplib.py
45
+++ b/Lib/test/test_httplib.py
46
47
self.assertTrue('Host: destination.com' in conn.sock.data)
48
49
50
+class HttpMethodTests(TestCase):
51
+ def test_invalid_method_names(self):
52
+ methods = (
53
+ 'GET\r',
54
+ 'POST\n',
55
+ 'PUT\n\r',
56
+ 'POST\nValue',
57
+ 'POST\nHOST:abc',
58
+ 'GET\nrHost:abc\n',
59
+ 'POST\rRemainder:\r',
60
+ 'GET\rHOST:\n',
61
+ '\nPUT'
62
+ )
63
+
64
+ for method in methods:
65
+ conn = httplib.HTTPConnection('example.com')
66
+ conn.sock = FakeSocket(None)
67
+ self.assertRaises(ValueError, conn.request, method=method, url="/")
68
+
69
+
70
@test_support.reap_threads
71
def test_main(verbose=None):
72
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
73
- HTTPTest, HTTPSTest, SourceAddressTest,
74
+ HTTPTest, HttpMethodTests, HTTPSTest, SourceAddressTest,
75
TunnelTests)
76
77
if __name__ == '__main__':
78