diff --git a/proofline/_transport.py b/proofline/_transport.py index 7f490782..93aea1e3 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -145,6 +145,30 @@ def base_url(value: str) -> str: return value.rstrip("/") +HOSTED_ORIGIN = "https://proofline.sh" + + +def configured_base_url(value: str | None) -> str: + """Resolve the origin: the argument, then PROOFLINE_BASE_URL, then the hosted origin. + + An empty ``PROOFLINE_BASE_URL`` counts as unset, so a CI template that expands a + missing variable to an empty string still reaches the hosted origin. A bad value + read from the environment is reported by name and never echoed, because it can + carry credentials. + """ + if value is not None: + return base_url(value) + configured = os.environ.get("PROOFLINE_BASE_URL", "") + if not configured: + return HOSTED_ORIGIN + try: + return base_url(configured) + except ValueError: + raise ValueError( + "PROOFLINE_BASE_URL must be an HTTPS origin, or HTTP with a literal loopback address." + ) from None + + def invalid_response() -> ProoflineError: return ProoflineError( 0, "urn:proofline:sdk:invalid-response", "Proofline returned an invalid API response." diff --git a/proofline/client.py b/proofline/client.py index dd5106f6..739f003f 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -218,11 +218,11 @@ def __init__( self, token: str | SecretStr | None = None, *, - base_url: str = "https://proofline.sh", + base_url: str | None = None, timeout: float = 30, ) -> None: - self._api = _configuration(token, base_url, timeout) - self._base_url = _transport.base_url(base_url) + self._base_url = _transport.configured_base_url(base_url) + self._api = _configuration(token, self._base_url, timeout) self._http = httpx.Client( base_url=self._base_url, auth=_transport.BearerAuth(self._api.token), @@ -588,11 +588,11 @@ def __init__( self, token: str | SecretStr | None = None, *, - base_url: str = "https://proofline.sh", + base_url: str | None = None, timeout: float = 30, ) -> None: - self._api = _configuration(token, base_url, timeout) - self._base_url = _transport.base_url(base_url) + self._base_url = _transport.configured_base_url(base_url) + self._api = _configuration(token, self._base_url, timeout) self._http = httpx.AsyncClient( base_url=self._base_url, auth=_transport.BearerAuth(self._api.token), diff --git a/scripts/check.py b/scripts/check.py index b98a5a80..73370a82 100644 --- a/scripts/check.py +++ b/scripts/check.py @@ -21,7 +21,7 @@ def python_at(directory: Path) -> Path: def command(*args: str, cwd: Path = ROOT) -> None: env = dict(os.environ) - for name in ("PYTHONPATH", "PYTHONHOME", "PROOFLINE_TOKEN"): + for name in ("PYTHONPATH", "PYTHONHOME", "PROOFLINE_TOKEN", "PROOFLINE_BASE_URL"): env.pop(name, None) for name in tuple(env): if name.startswith("UV_"): diff --git a/tests/test_http.py b/tests/test_http.py index 7b922c34..ec1e99fe 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -2534,3 +2534,49 @@ def respond(request: Request, index: int) -> Reply: assert len(exported.rows) == 1 assert exported.rows[0]["claim"] == row["claim"] assert exported.next_cursor == "next-page" + + +@MODES +async def test_base_url_defaults_to_the_environment_then_the_hosted_origin( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + def respond(request: Request, index: int) -> Reply: + return Reply(body=page()) + + with Server(respond) as server: + monkeypatch.setenv("PROOFLINE_BASE_URL", server.url) + if mode == "sync": + with ProoflineClient(SecretStr(TOKEN)) as client: + await call(client, "list_reviews", ORG) + else: + async with AsyncProoflineClient(SecretStr(TOKEN)) as client: + await call(client, "list_reviews", ORG) + assert server.requests[0].path.startswith(f"/api/v1/organizations/{ORG}/reviews") + assert server.requests[0].headers["Authorization"] == f"Bearer {TOKEN}" + + with Server(respond) as explicit_server: + monkeypatch.setenv("PROOFLINE_BASE_URL", "http://127.0.0.1:9") + async with session(mode, explicit_server.url) as client: + await call(client, "list_reviews", ORG) + assert len(explicit_server.requests) == 1 + + monkeypatch.delenv("PROOFLINE_BASE_URL", raising=False) + assert _transport.configured_base_url(None) == "https://proofline.sh" + monkeypatch.setenv("PROOFLINE_BASE_URL", "") + assert _transport.configured_base_url(None) == "https://proofline.sh" + assert _transport.configured_base_url("https://staging.example") == "https://staging.example" + + for bad in ( + "http://example.com", + "https://user:secret@example.com", + "https://example.com/path", + "https://example.com?query=1", + ): + monkeypatch.setenv("PROOFLINE_BASE_URL", bad) + with pytest.raises(ValueError) as refused: + _transport.configured_base_url(None) + assert str(refused.value).startswith("PROOFLINE_BASE_URL must be") + assert "secret" not in str(refused.value) + + with pytest.raises(ValueError): + _transport.configured_base_url("http://example.com")