Skip to content

Commit

Permalink
Make redirect URL auth take precedence over input auth in client
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Louis Peeters committed Oct 15, 2024
1 parent 86f84b8 commit 386ff99
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/9436.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Authentication provided by a redirect now takes precedence over provided ``auth`` when making requests with the client -- by :user:`PLPeeters`.
4 changes: 3 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ async def _request(
"credentials encoded in URL"
)

if auth is None:
# Override the auth with the one from the URL only if we
# have no auth, or if we got an auth from a redirect URL
if auth is None or (history and auth_from_url is not None):
auth = auth_from_url

if (
Expand Down
7 changes: 7 additions & 0 deletions docs/client_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ argument. An instance of :class:`BasicAuth` can be passed in like this::
async with ClientSession(auth=auth) as session:
...

Note that if the request is redirected and the redirect URL contains
credentials, those credentials will supersede any previously set credentials.
In other words, if ``http://user@example.com`` redirects to
``http://other_user@example.com``, the second request will be authenticated
as ``other_user``. Providing both the ``auth`` parameter and authentication in
the *initial* URL will result in a :exc:`ValueError`.

For other authentication flows, the ``Authorization`` header can be set
directly::

Expand Down
3 changes: 3 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2983,6 +2983,9 @@ async def close(self) -> None:
assert len(resp.history) == 1
assert str(resp.url) == "http://example.com"
assert resp.status == 200
assert (
resp.request_info.headers.get("authorization") == "Basic dXNlcjo="
), "Expected redirect credentials to take precedence over provided auth"


@pytest.fixture
Expand Down

0 comments on commit 386ff99

Please sign in to comment.