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
8 changes: 6 additions & 2 deletions proofline/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
28 changes: 28 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down