Skip to content

Reject tokens that cannot be sent, so they never appear in an exception - #159

Open
JoshuaF-ML wants to merge 1 commit into
prooflinedev:mainfrom
JoshuaF-ML:fix/reject-non-ascii-token
Open

JoshuaF-ML wants to merge 1 commit into
prooflinedev:mainfrom
JoshuaF-ML:fix/reject-non-ascii-token

Conversation

@JoshuaF-ML

Copy link
Copy Markdown

Problem

token_value rejects empty tokens and whitespace but accepts non-ASCII characters, such as a byte order mark left by a token file (str.strip() keeps it) or a zero-width space from copy and paste.

The token then fails while the Authorization header is encoded:

  • export_reviews (sync and async) and mcp.list_organizations (sync) raise a raw UnicodeEncodeError. Its repr() and .object hold Bearer <token>.
  • list_repositories (sync) raises urn:proofline:sdk:invalid-response, which does not point at the token, and keeps the encoding error in its context chain.

The standard traceback text did not show the token. Anything that logs repr(error) or inspects the exception would. The README says the clients redact the token from diagnostic output.

Approach

token_value now accepts only visible ASCII (! to ~) and raises ValueError, without the token, when the client is constructed. One check covers every request path. It also refuses control characters and DEL; DEL previously reached the server in my local test.

Reproduce

from uuid import UUID

from proofline import ProoflineClient

token = "\ufefffake-token"  # a byte order mark that str.strip() keeps

try:
    with ProoflineClient(token, base_url="http://127.0.0.1:9") as client:
        client.export_reviews(UUID(int=1), "reviews", from_time="a", to_time="b")
except Exception as error:
    print(repr(error))

Before:

UnicodeEncodeError('ascii', 'Bearer \ufefffake-token', 7, 8, 'ordinal not in range(128)')

After:

ValueError('Pass a Proofline token of visible ASCII characters or set PROOFLINE_TOKEN.')

Verification

On Windows 11 with Python 3.12:

  • The new test has 20 cases (five bad characters, argument and PROOFLINE_TOKEN, sync and async). All 20 fail without the fix and pass with it.
  • ruff check ., ruff format --check . and mypy proofline pass.
  • pytest -q: 254 tests. The latest run had 252 passed and 2 failed, and an earlier run with this change passed all 254. Both failures are the sync cases of test_body_stream_deadline_does_not_turn_authority_into_timeout, which also failed on the untouched clone and is intermittent on this machine.
  • I did not run scripts/check.py.

I used an AI coding agent (Claude Code) to find, fix and test this.

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 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant