#8129: fix(auth): validate OAuth redirect URI to prevent open redirects
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
#8389: fix(test): eliminate flaky chutes-oauth 'bad port' race condition (...
by Glucksberg · 2026-02-04
78.3%
#19756: fix(security): OC-101 refresh token rotation enforcement — Aether A...
by aether-ai-agent · 2026-02-18
74.7%
#8124: fix(browser): add path validation for file upload and download
by yubrew · 2026-02-03
73.0%
#22105: feat(auth): add refreshable Anthropic OAuth login flow
by sauerdaniel · 2026-02-20
72.5%
#8494: fix(test): resolve flaky port race condition in chutes-oauth.test.ts
by gavinbmoore · 2026-02-04
72.2%
#3591: CLI: add OpenAI-compatible endpoint auth choice
by surak · 2026-01-28
71.7%
#23814: Gateway: block unauthenticated tool-invocation HTTP surfaces
by bmendonca3 · 2026-02-22
71.2%
#11812: fix: MSTeams attachment fetch follows redirects before allowlist ch...
by coygeek · 2026-02-08
71.2%
#8339: fix(tts): validate ElevenLabs base URL against allowlist
by yubrew · 2026-02-03
70.7%
#14197: fix(security): harden browser API auth, token comparisons, and hook...
by leecarollyn-gif · 2026-02-11
70.7%