Skip to content

Commit 7971330

Browse files
committed
Fix #1699: Clear auth information on redirecting to other domain
1 parent cc97b8e commit 7971330

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

aiohttp/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def _request(self, method, url, *,
333333

334334
if url.origin() != r_url.origin():
335335
auth = None
336-
headers.pop(hdrs.AUTHORIZATION)
336+
headers.pop(hdrs.AUTHORIZATION, None)
337337

338338
url = r_url
339339
params = None

changes/1699.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clear auth information on redirecting to other domain

tests/test_client_functional.py

+51
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io
66
import json
77
import pathlib
8+
import socket
89
import ssl
910
from unittest import mock
1011

@@ -13,6 +14,7 @@
1314

1415
import aiohttp
1516
from aiohttp import ServerFingerprintMismatch, hdrs, web
17+
from aiohttp.abc import AbstractResolver
1618
from aiohttp.helpers import create_future
1719
from aiohttp.multipart import MultipartWriter
1820

@@ -2237,3 +2239,52 @@ def test_creds_in_auth_and_url(loop):
22372239
auth=aiohttp.BasicAuth('user2', 'pass2'))
22382240
finally:
22392241
yield from session.close()
2242+
2243+
2244+
@asyncio.coroutine
2245+
def test_drop_auth_on_redirect_to_other_host(test_server, loop):
2246+
@asyncio.coroutine
2247+
def srv1(request):
2248+
assert request.host == 'host1.com'
2249+
assert request.headers['Authorization'] == 'Basic dXNlcjpwYXNz'
2250+
raise web.HTTPFound('http://host2.com/path2')
2251+
2252+
@asyncio.coroutine
2253+
def srv2(request):
2254+
assert request.host == 'host2.com'
2255+
assert 'Authorization' not in request.headers
2256+
return web.Response()
2257+
2258+
app = web.Application()
2259+
app.router.add_route('GET', '/path1', srv1)
2260+
app.router.add_route('GET', '/path2', srv2)
2261+
2262+
server = yield from test_server(app)
2263+
2264+
class FakeResolver(AbstractResolver):
2265+
2266+
@asyncio.coroutine
2267+
def resolve(self, host, port=0, family=socket.AF_INET):
2268+
return [{'hostname': host,
2269+
'host': server.host,
2270+
'port': server.port,
2271+
'family': socket.AF_INET,
2272+
'proto': 0,
2273+
'flags': socket.AI_NUMERICHOST}]
2274+
2275+
@asyncio.coroutine
2276+
def close(self):
2277+
pass
2278+
2279+
connector = aiohttp.TCPConnector(loop=loop, resolver=FakeResolver())
2280+
client = aiohttp.ClientSession(connector=connector)
2281+
try:
2282+
resp = yield from client.get('http://host1.com/path1',
2283+
auth=aiohttp.BasicAuth('user', 'pass'))
2284+
assert resp.status == 200
2285+
resp = yield from client.get('http://host1.com/path1',
2286+
headers={'Authorization':
2287+
'Basic dXNlcjpwYXNz'})
2288+
assert resp.status == 200
2289+
finally:
2290+
yield from client.close()

0 commit comments

Comments
 (0)