Skip to content

fix: validate returnTo parameter to prevent open redirect#46

Open
nitishagar wants to merge 1 commit into
jetkvm:devfrom
nitishagar:fix-7
Open

fix: validate returnTo parameter to prevent open redirect#46
nitishagar wants to merge 1 commit into
jetkvm:devfrom
nitishagar:fix-7

Conversation

@nitishagar

Copy link
Copy Markdown

Add URL validation for the returnTo parameter in OIDC authentication flow to prevent open redirect vulnerabilities.

  • Add isValidReturnToUrl() helper function to validate URLs
  • Validate returnTo at input point (when storing in session)
  • Validate returnTo at output points (before redirects)
  • Reject external URLs with BadRequestError
  • Use safe fallback for invalid URLs at redirect points

Fixes #7

@CLAassistant

CLAassistant commented Nov 20, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@IDisposable IDisposable left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is an extremely good idea. :)

Comment thread src/oidc.ts Outdated
Comment on lines +185 to +209
// Validate returnTo before redirecting (defense in depth)
if (!isValidReturnToUrl(returnTo, APP_HOSTNAME)) {
console.warn("Invalid returnTo URL detected at redirect point:", returnTo);
// Fall back to safe default
const safeUrl = new URL(`${APP_HOSTNAME}/devices`);
safeUrl.searchParams.append("tempToken", tempToken);
safeUrl.searchParams.append("deviceId", deviceId);
safeUrl.searchParams.append("oidcGoogle", tokenSet.id_token.toString());
safeUrl.searchParams.append("clientId", process.env.GOOGLE_CLIENT_ID);
return res.redirect(safeUrl.toString());
}

const url = new URL(returnTo);
url.searchParams.append("tempToken", tempToken);
url.searchParams.append("deviceId", deviceId);
url.searchParams.append("oidcGoogle", tokenSet.id_token.toString());
url.searchParams.append("clientId", process.env.GOOGLE_CLIENT_ID);
return res.redirect(url.toString());
}
// Validate returnTo before redirecting (defense in depth)
if (!isValidReturnToUrl(returnTo, APP_HOSTNAME)) {
console.warn("Invalid returnTo URL detected at redirect point:", returnTo);
// Fall back to safe default
return res.redirect(`${APP_HOSTNAME}/devices`);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not needed, as we are the one setting the session cookie, and we sign them with our COOKIE_SECRET.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

👍 - removed both output-side checks in Callback() (this one in the no-device branch, and the equivalent one in the device-adopt branch). The load-bearing input validation in Google() stays.

Add URL validation for the returnTo parameter in OIDC authentication
flow to prevent open redirect vulnerabilities.

- Add isValidReturnToUrl() helper function to validate URLs
- Validate returnTo at input point (when storing in session)
- Export isValidReturnToUrl so it can be unit-tested directly
- Reject external URLs with BadRequestError

The returnTo is validated once at the only write site (the Google()
handler); the session cookie is HMAC-signed, so the value read at
redirect time is the value that was validated at write time. Output-side
re-validation in Callback() was therefore tautologically redundant and
is removed.

Fixes jetkvm#7
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.

Open redirect via returnTo

4 participants