Node.js based forum software built for the modern web
fix: use normalizeReturnToPath for redirect validation
Julian Lamd99545fa74fa
diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.jsindex 593a112582..260f4fd26c 100644--- a/src/controllers/activitypub/index.js+++ b/src/controllers/activitypub/index.js@@ -48,7 +48,7 @@ Controller.fetch = async (req, res, next) => {}default:- return helpers.redirect(res, url.href, false);+ return helpers.redirect(res, { external: url.href });}}@@ -59,13 +59,13 @@ Controller.fetch = async (req, res, next) => {url = new URL(`outgoing?url=${encodeURIComponent(url.href)}`, nconf.get('url'));}- helpers.redirect(res, url.href, false);+ helpers.redirect(res, { external: url.href });} catch (e) {if (!url || !url.href) {return next();}activitypub.helpers.log(`[activitypub/fetch] Invalid URL received: ${url}`);- helpers.redirect(res, url.href, false);+ helpers.redirect(res, { external: url.href });}};diff --git a/src/controllers/helpers.js b/src/controllers/helpers.jsindex 398c7dee75..2e1b252ab3 100644--- a/src/controllers/helpers.js+++ b/src/controllers/helpers.js@@ -181,6 +181,11 @@ helpers.redirect = function (res, url, permanent) {if (res.locals.isAPI) {res.set('X-Redirect', encodeURIComponent(url)).status(200).json(url);} else {+ // Reject unsafe redirect URLs — fall back to home page+ if (!helpers.normalizeReturnToPath(url)) {+ winston.warn(`[security] Unsafe redirect attempted: ${url}`);+ url = '/';+ }res.redirect(permanent ? 308 : 307, prependRelativePath(url));}};diff --git a/src/routes/authentication.js b/src/routes/authentication.jsindex bfb01a124e..2d323ec43d 100644--- a/src/routes/authentication.js+++ b/src/routes/authentication.js@@ -98,7 +98,10 @@ Auth.reloadRoutes = async function (params) {opts.state = req.session.ssoState;}if (req.query.next) {- req.session.next = req.query.next;+ const next = helpers.normalizeReturnToPath(req.query.next);+ if (next) {+ req.session.next = next;+ }}// Allow SSO plugins to override/append options (for use in passport prototype authorizationParams)