Skip to content

Normalize environment tokens and report invalid credentials before network requests - #127

Open
aryansri05 wants to merge 4 commits into
prooflinedev:mainfrom
aryansri05:fix/environment-token-whitespace
Open

aryansri05 wants to merge 4 commits into
prooflinedev:mainfrom
aryansri05:fix/environment-token-whitespace

Conversation

@aryansri05

Copy link
Copy Markdown

Problem

When PROOFLINE_TOKEN ends in a line break or carriage return, the HTTP client refuses to build the request, nothing is sent, and the CLI reports an outage:

Command Output on main (5dd916f) Exit
--org ID reviews, --org ID status could not reach <origin>: builder error 1
--org ID ci review … same message 4, the code for "Proofline could not be reached"
any command without --org could not read PROOFLINE_TOKEN's organization (the MCP server could not be reached); name it with --org or PROOFLINE_ORGANIZATION_ID 1

Nothing names the token, and a CI job reads exit 4 as Proofline being down.

Reproduce in bash or zsh against any origin, reachable or not:

PROOFLINE_TOKEN=$'pl_example\n' proofline --org 0190f3a0-0000-7000-8000-000000000001 reviews

$'…' keeps the line break in the value. "$(printf 'pl_example\n')" does not reproduce it, because command substitution strips trailing newlines.

I reproduced this by setting the variable directly. A CI secret saved with its trailing newline, or a .env file with CRLF line endings, are plausible ways to end up there; I did not test either.

Change

  • credentials::load trims whitespace around the token before using it, so the binary and any caller of run are treated alike.
  • Whitespace or a control character inside the token is refused there, before the organization lookup and before any request that carries the token. The message names PROOFLINE_TOKEN and the command exits 1, not 4.
  • A variable that holds only whitespace is refused the same way, instead of being treated as unset and letting the stored sign-in or the job's OIDC identity answer for it.
  • An empty variable stays unset, as before. A workflow that passes a secret it does not have sets it to the empty string, and the OIDC path depends on that.
  • config and setup commands no longer wrap these two errors in a proofline login hint, because proofline login refuses while the variable is set.

The token never appears in either message.

Behaviour change to note

With a working stored sign-in and a whitespace-only PROOFLINE_TOKEN, a Git hook used to review with the stored sign-in. It now prints the token error and, under the default onIncomplete policy, lets the commit or push proceed, as it does for any other credential failure. onIncomplete=block refuses it. I kept the hook's existing policy rather than making token errors a hard failure.

Tests

tests/http.rs runs the binary against a loopback console:

  • padded tokens, no-break spaces included, reach the review API as Bearer <token> (with --org);
  • padded tokens reach the MCP organization lookup and the tool call that follows (without --org);
  • whitespace or a control character inside the token: exit 1, zero requests, the token in neither stream, no login hint, across ci review, reviews and setup status, with and without --org;
  • a whitespace-only token: the same, in an environment where a GitHub Actions OIDC identity was available and must not be used;
  • a console that answers 502 ends ci review with exit 4 only after each request arrived with the trimmed token;
  • a console that refuses connections still ends ci review with exit 4;
  • the existing GitHub Actions OIDC test also runs with PROOFLINE_TOKEN set to the empty string and expects the same requests as with it unset.

The first five fail on main's src/ and pass with this change. The last two pass on both, since they pin behaviour that must not change. src/credentials.rs has unit tests for the same rules.

Checks run

On rustc 1.97.0 (stable), not the pinned 1.100.0, which was not available in my environment:

  • cargo fmt --check
  • cargo clippy --locked --all-targets -- -D warnings
  • cargo test --locked
  • cargo deny check (cargo-deny 0.20.2)

All four passed. The pinned toolchain has not run this change yet; this repository's Checks workflow will be the first to.

…twork requests

A PROOFLINE_TOKEN that ends in a line break or carriage return made the
HTTP client refuse to build every request. Nothing was sent, yet the CLI
reported "could not reach <origin>: builder error", exited 4 from
`ci review`, and without --org asked for an organization ID.

Whitespace around the variable is no longer part of the token. A control
character inside it, or a variable that holds only whitespace, is refused
in credentials::load before the organization lookup and any request, with
a message that names PROOFLINE_TOKEN and exit 1. An empty variable stays
unset, as before.
…t for it

Review follow-ups:

- Whitespace inside PROOFLINE_TOKEN is refused with the control
  characters, before any request that carries the token, instead of
  being sent in a malformed Authorization header.
- The configuration and setup commands no longer wrap a blank or
  malformed PROOFLINE_TOKEN in a `proofline login` hint, which refuses
  while the variable is set.
- The comment in credentials::load says the check precedes requests that
  carry the token; the update check runs earlier and never has it.
- Tests cover no-break and other Unicode spaces around, inside and as
  the whole value.
…decisive

Review follow-ups:

- credentials::load trims the token itself, so a caller of the public
  `run` gets the same treatment as the binary. `environment_token` only
  decides that an empty variable is unset.
- The exit-4 test for a padded token now uses a console that answers
  502 and records each bearer, so it cannot pass on a request that was
  never built. The refused-connection case keeps a clean token.
- The GitHub Actions OIDC test also runs with PROOFLINE_TOKEN set to the
  empty string and expects the same requests as with it unset.
- A C1 control character inside the token is covered, and the comment on
  the whitespace-only test says its fixture only records issuer attempts.
A caller of the public `run` that passes an empty string as `Some` would skip the GitHub Actions sign-in and be refused as a blank token. The doc comment now points at `credentials::environment_token`, which the binary uses.

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