Skip to content

Commit 1649dd3

Browse files
authored
fix: Session management issue that causes malformed redirect URLs (#3011)
1 parent 043850b commit 1649dd3

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Parse-Dashboard/Authentication.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,22 @@ function initialize(app, options) {
7676
csrf(),
7777
(req,res,next) => {
7878
let redirect = 'apps';
79+
let originalRedirect = null;
7980
if (req.body.redirect) {
80-
redirect = req.body.redirect.charAt(0) === '/' ? req.body.redirect.substring(1) : req.body.redirect
81+
originalRedirect = req.body.redirect;
82+
// Validate redirect to prevent open redirect vulnerability
83+
if (originalRedirect.includes('://') || originalRedirect.startsWith('//')) {
84+
// Reject absolute URLs and protocol-relative URLs
85+
redirect = 'apps';
86+
originalRedirect = null;
87+
} else {
88+
// Strip leading slash from redirect to prevent double slashes
89+
redirect = originalRedirect.charAt(0) === '/' ? originalRedirect.substring(1) : originalRedirect;
90+
}
8191
}
8292
return passport.authenticate('local', {
8393
successRedirect: `${self.mountPath}${redirect}`,
84-
failureRedirect: `${self.mountPath}login${req.body.redirect ? `?redirect=${req.body.redirect}` : ''}`,
94+
failureRedirect: `${self.mountPath}login${originalRedirect ? `?redirect=${originalRedirect}` : ''}`,
8595
failureFlash : true
8696
})(req, res, next)
8797
},

Parse-Dashboard/app.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,26 @@ You have direct access to the Parse database through function calls, so you can
10621062
}
10631063

10641064
app.get('/login', csrf(), function(req, res) {
1065-
const redirectURL = req.url.includes('?redirect=') && req.url.split('?redirect=')[1].length > 1 && req.url.split('?redirect=')[1];
1065+
let redirectURL = null;
1066+
try {
1067+
const url = new URL(req.url, 'http://localhost');
1068+
redirectURL = url.searchParams.get('redirect');
1069+
} catch (error) {
1070+
console.warn('Invalid URL in login redirect:', error.message);
1071+
}
10661072
if (!users || (req.user && req.user.isAuthenticated)) {
1073+
// Validate and sanitize redirect URL to prevent open redirect vulnerability
1074+
if (redirectURL) {
1075+
// Reject absolute URLs and protocol-relative URLs
1076+
if (redirectURL.includes('://') || redirectURL.startsWith('//')) {
1077+
redirectURL = null;
1078+
} else {
1079+
// Strip leading slash to prevent double slashes
1080+
if (redirectURL.charAt(0) === '/') {
1081+
redirectURL = redirectURL.substring(1);
1082+
}
1083+
}
1084+
}
10671085
return res.redirect(`${mountPath}${redirectURL || 'apps'}`);
10681086
}
10691087

0 commit comments

Comments
 (0)