Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.d/20261106_164949_kurtmckee.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Fixed
-----

- Fix session cookies being stored and sent on redirected requests. (:pr:`NUMBER`)

Previously, the SDK would not send session cookies when making an HTTP request,
but if the request was subsequently redirected then session cookies could be added.

Now, ``RequestsTransport`` no longer stores cookies set by responses.
47 changes: 47 additions & 0 deletions src/globus_sdk/transport/_no_cookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import annotations

import http.cookiejar
import typing as t

if t.TYPE_CHECKING:
import urllib.request

# NOTE: this module imports `http.cookiejar` at import time, so it must only be
# imported lazily (e.g., when a `RequestsTransport` is constructed), in the same way
# that `requests` is.


class NoCookiesPolicy(http.cookiejar.CookiePolicy):
"""
A cookie policy which rejects every cookie, both when a response sets one
and when a request would send one.

The SDK never intends to send cookies, but `requests` still stores any
``Set-Cookie`` in the session's cookie jar and merges that jar into redirect
hops. Using this policy on the session's jar keeps a cookie set by one
response from being sent on a later, unrelated request.
"""

# These class variables are not defined by the `CookiePolicy` class contract.
# However, a `CookiePolicy` subclass, `DefaultCookiePolicy`, defines them,
# and the standard library's `CookieJar` accesses them as attributes.
# They exist here to prevent attribute errors, though they have no effect.
netscape = False
rfc2965 = False
hide_cookie2 = False

def set_ok(
self, cookie: http.cookiejar.Cookie, request: urllib.request.Request
) -> bool:
return False

def return_ok(
self, cookie: http.cookiejar.Cookie, request: urllib.request.Request
) -> bool:
return False

def domain_return_ok(self, domain: str, request: urllib.request.Request) -> bool:
return False

def path_return_ok(self, path: str, request: urllib.request.Request) -> bool:
return False
3 changes: 3 additions & 0 deletions src/globus_sdk/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ def __init__(
) -> None:
import requests

from ._no_cookies import NoCookiesPolicy

self.session = requests.Session()
self.session.cookies.set_policy(NoCookiesPolicy())
self.verify_ssl = config.get_ssl_verify(verify_ssl)
self.http_timeout = config.get_http_timeout(http_timeout)
self._user_agent = self.BASE_USER_AGENT
Expand Down
183 changes: 183 additions & 0 deletions tests/functional/base_client/test_transport_session_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
"""
The transport owns a ``requests.Session``, but it sends requests with
``session.send(request.prepare())`` and never calls ``Session.prepare_request``.
The SDK therefore does not use the session's headers, params, auth, hooks, or cookies:
the transport and the client configure those things themselves.

These tests pin that behavior down, for requests which are sent directly
and for requests which are redirected.
"""

import pytest
import responses

import globus_sdk
from globus_sdk.testing import get_last_request

BASE_URL = "https://foo.api.globus.org"
OTHER_URL = "https://other.api.globus.org"


def _add_redirect(from_url, to_url):
responses.add(
responses.GET,
from_url,
status=302,
headers={"Location": to_url},
)


def _send(client, *, redirected):
"""
Send one GET and return the last request which went over the (mocked) wire.

If ``redirected`` is True, the request is redirected once,
and the returned request is the redirect hop, not the first hop.
"""

if redirected:
_add_redirect(f"{BASE_URL}/start", f"{BASE_URL}/end")
responses.add(responses.GET, f"{BASE_URL}/end", json={})
client.get("/start")
else:
responses.add(responses.GET, f"{BASE_URL}/end", json={})
client.get("/end")

req = get_last_request()
assert req.url.startswith(f"{BASE_URL}/end")
return req


# --- cookies ---


def test_cookie_from_earlier_response_is_not_sent_on_a_direct_request(client):
responses.add(
responses.GET,
f"{BASE_URL}/set-cookie",
json={},
headers={"Set-Cookie": "session=SECRET; Path=/"},
)
client.get("/set-cookie")

req = _send(client, redirected=False)
assert "Cookie" not in req.headers


@pytest.mark.parametrize(
"redirect_origin",
(
pytest.param("same-host", id="redirect-within-the-cookie-host"),
pytest.param("other-host", id="redirect-from-another-host"),
),
)
def test_cookie_from_earlier_response_is_not_sent_on_a_redirect_hop(
client, client_class, redirect_origin
):
# one response sets a cookie
responses.add(
responses.GET,
f"{BASE_URL}/set-cookie",
json={},
headers={"Set-Cookie": "session=SECRET; Path=/"},
)
client.get("/set-cookie")

# a later, unrelated request is redirected to a URL the cookie matches
if redirect_origin == "same-host":
second_client = client
second_url = f"{BASE_URL}/start"
else:
# a different client with a different base URL, sharing one transport
second_client = client_class(base_url=OTHER_URL, transport=client.transport)
second_url = f"{OTHER_URL}/start"
_add_redirect(second_url, f"{BASE_URL}/end")
responses.add(responses.GET, f"{BASE_URL}/end", json={})

second_client.get("/start")

req = get_last_request()
assert req.url == f"{BASE_URL}/end"
assert "Cookie" not in req.headers


def test_cookie_from_a_failed_attempt_is_not_sent_on_a_redirect_hop_of_the_retry(
client,
):
# The cookie is stored by the first attempt's 503 (e.g., a load balancer's
# sticky-session cookie). The retry is a first hop, so it does not carry the
# cookie, but if the retry is redirected, that hop does.
responses.add(
responses.GET,
f"{BASE_URL}/flaky",
status=503,
headers={"Set-Cookie": "sticky=NODE-A; Path=/"},
)
_add_redirect(f"{BASE_URL}/flaky", f"{BASE_URL}/end")
responses.add(responses.GET, f"{BASE_URL}/end", json={})

client.get("/flaky")

# guard against this test silently ceasing to test a retry followed by a redirect
assert [call.response.status_code for call in responses.calls] == [503, 302, 200]
req = get_last_request()
assert req.url == f"{BASE_URL}/end"
assert "Cookie" not in req.headers


# --- other session state which is not used ---


@pytest.mark.parametrize("redirected", (False, True), ids=("direct", "redirected"))
def test_session_headers_are_not_sent(client, redirected):
client.transport.session.headers["X-Session-Header"] = "from-the-session"

req = _send(client, redirected=redirected)

assert "X-Session-Header" not in req.headers
# the SDK's own identification is used, not the session's
assert req.headers["User-Agent"] == client.transport.user_agent
assert "python-requests" not in req.headers["User-Agent"]


def test_session_params_are_not_sent(client):
# not parametrized over redirects: a redirect hop's URL comes from `Location`
client.transport.session.params = {"from_session": "1"}

req = _send(client, redirected=False)

assert "from_session" not in req.url


@pytest.mark.parametrize("redirected", (False, True), ids=("direct", "redirected"))
def test_session_auth_is_not_sent(client, redirected):
client.transport.session.auth = ("session-user", "session-password")

req = _send(client, redirected=redirected)

assert "Authorization" not in req.headers


@pytest.mark.parametrize("redirected", (False, True), ids=("direct", "redirected"))
def test_session_hooks_are_not_run(client, redirected):
calls = []

def hook(response, **kwargs):
calls.append(response.url)
return response

client.transport.session.hooks["response"].append(hook)

_send(client, redirected=redirected)

assert calls == []


def test_authorizer_provides_authorization_despite_session_auth(client_class):
# the only source of an Authorization header is the client's authorizer
client = client_class(authorizer=globus_sdk.AccessTokenAuthorizer("sdk-token"))
client.transport.session.auth = ("session-user", "session-password")

req = _send(client, redirected=False)

assert req.headers["Authorization"] == "Bearer sdk-token"
Loading