Merge pull request #5626 from Gallaecio/more-lenient-proxying

Relax Proxy-Authorization restrictions
This commit is contained in:
Mikhail Korobov 2022-10-26 23:36:29 +05:00 committed by GitHub
commit a1075b8979
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -78,4 +78,7 @@ class HttpProxyMiddleware:
del request.headers[b'Proxy-Authorization']
del request.meta['_auth_proxy']
elif b'Proxy-Authorization' in request.headers:
del request.headers[b'Proxy-Authorization']
if proxy_url:
request.meta['_auth_proxy'] = proxy_url
else:
del request.headers[b'Proxy-Authorization']

View File

@ -400,6 +400,9 @@ class TestHttpProxyMiddleware(TestCase):
self.assertNotIn(b'Proxy-Authorization', request.headers)
def test_proxy_authentication_header_proxy_without_credentials(self):
"""As long as the proxy URL in request metadata remains the same, the
Proxy-Authorization header is used and kept, and may even be
changed."""
middleware = HttpProxyMiddleware()
request = Request(
'https://example.com',
@ -408,7 +411,16 @@ class TestHttpProxyMiddleware(TestCase):
)
assert middleware.process_request(request, spider) is None
self.assertEqual(request.meta['proxy'], 'https://example.com')
self.assertNotIn(b'Proxy-Authorization', request.headers)
self.assertEqual(request.headers['Proxy-Authorization'], b'Basic foo')
assert middleware.process_request(request, spider) is None
self.assertEqual(request.meta['proxy'], 'https://example.com')
self.assertEqual(request.headers['Proxy-Authorization'], b'Basic foo')
request.headers['Proxy-Authorization'] = b'Basic bar'
assert middleware.process_request(request, spider) is None
self.assertEqual(request.meta['proxy'], 'https://example.com')
self.assertEqual(request.headers['Proxy-Authorization'], b'Basic bar')
def test_proxy_authentication_header_proxy_with_same_credentials(self):
middleware = HttpProxyMiddleware()