Skip to content

fix(auth): a wrong-state request to the OAuth loopback no longer ends the login - #582

Merged
miguel-heygen merged 4 commits into
mainfrom
credlock/loopback-wrong-state
Nov 16, 2026
Merged

miguel-heygen merged 4 commits into
mainfrom
credlock/loopback-wrong-state

Conversation

@miguel-heygen

Copy link
Copy Markdown
Contributor

What changed

During heygen auth login --oauth, a request to the loopback callback with a missing or wrong state now gets its 400 invalid_state page and nothing else. It delivers no result and leaves the listener running. The login keeps waiting for the real callback, cancellation, or the existing timeout.

Before, handleCallback treated that request as the login's outcome: it delivered ErrStateMismatch through the one-shot result and shut the listener down. Any process on the machine that found the port could end someone's login with one unauthenticated GET. The real callback could then never be delivered.

The state check is unchanged. It is still a constant-time comparison, and it still runs before the error and code branches. So only a request with the right state can end the flow: success, an IdP error, or a missing code.

ErrStateMismatch no longer has a producer, so it is removed with its oauth_state_mismatch failure reason. The tests that pin the reason vocabulary drop it too.

What I measured

fails without the fix: TestStartLoopbackServer_WrongStateLeavesLoginWaiting on main.

  • The test sends four GETs, each of which must get a 400 invalid_state page:
    • no state;
    • a wrong state;
    • an error with a wrong state;
    • an error with no state.
  • It then sends a correct-state callback, whose code must be delivered.
  • On main it fails at the second request, connection refused, because the first one had already shut the listener.

On this branch, with Go 1.26.10:

  • go test -count=1 ./... passes.
  • go test -count=3 passes for the new test, the sentinel table and the happy path.
  • go vet is clean, and gofmt -l is clean on the changed packages.

What I did NOT exercise

  • A real browser login against the IdP.
  • golangci-lint locally. CI runs it.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving.

Change. A request whose state is missing or wrong now gets only its 400 invalid_state page and returns. It no longer calls deliver or shutdown, so it can't take the one-shot result or close the listener. The constant-time state check still runs before the error and code branches. That leaves exactly four ways the login can end, and only the first two can come from a request:

  • a request with the right state (code, IdP error or missing code);
  • a server error;
  • cancellation;
  • the 5-minute timeout.

Unlimited guesses. Before, a single wrong guess closed the listener. Now an attacker can keep guessing, but GenerateState draws 32 bytes from crypto/rand (256 bits), so that's no practical gain.

Removing ErrStateMismatch. It lives under internal/, so nothing outside the module can import it. The only remaining "state mismatch" text is in comments.

Verified at a656ef1 (Go 1.26.10):

  • go test -count=1 ./internal/auth/oauth/ ./cmd/heygen/ passes.
  • With the old deliver + go lb.shutdown() restored in that branch, TestStartLoopbackServer_WrongStateLeavesLoginWaiting fails on its second GET with connection refused, as the body says.

Non-blocking:

  • The LoopbackResult doc comment (loopback.go:61) still lists "state mismatch" among the Err cases.
  • A person's own stale tab that hits the callback with an old state now waits out the timeout instead of failing fast. That's the right trade, and it might be worth a line in the 400 page's text ("you can close this tab; the login is still waiting").

— Rames

@miguel-heygen
miguel-heygen force-pushed the credlock/loopback-wrong-state branch from ebb99c5 to b4ea8ac Compare November 16, 2026 02:15
@miguel-heygen
miguel-heygen enabled auto-merge (squash) November 16, 2026 03:56
@miguel-heygen
miguel-heygen merged commit 0a75203 into main Nov 16, 2026
12 checks passed
@miguel-heygen
miguel-heygen deleted the credlock/loopback-wrong-state branch November 16, 2026 03:57
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.

2 participants