Node.js based forum software built for the modern web
refactor: change href arg validation
Barış Soner Uşaklı58a70e20da90
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.jsindex 35dcf8156e..7c76d8f5c3 100644--- a/public/src/modules/translator.common.js+++ b/public/src/modules/translator.common.js@@ -266,13 +266,25 @@ module.exports = function (utils, load, warn) {.replace(/&rsqb;/g, ']');out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped);});- // only allow http/https URLs and relative paths (starting with /) in href attributes- out = out.replace(/href=(['"])((?!https?:\/\/|\/)[^'"]*)\1/gi, 'href=$1$1');+ out = validateHrefAttributes(out);return out;});});};+ function validateHrefAttributes(translated) {+ return translated.replace(/href="([^"]*)"/gi, (match, href) => {+ return 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;+ }+/*** Load translation file (or use a cached version), and optionally return the translation of a certain key* @param {string} namespace - The file name of the translation namespacediff --git a/test/translator.js b/test/translator.jsindex 26fbe7e28e..65790d6177 100644--- a/test/translator.js+++ b/test/translator.js@@ -205,26 +205,62 @@ describe('new Translator(language)', () => {);});- it('should set href to empty string if argument is not a URL', async function () {- const translator = Translator.create('en-GB');+ it.only('should let valid urls through and empty href for invalid urls', async function () {+ shim.addTranslation('en-GB', 'topic', {+ 'href-test-1': 'This topic has been merged into <a href="%1">%2</a> and <a href="%1">%3</a>',+ 'href-test-2': 'This topic has been merged into <a href="%1/topic/%2">%3</a>',+ 'href-test-3': '<a href="%1">%2</a> and <a href="%3">%4</a>',+ 'href-test-4': '<a href="%1%2">%3</a>',+ });++ assert.strictEqual(+ await shim.translate('[[topic:merged-message, https://example.com, foo]]'),+ 'This topic has been merged into <a href="https://example.com">foo</a>'+ );assert.strictEqual(- await translator.translate('[[topic:merged-message, javascript is a nice language, foo]]'),+ await shim.translate('[[topic:merged-message, http://example.com, foo]]'),+ 'This topic has been merged into <a href="http://example.com">foo</a>'+ );++ assert.strictEqual(+ await shim.translate('[[topic:merged-message, /topic/123, foo]]'),+ 'This topic has been merged into <a href="/topic/123">foo</a>'+ );++ assert.strictEqual(+ await shim.translate('[[topic:href-test-1, /topic/123, foo, bar]]'),+ 'This topic has been merged into <a href="/topic/123">foo</a> and <a href="/topic/123">bar</a>'+ );++ assert.strictEqual(+ await shim.translate('[[topic:href-test-1, javascript:alert(origin), foo, bar]]'),+ 'This topic has been merged into <a href="">foo</a> and <a href="">bar</a>'+ );++ assert.strictEqual(+ await shim.translate('[[topic:href-test-2, javascript:alert(origin), foo, bar]]'),+ 'This topic has been merged into <a href="">bar</a>'+ );++ assert.strictEqual(+ await shim.translate('[[topic:merged-message, javascript is a nice language, foo]]'),'This topic has been merged into <a href="">foo</a>');- });- it('should let valid urls through', async function () {- const translator = Translator.create('en-GB');+ assert.strictEqual(+ await shim.translate('[[topic:href-test-3, javascript:alert(origin), foo, data:123, baz]]'),+ '<a href="">foo</a> and <a href="">baz</a>'+ );assert.strictEqual(- await translator.translate('[[topic:merged-message, https://example.com, foo]]'),- 'This topic has been merged into <a href="https://example.com">foo</a>'+ await shim.translate('[[topic:href-test-4, javascript:alert(origin), javascript:alert(origin), foo]]'),+ '<a href="">foo</a>');assert.strictEqual(- await translator.translate('[[topic:merged-message, /topic/123, foo]]'),- 'This topic has been merged into <a href="/topic/123">foo</a>'+ await shim.translate('[[topic:merged-message, "javascript:alert(origin), foo]]'),+ 'This topic has been merged into <a href="">foo</a>');});