From df0c6027af7c7eabf57ba43a9f6f1d8ba5f6cf27 Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Sat, 21 Nov 2026 20:55:39 -0600 Subject: [PATCH] fix: sign in over HTTPS when the server requires it An http sign-in against a server that only accepts https fails with 405 after the credentials have already been sent. Probe the unauthenticated serverInfo endpoint first and switch the address to https when that probe redirects or when http is refused and https answers. --- .../server/endpoint/auth_endpoint.py | 46 +++++++++++++++ test/test_auth.py | 57 +++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/tableauserverclient/server/endpoint/auth_endpoint.py b/tableauserverclient/server/endpoint/auth_endpoint.py index 110d38a18..8773e39a8 100644 --- a/tableauserverclient/server/endpoint/auth_endpoint.py +++ b/tableauserverclient/server/endpoint/auth_endpoint.py @@ -1,5 +1,6 @@ import logging from typing import TYPE_CHECKING +from urllib.parse import urljoin, urlparse import warnings from defusedxml.ElementTree import fromstring @@ -66,6 +67,7 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr: >>> # call the sign-in method with the auth object >>> server.auth.sign_in(tableau_auth) """ + self._use_https_if_the_server_requires_it() url = f"{self.baseurl}/signin" signin_req = RequestFactory.Auth.signin_req(auth_req) # Route through _make_request so signin gets the same redirect handling @@ -91,6 +93,50 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr: self.parent_srv.use_server_version() return Auth.contextmgr(self.sign_out) + def _use_https_if_the_server_requires_it(self) -> None: + """Switch an http address to https before the sign-in POST. + + An http sign-in against a server that requires https is rejected with + 405, after the credentials have already been sent in cleartext. Probe + the unauthenticated serverInfo endpoint first. A redirect to https on + the same host, or an http refusal that succeeds over https, updates + the stored address. An http-only server is left unchanged. + """ + address = self.parent_srv.server_address + parsed = urlparse(address) + if parsed.scheme != "http": + return + timeout = self.parent_srv.http_options.get("timeout") + verify = self.parent_srv.http_options.get("verify", True) + probe_url = f"{address}/api/{self.parent_srv.version}/serverInfo" + try: + response = self.parent_srv.session.get(probe_url, allow_redirects=False, timeout=timeout, verify=verify) + except Exception as exc: + logger.info("Could not probe %s before sign-in: %s", probe_url, exc) + return + + https_address = "https://" + address[len("http://") :] + if response.is_redirect or response.status_code in (301, 302, 303, 307, 308): + location = response.headers.get("Location") or "" + target = urlparse(urljoin(probe_url, location)) + if target.scheme == "https" and target.netloc == parsed.netloc: + self.parent_srv._server_address = https_address + logger.info("Server requires HTTPS; signing in at %s", https_address) + return + if response.status_code not in (400, 403, 405): + return + https_probe = f"{https_address}/api/{self.parent_srv.version}/serverInfo" + try: + https_response = self.parent_srv.session.get( + https_probe, allow_redirects=False, timeout=timeout, verify=verify + ) + except Exception as exc: + logger.info("Could not probe %s before sign-in: %s", https_probe, exc) + return + if https_response.status_code == 200: + self.parent_srv._server_address = https_address + logger.info("Server requires HTTPS; signing in at %s", https_address) + # We use the same request that username/password login uses for all auth types. # The distinct methods are mostly useful for explicitly showing api version support for each auth type @api(version="3.6") diff --git a/test/test_auth.py b/test/test_auth.py index c50f4d29b..47f355ff5 100644 --- a/test/test_auth.py +++ b/test/test_auth.py @@ -19,6 +19,63 @@ def server() -> TSC.Server: return server_instance +def test_sign_in_switches_to_https_when_the_probe_redirects() -> None: + server = TSC.Server("http://tableau.example", False) + response_xml = SIGN_IN_XML.read_text() + with requests_mock.mock() as m: + m.get( + "http://tableau.example/api/2.4/serverInfo", + status_code=301, + headers={"Location": "https://tableau.example/api/2.4/serverInfo"}, + ) + m.post("https://tableau.example/api/2.4/auth/signin", text=response_xml) + server.auth.sign_in(TSC.TableauAuth("testuser", "password", site_id="Samples")) + + assert server.server_address == "https://tableau.example" + assert [call.method for call in m.request_history] == ["GET", "POST"] + assert m.request_history[1].url.startswith("https://tableau.example/") + + +def test_sign_in_stays_on_http_when_the_server_accepts_it() -> None: + server = TSC.Server("http://plain.example", False) + response_xml = SIGN_IN_XML.read_text() + info_xml = """ + + + 10.0.0 + 2.4 + + """ + with requests_mock.mock() as m: + m.get("http://plain.example/api/2.4/serverInfo", text=info_xml) + m.post("http://plain.example/api/2.4/auth/signin", text=response_xml) + server.auth.sign_in(TSC.TableauAuth("testuser", "password")) + + assert server.server_address == "http://plain.example" + assert m.request_history[1].url.startswith("http://plain.example/") + + +def test_sign_in_switches_to_https_when_http_is_rejected() -> None: + server = TSC.Server("http://secure.example", False) + response_xml = SIGN_IN_XML.read_text() + info_xml = """ + + + 2024.1.0 + 3.22 + + """ + with requests_mock.mock() as m: + m.get("http://secure.example/api/2.4/serverInfo", status_code=405) + m.get("https://secure.example/api/2.4/serverInfo", text=info_xml) + m.post("https://secure.example/api/2.4/auth/signin", text=response_xml) + server.auth.sign_in(TSC.TableauAuth("testuser", "password")) + + assert server.server_address == "https://secure.example" + assert m.request_history[0].method == "GET" + assert m.request_history[-1].url.startswith("https://secure.example/") + + def test_sign_in(server: TSC.Server) -> None: with open(SIGN_IN_XML, "rb") as f: response_xml = f.read().decode("utf-8")