From f425de7efc9950e71348a4371b6b833a642902c7 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Wed, 18 Nov 2026 11:22:54 -0500 Subject: [PATCH 1/2] MLE-34233 Scope credentials to configured origin --- marklogic/client.py | 10 ++- marklogic/cloud_auth.py | 10 ++- marklogic/internal/url.py | 35 ++++++++ pyproject.toml | 1 + tests/conftest.py | 13 ++- tests/test_client_security.py | 147 ++++++++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+), 8 deletions(-) create mode 100644 marklogic/internal/url.py create mode 100644 tests/test_client_security.py diff --git a/marklogic/client.py b/marklogic/client.py index 132e72c..adad2fe 100644 --- a/marklogic/client.py +++ b/marklogic/client.py @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. +# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. import json @@ -7,6 +7,7 @@ from marklogic.cloud_auth import MarkLogicCloudAuth from marklogic.documents import DocumentManager from marklogic.internal.eval import process_multipart_mixed_response +from marklogic.internal.url import is_same_origin from marklogic.rows import RowManager from marklogic.transactions import TransactionManager, Transaction from requests.auth import HTTPDigestAuth @@ -70,7 +71,12 @@ def prepare_request(self, request, *args, **kwargs): more information on prepared requests. Note that this is invoked after the 'request' method is invoked. """ - request.url = urljoin(self.base_url, request.url) + request_url = urljoin(self.base_url, request.url) + if not is_same_origin(request_url, self.base_url): + raise ValueError( + "Request URL must use the same origin as the configured base_url." + ) + request.url = request_url return super(Client, self).prepare_request(request, *args, **kwargs) @property diff --git a/marklogic/cloud_auth.py b/marklogic/cloud_auth.py index 86b2f20..72495af 100644 --- a/marklogic/cloud_auth.py +++ b/marklogic/cloud_auth.py @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. +# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. import logging @@ -7,6 +7,8 @@ from requests.auth import AuthBase from urllib.parse import urljoin +from marklogic.internal.url import is_same_origin + logger = logging.getLogger(__name__) @@ -68,7 +70,11 @@ def _generate_token(self): self._access_token = response.json()["access_token"] def _renew_token_if_necessary(self, response: Response, *args, **kwargs): - if response.status_code == 401 and not self.resent_request_on_401: + request_url = response.request.url if response.request else None + is_unauthorized = response.status_code == 401 + is_first_retry = not self.resent_request_on_401 + matches_base_url = is_same_origin(request_url, self._base_url) + if is_unauthorized and is_first_retry and matches_base_url: logger.debug("Received 401; will generate new token and try request again") self.resent_request_on_401 = True self._generate_token() diff --git a/marklogic/internal/url.py b/marklogic/internal/url.py new file mode 100644 index 0000000..913aaf6 --- /dev/null +++ b/marklogic/internal/url.py @@ -0,0 +1,35 @@ +# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. + + +from urllib.parse import urlsplit + + +def is_same_origin(url: str, base_url: str) -> bool: + if not isinstance(url, str) or not isinstance(base_url, str): + return False + + try: + url_parts = urlsplit(url) + base_parts = urlsplit(base_url) + url_port = url_parts.port + base_port = base_parts.port + except ValueError: + return False + + url_scheme = url_parts.scheme.lower() + base_scheme = base_parts.scheme.lower() + url_host = url_parts.hostname + base_host = base_parts.hostname + + if not url_scheme or not base_scheme or not url_host or not base_host: + return False + + default_ports = {"http": 80, "https": 443} + if url_port is None: + url_port = default_ports.get(url_scheme) + if base_port is None: + base_port = default_ports.get(base_scheme) + + url_origin = (url_scheme, url_host.lower(), url_port) + base_origin = (base_scheme, base_host.lower(), base_port) + return url_origin == base_origin diff --git a/pyproject.toml b/pyproject.toml index e00fa0d..1d64183 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,3 +32,4 @@ build-backend = "poetry.core.masonry.api" # Enables live logging; see https://docs.pytest.org/en/latest/how-to/logging.html#live-logs log_cli = 1 log_cli_level = "DEBUG" +markers = ["unit: tests that do not require a MarkLogic server"] diff --git a/tests/conftest.py b/tests/conftest.py index ccb4179..5c11bd1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. +# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. import pytest @@ -58,15 +58,20 @@ def cloud_config(): @pytest.fixture(autouse=True) -def prepare_test_database(admin_client: Client): +def prepare_test_database(admin_client: Client, request): """ Deletes any documents created by other tests to ensure a 'clean' database before a test runs. Does not delete documents in the 'test-data' collection which is intended to contain all the documents loaded by the test-app. A user with the 'admin' role is used so that temporal documents can be deleted. """ - query = "cts:uris((), (), cts:not-query(cts:collection-query('test-data'))) \ - ! xdmp:document-delete(.)" + if request.node.get_closest_marker("unit"): + return + + query = ( + "cts:uris((), (), cts:not-query(cts:collection-query('test-data'))) " + "! xdmp:document-delete(.)" + ) response = admin_client.post( "v1/eval", headers={"Content-type": "application/x-www-form-urlencoded"}, diff --git a/tests/test_client_security.py b/tests/test_client_security.py new file mode 100644 index 0000000..9d8d355 --- /dev/null +++ b/tests/test_client_security.py @@ -0,0 +1,147 @@ +# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. + + +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from threading import Thread +from unittest.mock import Mock, patch + +import pytest +from requests import Request, Response, Session + +from marklogic import Client +from marklogic.cloud_auth import MarkLogicCloudAuth + + +@pytest.mark.unit +@pytest.mark.parametrize( + "url", + ["https://attacker.example/resource", "//attacker.example/resource"], +) +def test_prepare_request_rejects_cross_origin_url(url): + client = Client("https://marklogic.example", auth=("user", "password")) + + with pytest.raises(ValueError, match="same origin"): + client.prepare_request(Request("GET", url)) + + +@pytest.mark.unit +def test_prepare_request_accepts_same_origin_absolute_url(): + client = Client("https://marklogic.example", auth=("user", "password")) + + prepared = client.prepare_request( + Request("GET", "https://marklogic.example/resource") + ) + + assert prepared.url == "https://marklogic.example/resource" + + +@pytest.mark.unit +def test_cloud_auth_does_not_retry_401_from_another_origin(): + session = Session() + token_response = Mock(status_code=200) + token_response.json.return_value = {"access_token": "initial-token"} + + with patch( + "marklogic.cloud_auth.requests.post", return_value=token_response + ) as post: + auth = MarkLogicCloudAuth(session, "https://marklogic.example", "api-key") + post.reset_mock() + + request = Request("GET", "https://attacker.example/resource").prepare() + response = Response() + response.status_code = 401 + response.request = request + + with patch.object(session, "send") as send: + assert auth._renew_token_if_necessary(response) is None + + post.assert_not_called() + send.assert_not_called() + assert "Authorization" not in request.headers + + +@pytest.mark.unit +def test_cloud_auth_does_not_send_token_to_cross_origin_redirect_target(): + class UnauthorizedHandler(BaseHTTPRequestHandler): + def do_GET(self): + self.server.authorization_headers.append(self.headers.get("Authorization")) + self.send_response(401) + self.send_header("Content-Length", "0") + self.end_headers() + + def log_message(self, format, *args): + pass + + class RedirectHandler(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(302) + self.send_header("Location", self.server.redirect_url) + self.send_header("Content-Length", "0") + self.end_headers() + + def log_message(self, format, *args): + pass + + foreign_server = ThreadingHTTPServer(("127.0.0.1", 0), UnauthorizedHandler) + foreign_server.authorization_headers = [] + redirect_server = ThreadingHTTPServer(("127.0.0.1", 0), RedirectHandler) + redirect_server.redirect_url = ( + f"http://127.0.0.1:{foreign_server.server_port}/unauthorized" + ) + threads = [ + Thread(target=server.serve_forever, daemon=True) + for server in (foreign_server, redirect_server) + ] + for thread in threads: + thread.start() + + token_response = Mock(status_code=200) + token_response.json.return_value = {"access_token": "initial-token"} + try: + with patch( + "marklogic.cloud_auth.requests.post", return_value=token_response + ) as post: + client = Client( + f"http://127.0.0.1:{redirect_server.server_port}", + cloud_api_key="api-key", + ) + post.reset_mock() + response = client.get("/redirect") + client.close() + finally: + for server in (redirect_server, foreign_server): + server.shutdown() + server.server_close() + for thread in threads: + thread.join() + + assert response.status_code == 401 + assert foreign_server.authorization_headers == [None] + post.assert_not_called() + + +@pytest.mark.unit +def test_cloud_auth_still_retries_401_from_configured_origin(): + session = Session() + token_response = Mock(status_code=200) + token_response.json.side_effect = [ + {"access_token": "initial-token"}, + {"access_token": "renewed-token"}, + ] + response = Response() + response.status_code = 401 + response.request = Request("GET", "https://marklogic.example/resource").prepare() + retry_response = Response() + retry_response.status_code = 200 + + with patch( + "marklogic.cloud_auth.requests.post", return_value=token_response + ) as post: + auth = MarkLogicCloudAuth(session, "https://marklogic.example", "api-key") + with patch.object(session, "send", return_value=retry_response) as send: + retried_response = auth._renew_token_if_necessary(response) + + assert retried_response is retry_response + assert post.call_count == 2 + send.assert_called_once() + assert response.request.headers["Authorization"] == "Bearer renewed-token" From 2adf15d3e43be6d91b54c78fe302a06a2f0d93c8 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Wed, 18 Nov 2026 13:02:57 -0500 Subject: [PATCH 2/2] Address Copilot comments --- marklogic/client.py | 12 ++++ marklogic/internal/url.py | 12 +++- tests/test_client_security.py | 111 ++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 3 deletions(-) diff --git a/marklogic/client.py b/marklogic/client.py index adad2fe..8c54363 100644 --- a/marklogic/client.py +++ b/marklogic/client.py @@ -79,6 +79,18 @@ def prepare_request(self, request, *args, **kwargs): request.url = request_url return super(Client, self).prepare_request(request, *args, **kwargs) + def should_strip_auth(self, old_url, new_url): + return not is_same_origin(old_url, new_url) + + def rebuild_auth(self, prepared_request, response): + if not is_same_origin(prepared_request.url, self.base_url): + prepared_request.hooks["response"] = [ + hook + for hook in prepared_request.hooks.get("response", []) + if getattr(hook, "__self__", None) is not self.auth + ] + return super(Client, self).rebuild_auth(prepared_request, response) + @property def documents(self): if not hasattr(self, "_documents"): diff --git a/marklogic/internal/url.py b/marklogic/internal/url.py index 913aaf6..8e60dc0 100644 --- a/marklogic/internal/url.py +++ b/marklogic/internal/url.py @@ -2,6 +2,8 @@ from urllib.parse import urlsplit +from requests.exceptions import RequestException +from requests.models import PreparedRequest def is_same_origin(url: str, base_url: str) -> bool: @@ -9,11 +11,15 @@ def is_same_origin(url: str, base_url: str) -> bool: return False try: - url_parts = urlsplit(url) - base_parts = urlsplit(base_url) + url_request = PreparedRequest() + url_request.prepare_url(url, None) + base_request = PreparedRequest() + base_request.prepare_url(base_url, None) + url_parts = urlsplit(url_request.url) + base_parts = urlsplit(base_request.url) url_port = url_parts.port base_port = base_parts.port - except ValueError: + except (RequestException, ValueError): return False url_scheme = url_parts.scheme.lower() diff --git a/tests/test_client_security.py b/tests/test_client_security.py index 9d8d355..6422ad1 100644 --- a/tests/test_client_security.py +++ b/tests/test_client_security.py @@ -35,6 +35,28 @@ def test_prepare_request_accepts_same_origin_absolute_url(): assert prepared.url == "https://marklogic.example/resource" +@pytest.mark.unit +@pytest.mark.parametrize("authorization", ["Basic dXNlcjpwYXNz", "Bearer token"]) +def test_rebuild_auth_strips_credentials_on_cross_origin_redirect(authorization): + client = Client("http://marklogic.example", auth=("user", "password")) + original_request = Request( + "GET", + "http://marklogic.example/resource", + headers={"Authorization": authorization}, + ).prepare() + redirected_request = Request( + "GET", + "https://marklogic.example/resource", + headers={"Authorization": authorization}, + ).prepare() + response = Response() + response.request = original_request + + client.rebuild_auth(redirected_request, response) + + assert "Authorization" not in redirected_request.headers + + @pytest.mark.unit def test_cloud_auth_does_not_retry_401_from_another_origin(): session = Session() @@ -120,6 +142,67 @@ def log_message(self, format, *args): post.assert_not_called() +@pytest.mark.unit +def test_digest_auth_does_not_answer_foreign_origin_challenge_after_redirect(): + class ChallengeHandler(BaseHTTPRequestHandler): + def do_GET(self): + self.server.authorization_headers.append( + self.headers.get("Authorization") + ) + self.send_response(401) + self.send_header( + "WWW-Authenticate", + 'Digest realm="attacker", nonce="nonce", qop="auth"', + ) + self.send_header("Content-Length", "0") + self.end_headers() + + def log_message(self, format, *args): + pass + + class RedirectHandler(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(302) + self.send_header("Location", self.server.redirect_url) + self.send_header("Content-Length", "0") + self.end_headers() + + def log_message(self, format, *args): + pass + + challenge_server = ThreadingHTTPServer( + ("127.0.0.1", 0), ChallengeHandler + ) + challenge_server.authorization_headers = [] + redirect_server = ThreadingHTTPServer(("127.0.0.1", 0), RedirectHandler) + redirect_server.redirect_url = ( + f"http://127.0.0.1:{challenge_server.server_port}/challenge" + ) + threads = [ + Thread(target=server.serve_forever, daemon=True) + for server in (challenge_server, redirect_server) + ] + for thread in threads: + thread.start() + + client = Client( + f"http://127.0.0.1:{redirect_server.server_port}", + digest=("user", "password"), + ) + try: + response = client.get("/redirect") + finally: + client.close() + for server in (redirect_server, challenge_server): + server.shutdown() + server.server_close() + for thread in threads: + thread.join() + + assert response.status_code == 401 + assert challenge_server.authorization_headers == [None] + + @pytest.mark.unit def test_cloud_auth_still_retries_401_from_configured_origin(): session = Session() @@ -145,3 +228,31 @@ def test_cloud_auth_still_retries_401_from_configured_origin(): assert post.call_count == 2 send.assert_called_once() assert response.request.headers["Authorization"] == "Bearer renewed-token" + + +@pytest.mark.unit +def test_cloud_auth_renews_for_idna_normalized_same_origin(): + session = Session() + token_response = Mock(status_code=200) + token_response.json.side_effect = [ + {"access_token": "initial-token"}, + {"access_token": "renewed-token"}, + ] + response = Response() + response.status_code = 401 + response.request = Request( + "GET", "https://xn--bcher-kva.example/resource" + ).prepare() + retry_response = Response() + retry_response.status_code = 200 + + with patch( + "marklogic.cloud_auth.requests.post", return_value=token_response + ) as post: + auth = MarkLogicCloudAuth(session, "https://bücher.example", "api-key") + with patch.object(session, "send", return_value=retry_response) as send: + retried_response = auth._renew_token_if_necessary(response) + + assert retried_response is retry_response + assert post.call_count == 2 + send.assert_called_once()