t3code icon indicating copy to clipboard operation
t3code copied to clipboard

fix(connect): reject malformed state/PKCE challenge on the /connect page

Open arhxam opened this issue 4 months ago • 2 comments

What Changed

readConnectAuthorizeRequest (in packages/shared, used by the /connect page and the CLI token exchange) now validates that the state and challenge fragment values have the exact base64url shape the CLI generates, instead of only checking that they are non-empty. Anything malformed returns null.

  • state = encodeBase64Url(16 bytes) → 22 base64url chars
  • PKCE challenge = encodeBase64Url(SHA-256 digest) → 43 base64url chars

Added unit tests for a stray space, a non-base64url character (│), a wrong-length value, and that the well-formed pair still round-trips.

Why

Closes #4934.

The headless flow prints a /connect#state=...&challenge=... URL. When that URL wraps in a narrow terminal (tmux/screen pane borders, native text selection), a stray character can be pasted into the fragment. The old check accepted it, the full browser authorization flow ran, and the resulting code was only rejected at the very end by the waiting CLI with "That code belongs to a different connect request" — a message that never hints at URL corruption, long after the point where it could have been caught.

Both values have a known, generated shape, so the page can fail closed up front. The existing /connect surface already renders a "this connect link is incomplete — re-run t3 connect and open the freshly printed URL" message whenever the parsed request is null, so malformed URLs now get that actionable guidance before the browser flow rather than a confusing failure after it. No behavior change for well-formed URLs (verified: the full packages/shared suite and CliTokenManager.test.ts, which feed real generated values through the validator, still pass).

Checklist

  • [x] This PR is small and focused
  • [x] I explained what changed and why
  • [x] No UI changes (the existing null-request message is reused)

[!NOTE] Low Risk Validation-only change in shared connect auth parsing; well-formed CLI URLs are unchanged and malformed inputs fail earlier with clearer UX.

Overview readConnectAuthorizeRequest now rejects state and challenge unless they match the CLI’s fixed base64url shape (22 chars for state, 43 for the PKCE challenge), not merely non-empty after trim.

Corrupted connect URLs—spaces from terminal wrapping, invalid characters from pane borders, truncated values—return null immediately so the /connect page can show the existing “re-run t3 connect” guidance instead of running the full browser OAuth flow and failing later in the CLI.

Tests cover stray spaces, non-base64url characters, wrong length, and confirm well-formed URLs still round-trip.

Reviewed by Cursor Bugbot for commit a723ffd89f51a9841f789b367ff374b46156c4a4. Bugbot is set up for automated code reviews on this repo. Configure here.

[!NOTE]

Reject malformed state/PKCE challenge values in readConnectAuthorizeRequest

Adds strict validation to readConnectAuthorizeRequest in connectAuth.ts so that non-empty but malformed state or challenge values are rejected. Previously, only falsy values triggered a null return; now both fields must be valid base64url strings of exact expected lengths (22 and 43 characters respectively). A BASE64URL_PATTERN regex and an isBase64UrlOfLength helper enforce this.

📊 Macroscope summarized a723ffd. 1 file reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted

🗂️ Filtered Issues

No issues evaluated.

arhxam avatar Jul 30 '26 17:07 arhxam

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6dffaedc-dcb4-4e35-aab9-262656b0de4b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 30 '26 17:07 coderabbitai[bot]

Approvability

Verdict: Needs human review

2 blocking correctness issues found. This PR modifies OAuth/authentication validation logic (connectAuth) and has two unresolved HIGH severity review comments identifying gaps in the validation: non-canonical base64url characters are accepted, and trimming before validation defeats whitespace rejection. Security-sensitive auth code with substantive unresolved concerns requires human review.

You can customize Macroscope's approvability policy. Learn more.

macroscopeapp[bot] avatar Jul 30 '26 17:07 macroscopeapp[bot]