Skip to content
Open
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
59 changes: 38 additions & 21 deletions tableauserverclient/server/endpoint/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@
USER_AGENT_HEADER = "User-Agent"


_DEFAULT_PORTS = {"http": 80, "https": 443}
_CREDENTIAL_HEADERS = {TABLEAU_AUTH_HEADER, "authorization", "cookie"}
_CREDENTIAL_PARAMETERS = ("cookies", "auth")


def _same_host(current_url: str, next_url: str) -> bool:
current = urlparse(current_url)
next_ = urlparse(next_url)
if (current.hostname or "").lower() != (next_.hostname or "").lower():
return False
if current.scheme != next_.scheme:
# http -> https on the same hostname, matching the server address promotion
# in Endpoint._follow_redirect_if_any
return True
return (current.port or _DEFAULT_PORTS.get(current.scheme)) == (next_.port or _DEFAULT_PORTS.get(next_.scheme))


def _without_credentials(parameters: dict[str, Any]) -> dict[str, Any]:
stripped = {k: v for k, v in parameters.items() if k not in _CREDENTIAL_PARAMETERS}
stripped["headers"] = {
k: v for k, v in (parameters.get("headers") or {}).items() if k.lower() not in _CREDENTIAL_HEADERS
}
return stripped


class Endpoint:
def __init__(self, parent_srv: "Server"):
self.parent_srv = parent_srv
Expand Down Expand Up @@ -266,27 +291,19 @@ def _hostport(parsed):
new_address = f"https://{next_host}{port_suffix}"
self.parent_srv._server_address = new_address
logger.info(f"Server redirected to HTTPS; updated server address to {new_address}")
# Auth-material policy: the request `parameters` (headers, body,
# cookies) are forwarded to the redirect target unchanged. Two
# cases carry credentials:
# 1. Already-signed-in calls carry the issued token in the
# X-Tableau-Auth header and session cookies.
# 2. sign_in itself has no token yet; the raw credentials
# (username+password or PAT secret) travel in the POST
# body of the signin request.
# Both are forwarded on redirect. This is intentional. TSC is a
# client library for a specific server the caller has already
# agreed to trust, and customers routinely deploy Tableau Server
# behind reverse proxies, load balancers, and SSO front-ends that
# redirect between hosts within their own infrastructure
# (e.g. tableau.corp.example -> east.tableau.corp.example, or
# an SSO IdP -> the auth-callback endpoint on a different
# subdomain). Stripping credentials on cross-host redirects
# would break sign-in against every such deployment.
# The HTTPS -> HTTP downgrade guard above (line 208) is the
# boundary that keeps this from becoming a security regression:
# once the caller connects over HTTPS, credentials never leave
# TLS to a downgraded target.
# Method and body follow every redirect, so a sign_in redirected to
# another host still sends its credentials there (over https only,
# given the downgrade check above). Session credentials (the
# X-Tableau-Auth token, Authorization and Cookie headers, cookies
# and auth options) are only sent while the redirect stays on the
# same host; once a hop leaves it they are dropped for the rest of
# the chain.
if not _same_host(current_url, next_url):
logger.warning(
f"Following redirect from host {urlparse(current_url).netloc} to a different host "
f"{urlparse(next_url).netloc}; not sending session credentials"
)
parameters = _without_credentials(parameters)
logger.debug(f"Following {response.status_code} redirect: {current_url} -> {next_url}")
history.append(response)
current_url = next_url
Expand Down
114 changes: 114 additions & 0 deletions test/test_redirect_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,120 @@ def test_cross_host_redirect_followed(server: TSC.Server) -> None:
assert server.auth_token is not None


def _capture_into(seen: list, body: str):
def capture(request, context):
seen.append(request)
context.status_code = 200
return body

return capture


def test_cross_host_redirect_drops_auth_header(signed_in_server: TSC.Server) -> None:
seen: list = []
with requests_mock.mock() as m:
m.get(
signed_in_server.workbooks.baseurl,
status_code=302,
headers={"Location": "http://other.test/api/3.10/sites/site-id/workbooks"},
)
m.get("http://other.test/api/3.10/sites/site-id/workbooks", text=_capture_into(seen, _workbooks_get_xml()))
signed_in_server.workbooks.get()
assert len(seen) == 1
assert "x-tableau-auth" not in seen[0].headers


def test_cross_host_redirect_drops_credential_headers_and_cookies() -> None:
s = TSC.Server(
"http://test", False, http_options={"headers": {"Authorization": "Basic abc"}, "cookies": {"c": "1"}}
)
s.version = "3.10"
s._set_auth("site-id", "user-id", "auth-token", "")
seen: list = []
with requests_mock.mock() as m:
m.get(s.workbooks.baseurl, status_code=302, headers={"Location": "http://other.test/wb"})
m.get("http://other.test/wb", text=_capture_into(seen, _workbooks_get_xml()))
s.workbooks.get()
assert len(seen) == 1
assert "Authorization" not in seen[0].headers
assert "Cookie" not in seen[0].headers


def test_cross_host_sign_in_redirect_keeps_body(server: TSC.Server) -> None:
seen: list = []
with requests_mock.mock() as m:
m.post(
server.auth.baseurl + "/signin",
status_code=307,
headers={"Location": "https://east.test/api/3.6/auth/signin"},
)
m.post("https://east.test/api/3.6/auth/signin", text=_capture_into(seen, _sign_in_xml()))
server.auth.sign_in(TSC.TableauAuth("u", "p"))
assert len(seen) == 1
assert b"<credentials" in seen[0].body
assert server.auth_token is not None


def test_https_to_http_downgrade_rejected_on_same_host() -> None:
s = TSC.Server("https://secure.test", False)
with requests_mock.mock() as m:
m.post(s.auth.baseurl + "/signin", status_code=301, headers={"Location": "http://secure.test/signin"})
with pytest.raises(RedirectError, match="HTTPS -> HTTP"):
s.auth.sign_in(TSC.TableauAuth("u", "p"))


def test_redirect_to_different_port_drops_auth_header() -> None:
s = TSC.Server("http://test:8000", False)
s.version = "3.10"
s._set_auth("site-id", "user-id", "auth-token", "")
seen: list = []
with requests_mock.mock() as m:
m.get(s.workbooks.baseurl, status_code=302, headers={"Location": "http://test:9000/wb"})
m.get("http://test:9000/wb", text=_capture_into(seen, _workbooks_get_xml()))
s.workbooks.get()
assert "x-tableau-auth" not in seen[0].headers


def test_credentials_stay_dropped_after_returning_to_original_host(signed_in_server: TSC.Server) -> None:
seen: list = []
baseurl = signed_in_server.workbooks.baseurl
with requests_mock.mock() as m:
m.get(baseurl, status_code=302, headers={"Location": "http://other.test/wb"})
m.get("http://other.test/wb", status_code=302, headers={"Location": baseurl + "?back=1"})
m.get(baseurl + "?back=1", text=_capture_into(seen, _workbooks_get_xml()))
signed_in_server.workbooks.get()
assert "x-tableau-auth" not in seen[0].headers


def test_same_host_redirect_keeps_auth_header_and_body(server: TSC.Server) -> None:
seen: list = []
with requests_mock.mock() as m:
m.post(server.auth.baseurl + "/signin", status_code=307, headers={"Location": "http://TEST/new"})
m.post("http://test/new", text=_capture_into(seen, _sign_in_xml()))
server.auth.sign_in(TSC.TableauAuth("u", "p"))
assert b"<credentials" in seen[0].body


def test_same_host_https_upgrade_keeps_auth_header(signed_in_server: TSC.Server) -> None:
seen: list = []
with requests_mock.mock() as m:
m.get(signed_in_server.workbooks.baseurl, status_code=301, headers={"Location": "https://test/wb"})
m.get("https://test/wb", text=_capture_into(seen, _workbooks_get_xml()))
signed_in_server.workbooks.get()
assert seen[0].headers.get("x-tableau-auth") == "auth-token"


def test_cross_host_redirect_does_not_mutate_http_options() -> None:
s = TSC.Server("http://test", False, http_options={"headers": {"Authorization": "Basic abc"}})
s.version = "3.10"
s._set_auth("site-id", "user-id", "auth-token", "")
with requests_mock.mock() as m:
m.get(s.workbooks.baseurl, status_code=302, headers={"Location": "http://other.test/wb"})
m.get("http://other.test/wb", text=_workbooks_get_xml())
s.workbooks.get()
assert s.http_options["headers"]["Authorization"] == "Basic abc"


# --- Location edge cases ----------------------------------------------------


Expand Down
Loading