From 9fb0d8a06fd8e55f6967829eb9f71395866de5e3 Mon Sep 17 00:00:00 2001 From: Joshua Thomas Johnson <283407315+JoshuaF-ML@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:42:50 +0530 Subject: [PATCH] Reject tokens that cannot be sent in the Authorization header A non-ASCII token passed token_value and then raised UnicodeEncodeError while the header was encoded. That error holds the whole header, so its repr exposed the bearer token. Accept only visible ASCII and raise ValueError at construction. Co-Authored-By: Claude Sonnet 5.5 --- proofline/_transport.py | 8 ++++++-- tests/test_http.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/proofline/_transport.py b/proofline/_transport.py index d3e99302..3dc96f57 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -117,8 +117,12 @@ def token_value(token: str | SecretStr | None) -> SecretStr: else SecretStr(token if token is not None else os.environ.get("PROOFLINE_TOKEN", "")) ) value = secret.get_secret_value() - if not value or any(character.isspace() for character in value): - raise ValueError("Pass a Proofline token or set PROOFLINE_TOKEN.") + # Only visible ASCII fits the Authorization header. The error raised while + # encoding anything else holds the whole header, so refuse it here. + if not value or any(not "!" <= character <= "~" for character in value): + raise ValueError( + "Pass a Proofline token of visible ASCII characters or set PROOFLINE_TOKEN." + ) return secret diff --git a/tests/test_http.py b/tests/test_http.py index 68c6228e..1ebc3ac2 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1883,6 +1883,34 @@ def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.Monkey pass +@MODES +@pytest.mark.parametrize("source", ["argument", "environment"]) +@pytest.mark.parametrize( + "token", + [ + "\ufeff" + TOKEN, # byte order mark from a token file; str.strip() keeps it + TOKEN + "\u200b", # zero-width space from copy and paste + TOKEN + "\u00e9", + TOKEN + "\x7f", + TOKEN + "\x01", + ], + ids=["bom", "zero-width-space", "latin-1", "delete", "control"], +) +async def test_unsendable_token_is_rejected_without_exposing_it( + mode: str, source: str, token: str, monkeypatch: pytest.MonkeyPatch +) -> None: + # An HTTP header cannot carry these, and the error raised while encoding one holds the token. + argument = token if source == "argument" else None + if source == "environment": + monkeypatch.setenv("PROOFLINE_TOKEN", token) + client_type = ProoflineClient if mode == "sync" else AsyncProoflineClient + with pytest.raises(ValueError) as raised: + client_type(argument) + assert type(raised.value) is ValueError + assert raised.value.__context__ is None + assert TOKEN not in repr(raised.value) + + @MODES @pytest.mark.parametrize("size", [131_072, 131_073, 150_000, 8 * 1024 * 1024 + 1]) async def test_csv_export_preserves_large_cells(mode: str, size: int) -> None: