Skip to content

Node.js based forum software built for the modern web

fix: dont allow redirect to unsafe url via /ap?resource path

Barış Soner Uşaklı373f3f35c7ad
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index 3bcb69888e..c0a0d75cae 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -242,13 +242,16 @@ ajaxify.widgets = { render: render };
if (data.responseJSON && data.responseJSON.external) {
// this is used by sso plugins to redirect to the auth route
// cant use ajaxify.go for /auth/sso routes
- window.location.href = data.responseJSON.external;
+ window.location.href = utils.isSafeHref(data.responseJSON.external) ?
+ data.responseJSON.external : config.relative_path + '/';
} else if (typeof data.responseJSON === 'string') {
ajaxifyTimer = undefined;
+ const targetHref = utils.isSafeHref(data.responseJSON) ?
+ data.responseJSON : config.relative_path + '/';
if (data.responseJSON.startsWith('http://') || data.responseJSON.startsWith('https://')) {
- window.location.href = data.responseJSON;
+ window.location.href = targetHref;
} else {
- ajaxify.go(data.responseJSON.slice(1), callback, quiet);
+ ajaxify.go(targetHref.slice(1), callback, quiet);
}
}
}
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 69fc4310a1..a377ed7097 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -169,6 +169,10 @@ helpers.redirect = function (res, url, permanent) {
// this is used by sso plugins to redirect to the auth route
// { external: '/auth/sso' } or { external: 'https://domain/auth/sso' }
if (url.hasOwnProperty('external')) {
+ if (!utils.isSafeHref(url.external)) {
+ winston.warn(`[security] Unsafe external redirect attempted: ${url.external}`);
+ url.external = '/';
+ }
const redirectUrl = prependRelativePath(url.external);
if (res.locals.isAPI) {
res.set('X-Redirect', encodeURIComponent(redirectUrl)).status(200).json({ external: redirectUrl });
diff --git a/test/controllers.js b/test/controllers.js
index ead5404d5d..45080cd80e 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -1858,6 +1858,12 @@ describe('Controllers', () => {
assert.equal(result.body, '/recent');
});
+ it.only('should redirect to home if link is unsafe', async () => {
+ const result = await request.get(`${nconf.get('url')}/api/ap?resource=javascript:alert(origin)`, { jar });
+ assert.equal(result.response.headers['x-redirect'], encodeURIComponent('/'));
+ assert.equal(result.body.external, '/');
+ });
+
it('should get recent topic replies from children categories', async () => {
const parentCategory = await categories.create({ name: 'parent category', backgroundImage: 'path/to/some/image' });
const childCategory1 = await categories.create({ name: 'child category 1', parentCid: category.cid });