mirror of
https://github.com/requarks/wiki.git
synced 2026-08-26 05:07:30 -05:00
fix: validate loginRedirect cookie to prevent open redirect (#7923)
The loginRedirect cookie value was used directly in res.redirect() and window.location.replace() without validation, allowing redirection to arbitrary external URLs. Added validation to ensure the redirect target is a relative path before use. Co-authored-by: kolega.dev <faizan@kolega.ai>
This commit is contained in:
@@ -73,16 +73,22 @@ router.all('/login/:strategy/callback', async (req, res, next) => {
|
||||
res.cookie('jwt', authResult.jwt, commonHelper.getCookieOpts())
|
||||
|
||||
const loginRedirect = req.cookies['loginRedirect']
|
||||
const isValidRedirect = loginRedirect && loginRedirect.startsWith('/') && !loginRedirect.startsWith('//') && !loginRedirect.includes('://')
|
||||
if (loginRedirect === '/' && authResult.redirect) {
|
||||
res.clearCookie('loginRedirect')
|
||||
res.redirect(authResult.redirect)
|
||||
} else if (loginRedirect) {
|
||||
} else if (isValidRedirect) {
|
||||
res.clearCookie('loginRedirect')
|
||||
res.redirect(loginRedirect)
|
||||
} else if (authResult.redirect) {
|
||||
res.redirect(authResult.redirect)
|
||||
} else {
|
||||
res.redirect('/')
|
||||
if (loginRedirect) {
|
||||
res.clearCookie('loginRedirect')
|
||||
}
|
||||
if (authResult.redirect) {
|
||||
res.redirect(authResult.redirect)
|
||||
} else {
|
||||
res.redirect('/')
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
next(err)
|
||||
|
||||
Reference in New Issue
Block a user