File CVE-2021-28861-double-slash-path.patch of Package python (Revision 7c7532457b948cc36e86ff51d95885bc)
Currently displaying revision 7c7532457b948cc36e86ff51d95885bc , Show latest
xxxxxxxxxx
1
Index: Python-2.7.18/Lib/BaseHTTPServer.py
2
===================================================================
3
--- Python-2.7.18.orig/Lib/BaseHTTPServer.py
4
+++ Python-2.7.18/Lib/BaseHTTPServer.py
5
6
return False
7
self.command, self.path, self.request_version = command, path, version
8
9
+ # CVE-2021-28861: The purpose of replacing '//' with '/' is to
10
+ # protect against open redirect attacks possibly triggered if the
11
+ # path starts with '//' because http clients treat //path as an
12
+ # absolute URI without scheme (similar to http://path) rather than
13
+ # a path.
14
+ if self.path.startswith('//'):
15
+ self.path = '/' + self.path.lstrip('/') # Reduce to a single /
16
+
17
# Examine the headers and look for a Connection directive
18
self.headers = self.MessageClass(self.rfile, 0)
19
20
Index: Python-2.7.18/Lib/test/test_httpservers.py
21
===================================================================
22
--- Python-2.7.18.orig/Lib/test/test_httpservers.py
23
+++ Python-2.7.18/Lib/test/test_httpservers.py
24
25
self.assertEqual(response.getheader("Location"),
26
self.tempdir_name + "/?hi=1")
27
28
+ def test_get_dir_redirect_location_domain_injection_bug(self):
29
+ """Ensure //evil.co/..%2f../../X does not put //evil.co/ in Location.
30
+ //netloc/ in a Location header is a redirect to a new host.
31
+ https://github.com/python/cpython/issues/87389
32
+ This checks that a path resolving to a directory on our server cannot
33
+ resolve into a redirect to another server.
34
+ """
35
+ os.mkdir(os.path.join(self.tempdir, 'existing_directory'))
36
+ url = '/python.org/..%2f..%2f..%2f..%2f..%2f../%0a%0d/../' + self.tempdir_name + '/existing_directory'
37
+ expected_location = url + '/' # /python.org.../ single slash single prefix, trailing slash
38
+ # Canonicalizes to /tmp/tempdir_name/existing_directory which does
39
+ # exist and is a dir, triggering the 301 redirect logic.
40
+ response = self.request(url)
41
+ self.check_status_and_reason(response, 301)
42
+ location = response.getheader('Location')
43
+ self.assertEqual(location, expected_location, msg='non-attack failed!')
44
+
45
+ # //python.org... multi-slash prefix, no trailing slash
46
+ attack_url = '/' + url
47
+ response = self.request(attack_url)
48
+ self.check_status_and_reason(response, 301)
49
+ location = response.getheader('Location')
50
+ self.assertFalse(location.startswith('//'), msg=location)
51
+ self.assertEqual(location, expected_location,
52
+ msg='Expected Location header to start with a single / and '
53
+ 'end with a / as this is a directory redirect.')
54
+ # ///python.org... triple-slash prefix, no trailing slash
55
+ attack3_url = '//' + url
56
+ response = self.request(attack3_url)
57
+ self.check_status_and_reason(response, 301)
58
+ self.assertEqual(response.getheader('Location'), expected_location)
59
+
60
+ # If the second word in the http request (Request-URI for the http
61
+ # method) is a full URI, we don't worry about it, as that'll be parsed
62
+ # and reassembled as a full URI within BaseHTTPRequestHandler.send_head
63
+ # so no errant scheme-less //netloc//evil.co/ domain mixup can happen.
64
+ attack_scheme_netloc_2slash_url = 'https://pypi.org/' + url
65
+ expected_scheme_netloc_location = attack_scheme_netloc_2slash_url + '/'
66
+ response = self.request(attack_scheme_netloc_2slash_url)
67
+ self.check_status_and_reason(response, 301)
68
+ location = response.getheader('Location')
69
+ # We're just ensuring that the scheme and domain make it through, if
70
+ # there are or aren't multiple slashes at the start of the path that
71
+ # follows that isn't important in this Location: header.
72
+ self.assertTrue(location.startswith('https://pypi.org/'), msg=location)
73
+
74
75
cgi_file1 = """\
76
#!%s
77