From e872e97cff534d8af39f1e0a990e659ad7e7e1f4 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Mon, 16 Nov 2026 16:51:13 -0800 Subject: [PATCH] Do not mutate the caller's http_options headers Endpoint.set_parameters merged http_options into the request parameters with a shallow update and then wrote x-tableau-auth, content-type and User-Agent into parameters["headers"], which was the same dict object the caller passed as http_options["headers"]. The session token stayed in the caller's dict and was sent on later requests, including sign-in to a different Server built from the same options, and Server objects sharing one dict overwrote each other's token. Copy the headers dict per request before adding headers, so the caller's dict and other Server objects are never modified. Co-Authored-By: Claude Sonnet 5.5 --- .../server/endpoint/endpoint.py | 3 +- test/test_http_options_isolation.py | 67 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 test/test_http_options_isolation.py diff --git a/tableauserverclient/server/endpoint/endpoint.py b/tableauserverclient/server/endpoint/endpoint.py index 10d3cba84..64db73623 100644 --- a/tableauserverclient/server/endpoint/endpoint.py +++ b/tableauserverclient/server/endpoint/endpoint.py @@ -81,8 +81,7 @@ def __init__(self, parent_srv: "Server"): def set_parameters(http_options, auth_token, content, content_type, parameters) -> dict[str, Any]: parameters = parameters or {} parameters.update(http_options) - if "headers" not in parameters: - parameters["headers"] = {} + parameters["headers"] = dict(parameters.get("headers") or {}) if auth_token is not None: parameters["headers"][TABLEAU_AUTH_HEADER] = auth_token diff --git a/test/test_http_options_isolation.py b/test/test_http_options_isolation.py new file mode 100644 index 000000000..ca0fa39f8 --- /dev/null +++ b/test/test_http_options_isolation.py @@ -0,0 +1,67 @@ +import copy +from pathlib import Path + +import requests_mock + +import tableauserverclient as TSC +from tableauserverclient.server.endpoint.endpoint import TABLEAU_AUTH_HEADER + +SIGN_IN_XML = (Path(__file__).parent / "assets" / "auth_sign_in.xml").read_text() +TOKEN_A = "eIX6mvFsqyansa4KqEI1UwOpS8ggRs2l" + + +def _shared_options() -> dict: + return {"headers": {"X-Corp": "1"}, "timeout": 30} + + +def _sign_in_and_call(server: TSC.Server, m: requests_mock.Mocker) -> None: + m.post(server.auth.baseurl + "/signin", text=SIGN_IN_XML) + server.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples")) + m.get(server.workbooks.baseurl, text="") + server.workbooks.get_request(server.workbooks.baseurl) + + +def test_sign_in_does_not_mutate_caller_http_options() -> None: + shared = _shared_options() + original = copy.deepcopy(shared) + server_a = TSC.Server("http://a.example", http_options=shared) + server_b = TSC.Server("http://b.example", http_options=shared) + + with requests_mock.mock() as m: + _sign_in_and_call(server_a, m) + + assert shared == original + assert TABLEAU_AUTH_HEADER not in server_a.http_options["headers"] + assert TABLEAU_AUTH_HEADER not in server_b.http_options["headers"] + assert server_a.http_options["headers"]["X-Corp"] == "1" + + +def test_sign_in_to_second_server_does_not_send_first_server_token() -> None: + shared = _shared_options() + server_a = TSC.Server("http://a.example", http_options=shared) + server_b = TSC.Server("http://b.example", http_options=shared) + + with requests_mock.mock() as m: + _sign_in_and_call(server_a, m) + assert m.last_request.headers[TABLEAU_AUTH_HEADER] == TOKEN_A + m.post(server_b.auth.baseurl + "/signin", text=SIGN_IN_XML) + server_b.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples")) + sign_in_b = m.last_request + + assert sign_in_b.hostname == "b.example" + assert TABLEAU_AUTH_HEADER not in sign_in_b.headers + assert sign_in_b.headers["X-Corp"] == "1" + + +def test_requests_after_sign_out_carry_no_token() -> None: + server = TSC.Server("http://a.example", http_options=_shared_options()) + + with requests_mock.mock() as m: + _sign_in_and_call(server, m) + m.post(server.auth.baseurl + "/signout", text="") + server.auth.sign_out() + m.post(server.auth.baseurl + "/signin", text=SIGN_IN_XML) + server.auth.sign_in(TSC.TableauAuth("user", "password", site_id="Samples")) + sign_in_again = m.last_request + + assert TABLEAU_AUTH_HEADER not in sign_in_again.headers