Skip to content

Node.js based forum software built for the modern web

fix: use isSafeHref on user.url and category.url

Barış Soner Uşaklı26333b88c214
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.js
index 80295f6dd7..c450072506 100644
--- a/public/src/modules/translator.common.js
+++ b/public/src/modules/translator.common.js
@@ -61,17 +61,10 @@ module.exports = function (utils, load, warn) {
function validateHrefAttributes(translated) {
return translated.replace(/href\s*=\s*(["'])(.*?)\1/gi, (match, quote, href) => {
- return isSafeHref(href) ? match : 'href=""';
+ return utils.isSafeHref(href) ? match : 'href=""';
});
}
- function isSafeHref(href) {
- const normalizedHref = String(href).trim().toLowerCase();
- const isHttpUrl = normalizedHref.startsWith('https://') || normalizedHref.startsWith('http://');
- const isRelativeUrl = normalizedHref.startsWith('/') && !normalizedHref.startsWith('//');
- return isHttpUrl || isRelativeUrl;
- }
-
// split a translator string into an array of tokens
// but don't split by commas inside other translator strings
// "namespace:key, arg1, [[namespace:key, arg2]]" becomes
diff --git a/public/src/utils.common.js b/public/src/utils.common.js
index 2bcae6f200..99b2cb41a0 100644
--- a/public/src/utils.common.js
+++ b/public/src/utils.common.js
@@ -453,6 +453,13 @@ const utils = {
});
},
+ isSafeHref: function (href) {
+ const normalizedHref = String(href).trim().toLowerCase();
+ const isHttpUrl = normalizedHref.startsWith('https://') || normalizedHref.startsWith('http://');
+ const isRelativeUrl = normalizedHref.startsWith('/') && !normalizedHref.startsWith('//');
+ return isHttpUrl || isRelativeUrl;
+ },
+
// https://github.com/sindresorhus/is-absolute-url
isAbsoluteUrlRE: /^[a-zA-Z][a-zA-Z\d+\-.]*:/,
isWinPathRE: /^[a-zA-Z]:\\/,
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js
index 461f5a5a81..8572033346 100644
--- a/src/activitypub/mocks.js
+++ b/src/activitypub/mocks.js
@@ -239,7 +239,7 @@ Mocks.profile = async (actors) => {
followerCount,
followingCount,
- url,
+ url: activitypub.helpers.isUri(url) ? url : null,
inbox,
sharedInbox: endpoints ? endpoints.sharedInbox : null,
followersUrl: followers,
@@ -306,7 +306,7 @@ Mocks.category = async (actors) => {
// followerCount,
// followingCount,
- url,
+ url: activitypub.helpers.isUri(url) ? url : null,
inbox,
sharedInbox: endpoints ? endpoints.sharedInbox : null,
followersUrl: followers,
diff --git a/src/categories/data.js b/src/categories/data.js
index e45252f47d..fa76c07a4e 100644
--- a/src/categories/data.js
+++ b/src/categories/data.js
@@ -151,6 +151,10 @@ async function modifyCategories(categories, fields) {
category.name = category.nickname || category.name;
}
+ if (hasField('url')) {
+ category.url = utils.isSafeHref(category.url) ? category.url : '';
+ }
+
if (hasField('description') && category.cid !== -1) {
category.descriptionParsed = await plugins.hooks.fire('filter:parse.raw', category.description || '');
}
diff --git a/src/user/data.js b/src/user/data.js
index 49712fca5c..162a08a0ac 100644
--- a/src/user/data.js
+++ b/src/user/data.js
@@ -277,7 +277,8 @@ module.exports = function (User) {
// works around renderOverride supplying `url` to templates
if (user.url) {
- user.remoteUrl = String(user.url);
+ user.url = utils.isSafeHref(user.url) ? user.url : '';
+ user.remoteUrl = user.url;
} else {
delete user.url;
}
diff --git a/test/utils.js b/test/utils.js
index 36e17705ca..614ca9f9a2 100644
--- a/test/utils.js
+++ b/test/utils.js
@@ -494,6 +494,34 @@ describe('Utility Methods', () => {
});
});
+ describe('isSafeHref', () => {
+ it('should return true for http/https url', (done) => {
+ assert(utils.isSafeHref('http://nodebb.org'));
+ assert(utils.isSafeHref('https://nodebb.org'));
+ done();
+ });
+
+ it('should return true for /topic/123', (done) => {
+ assert(utils.isSafeHref('/topic/123'));
+ assert(utils.isSafeHref(' /topic/123'));
+ done();
+ });
+
+ it('should return false for //foo', (done) => {
+ assert(!utils.isSafeHref('//foo'));
+ assert(!utils.isSafeHref(' //foo'));
+ done();
+ });
+
+ it('should return false for javascript/data', (done) => {
+ assert(!utils.isSafeHref('javascript:alert(1)'));
+ assert(!utils.isSafeHref(' javascript:alert(1)'));
+ assert(!utils.isSafeHref('data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg=='));
+ assert(!utils.isSafeHref(' data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg=='));
+ done();
+ });
+ });
+
it('escape html', (done) => {
const escaped = utils.escapeHTML('&<>');
assert.equal(escaped, '&amp;&lt;&gt;');