← Back to PRs

#8129: fix(auth): validate OAuth redirect URI to prevent open redirects

by yubrew open 2026-02-03 16:29 View on GitHub →
commands stale
## Summary Add OAuth redirect URI validation to prevent open redirect attacks (CWE-601). ## The Problem The OAuth redirect URI was controlled by the `CHUTES_OAUTH_REDIRECT_URI` environment variable without validation. An attacker who could set this environment variable (via config injection or other means) could redirect OAuth authorization codes to an attacker-controlled server, enabling account takeover. ## Changes - `src/commands/auth-choice.apply.oauth.ts`: Added `validateOAuthRedirectUri()` function that enforces: - Only localhost and 127.0.0.1 hostnames allowed - Only port 1456 allowed for local OAuth flows - Must use http: protocol (not https for localhost) - Invalid/malformed URLs are rejected with descriptive errors ## Test Plan - [x] `pnpm build && pnpm test` passes - [x] Added `src/commands/auth-choice.apply.oauth.test.ts` with tests for: - Empty input returns default URI - localhost and 127.0.0.1 with correct port allowed - External hostnames rejected - Wrong port rejected - https protocol for localhost rejected - Invalid URLs rejected ## Related - [CWE-601: URL Redirection to Untrusted Site](https://cwe.mitre.org/data/definitions/601.html) - [OAuth 2.0 Security Best Current Practice - Redirect URI Validation](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics#section-4.1.3) --- Internal reference: VULN-023 This PR was generated with the following prompt: > Add OAuth redirect URI validation to prevent open redirect attacks (CWE-601) 🤖 Discovered by [bitsec.ai](https://bitsec.ai) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a `validateOAuthRedirectUri()` guard to the Chutes OAuth flow so the redirect URI can’t be set to an attacker-controlled host via `CHUTES_OAUTH_REDIRECT_URI`. The validator only permits `http://localhost` / `http://127.0.0.1` and constrains the port (intended to be 1456), and a new Vitest suite exercises the allow/deny cases. This fits into the auth onboarding flow in `applyAuthChoiceOAuth()`, where the redirect URI is passed into `loginChutes()` and also shown to users during local/remote OAuth instructions. <h3>Confidence Score: 3/5</h3> - Mostly safe to merge, but the port restriction is not fully enforced as written. - Changes are small and well-tested for many cases, but the core validation has a correctness gap (missing explicit-port handling) that can cause incorrect redirect URIs to be accepted and may break expected behavior. - src/commands/auth-choice.apply.oauth.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs