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