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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __pycache__
.venv
venv
.idea
.codesight/

.DS_Store
publishing_markLogic_client.token
6 changes: 5 additions & 1 deletion marklogic/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from marklogic.rows import RowManager
from marklogic.transactions import TransactionManager, Transaction
from requests.auth import HTTPDigestAuth
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse


class Client(requests.Session):
Expand All @@ -34,6 +34,10 @@ def __init__(
self.verify = verify

if cloud_api_key:
if base_url and urlparse(base_url).scheme.lower() != "https":
raise ValueError(
"base_url must use HTTPS when cloud_api_key is provided."
)
port = 443 if port == 0 else port
scheme = "https"

Expand Down
26 changes: 23 additions & 3 deletions marklogic/cloud_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

import logging
import requests
import warnings
from requests import Response, Session, Request
from requests.auth import AuthBase
from urllib.parse import urljoin
from urllib.parse import urljoin, urlparse

from marklogic.internal.url import is_same_origin

logger = logging.getLogger(__name__)
DEFAULT_TOKEN_REQUEST_TIMEOUT = 30


def _no_auth(request: Request) -> Request:
"""Return requests unchanged so token bootstrap bypasses session auth."""
return request


class MarkLogicCloudAuth(AuthBase):
Expand All @@ -35,6 +42,13 @@ def __init__(
self._base_url = base_url
self._api_key = api_key
self._cloud_token_duration = cloud_token_duration
if self._session.verify is False:
warnings.warn(
"verify=False disables TLS certificate validation for cloud token "
"requests and can expose API keys to man-in-the-middle attacks.",
UserWarning,
stacklevel=2,
)
self._generate_token()

# See https://docs.python-requests.org/en/latest/user/advanced/#event-hooks for
Expand All @@ -55,11 +69,14 @@ def _generate_token(self):
if self._cloud_token_duration > 0:
params["duration"] = self._cloud_token_duration

response = requests.post(
response = Session.request(
Comment thread
RitaChen609 marked this conversation as resolved.
Resolved
self._session,
"POST",
urljoin(self._base_url, "/token"),
data={"grant_type": "apikey", "key": self._api_key},
verify=self._session.verify,
auth=_no_auth,
params=params,
timeout=DEFAULT_TOKEN_REQUEST_TIMEOUT,
)

if response.status_code != 200:
Expand All @@ -71,6 +88,9 @@ def _generate_token(self):

def _renew_token_if_necessary(self, response: Response, *args, **kwargs):
request_url = response.request.url if response.request else None
if request_url and urlparse(request_url).path == "/token":
return response

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)
Expand Down
100 changes: 34 additions & 66 deletions tests/test_client_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,82 +64,50 @@ def test_cloud_auth_does_not_retry_401_from_another_origin():
token_response.json.return_value = {"access_token": "initial-token"}

with patch(
"marklogic.cloud_auth.requests.post", return_value=token_response
) as post:
"marklogic.cloud_auth.Session.request", return_value=token_response
) as request_mock:
auth = MarkLogicCloudAuth(session, "https://marklogic.example", "api-key")
post.reset_mock()
request_mock.reset_mock()

request = Request("GET", "https://attacker.example/resource").prepare()
prepared_request = Request("GET", "https://attacker.example/resource").prepare()
response = Response()
response.status_code = 401
response.request = request
response.request = prepared_request

with patch.object(session, "send") as send:
assert auth._renew_token_if_necessary(response) is None

post.assert_not_called()
request_mock.assert_not_called()
send.assert_not_called()
assert "Authorization" not in request.headers
assert "Authorization" not in prepared_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()

session = Session()
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()
with patch(
"marklogic.cloud_auth.Session.request", return_value=token_response
) as request_mock:
client = Client("https://marklogic.example", cloud_api_key="api-key")
request_mock.reset_mock()

original_request = Request("GET", "https://marklogic.example/resource").prepare()
redirected_request = Request(
"GET",
"https://attacker.example/resource",
headers={"Authorization": "Bearer initial-token"},
).prepare()
response = Response()
response.request = original_request

client.rebuild_auth(redirected_request, response)
client.close()

request_mock.assert_not_called()
assert "Authorization" not in redirected_request.headers


@pytest.mark.unit
Expand Down Expand Up @@ -218,14 +186,14 @@ def test_cloud_auth_still_retries_401_from_configured_origin():
retry_response.status_code = 200

with patch(
"marklogic.cloud_auth.requests.post", return_value=token_response
) as post:
"marklogic.cloud_auth.Session.request", return_value=token_response
) as request:
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
assert request.call_count == 2
send.assert_called_once()
assert response.request.headers["Authorization"] == "Bearer renewed-token"

Expand All @@ -247,12 +215,12 @@ def test_cloud_auth_renews_for_idna_normalized_same_origin():
retry_response.status_code = 200

with patch(
"marklogic.cloud_auth.requests.post", return_value=token_response
) as post:
"marklogic.cloud_auth.Session.request", return_value=token_response
) as request:
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
assert request.call_count == 2
send.assert_called_once()
143 changes: 143 additions & 0 deletions tests/test_cloud_auth_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Copyright (c) 2023-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.


import pytest

from marklogic import Client
from marklogic.cloud_auth import MarkLogicCloudAuth
from marklogic.cloud_auth import DEFAULT_TOKEN_REQUEST_TIMEOUT


@pytest.fixture(autouse=True)
def prepare_test_database():
"""Override integration fixture from conftest for isolated unit tests."""
return


class _TokenResponse:
status_code = 200
text = ""

@staticmethod
def json():
return {"access_token": "token-value"}


class _RequestStub:
def __init__(self, url):
self.url = url
self.headers = {}


class _ResponseStub:
def __init__(self, status_code, url):
self.status_code = status_code
self.request = _RequestStub(url)


def test_cloud_api_key_rejects_http_base_url():
with pytest.raises(ValueError) as err:
Client(base_url="http://example.com", cloud_api_key="k")

assert "base_url must use HTTPS when cloud_api_key is provided." == str(
err.value
)


def test_cloud_token_request_has_timeout(monkeypatch):
observed_method = None
observed_timeout = None
observed_url = None
observed_auth = None

def fake_request(self, method, url, *args, **kwargs):
nonlocal observed_method
nonlocal observed_timeout
nonlocal observed_url
nonlocal observed_auth
observed_method = method
observed_timeout = kwargs.get("timeout")
observed_url = url
observed_auth = kwargs.get("auth")
return _TokenResponse()

monkeypatch.setattr("requests.sessions.Session.request", fake_request)

Client(host="example.com", cloud_api_key="k")

assert "POST" == observed_method
assert DEFAULT_TOKEN_REQUEST_TIMEOUT == observed_timeout
assert "https://example.com:443/token" == observed_url
request = _RequestStub("https://example.com:443/token")
assert callable(observed_auth)
observed_auth(request)
assert "Authorization" not in request.headers


def test_cloud_token_request_ignores_base_path(monkeypatch):
observed_url = None

def fake_request(self, method, url, *args, **kwargs):
nonlocal observed_url
observed_url = url
return _TokenResponse()

monkeypatch.setattr("requests.sessions.Session.request", fake_request)

Client(
host="example.com",
cloud_api_key="k",
base_path="/ml/test/marklogic/manage",
)

assert "https://example.com:443/token" == observed_url


def test_cloud_api_key_warns_when_verify_disabled(monkeypatch):
def fake_request(self, method, url, *args, **kwargs):
return _TokenResponse()

monkeypatch.setattr("requests.sessions.Session.request", fake_request)

with pytest.warns(UserWarning, match="verify=False"):
Client(host="example.com", cloud_api_key="k", verify=False)


def test_renewal_guard_not_reset_by_token_response():
auth = MarkLogicCloudAuth.__new__(MarkLogicCloudAuth)
auth.resent_request_on_401 = False
auth._access_token = "old-token"
auth._base_url = "https://example.com:443"

first_401 = _ResponseStub(401, "https://example.com:443/v1/search")
token_200 = _ResponseStub(200, "https://example.com:443/token")
second_401 = _ResponseStub(401, "https://example.com:443/v1/search")
token_renewals = 0

class _SessionStub:
def __init__(self):
self.send_calls = 0

def send(self, request, *args, **kwargs):
self.send_calls += 1
if self.send_calls > 1:
raise AssertionError("Token was renewed more than once")
auth._renew_token_if_necessary(second_401)
return second_401

def fake_generate_token():
nonlocal token_renewals
token_renewals += 1
auth._access_token = f"new-token-{token_renewals}"
auth._renew_token_if_necessary(token_200)

auth._session = _SessionStub()
auth._generate_token = fake_generate_token

retried_response = auth._renew_token_if_necessary(first_401)

assert retried_response is second_401
assert 1 == token_renewals
assert 1 == auth._session.send_calls
assert False is auth.resent_request_on_401
assert "Bearer new-token-1" == first_401.request.headers["Authorization"]
Loading