Node.js based forum software built for the modern web
fix: tx.compile so arguments can't break out of the translation token
Barış Soner Uşaklı1205d0b147fc
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.jsindex 7e4d4869a9..80295f6dd7 100644--- a/public/src/modules/translator.common.js+++ b/public/src/modules/translator.common.js@@ -17,6 +17,15 @@ module.exports = function (utils, load, warn) {.replace(/&rsqb;/g, ']');}+ function isTranslationKey(str) {+ const tokens = parseTranslationString(str);+ return tokens.length === 1 && tokens[0].tx;+ }++ function hasTranslationKey(str) {+ return parseTranslationString(str).some(token => token.tx);+ }+// takes token '[[topic:moved-from, arg1, arg2]]' and// normalizes it to ['topic:moved-from', ['arg1', 'arg2']]function normalizeToken(token, ignoreArgs = false) {@@ -458,7 +467,8 @@ module.exports = function (utils, load, warn) {};Translator.escapeArg = function (arg) {- return String(arg).replace(/%/g, '%').replace(/,/g, ',');+ arg = String(arg).replace(/%/g, '%').replace(/,/g, ',');+ return isTranslationKey(arg) ? arg : Translator.escape(arg);};return Translator;@@ -486,6 +496,8 @@ module.exports = function (utils, load, warn) {languageDirection: function (lang) {return this.rtlLanguages.includes(lang) ? 'rtl' : 'ltr';},+ isTranslationKey: isTranslationKey,+ hasTranslationKey: hasTranslationKey,flush: function () {Object.keys(Translator.cache).forEach(function (code) {diff --git a/src/user/index.js b/src/user/index.jsindex b416ea586c..73ce5c0775 100644--- a/src/user/index.js+++ b/src/user/index.js@@ -184,7 +184,7 @@ User.getNotificationDisplayname = async function (uid) {await categories.getCategoryFields(uid, ['name']) :await User.getUserFields(uid, ['uid', 'username']);if (fromCategory) {- return userData.name;+ return translator.escape(userData.name);}if (userData.uid === 0) {diff --git a/src/user/notifications.js b/src/user/notifications.jsindex c9fe02d8f8..858becb74d 100644--- a/src/user/notifications.js+++ b/src/user/notifications.js@@ -11,6 +11,7 @@ const privileges = require('../privileges');const plugins = require('../plugins');const tx = require('../translator');const topics = require('../topics');+const posts = require('../posts');const user = require('./index');const UserNotifications = module.exports;@@ -135,7 +136,10 @@ UserNotifications.getNotifications = async function (nids, uid) {notificationData = await notifications.merge(notificationData);await Promise.all(notificationData.map(async (n) => {if (n?.bodyShort) {- n.bodyShort = await tx.translate(n.bodyShort, userSettings.userLang);+ n.bodyShort = posts.sanitize(await tx.translate(n.bodyShort, userSettings.userLang));+ }+ if (n?.bodyLong) {+ n.bodyLong = posts.sanitize(n.bodyLong);}}));diff --git a/test/notifications.js b/test/notifications.jsindex de2bf7a83d..bd187430a9 100644--- a/test/notifications.js+++ b/test/notifications.js@@ -14,6 +14,7 @@ const notifications = require('../src/notifications');const socketNotifications = require('../src/socket.io/notifications');const api = require('../src/api');const utils = require('../src/utils');+const tx = require('../src/translator');const sleep = util.promisify(setTimeout);@@ -309,11 +310,61 @@ describe('Notifications', () => {uid: uid,cid: cid,title: 'Test Topic Title',- content: 'The content of test topic',+ content: 'The content of test topic <script>alert(document.domain)</script>',});await sleep(1100);const data = await user.notifications.getAll(followerUid, '');- assert(data);+ assert(Array.isArray(data));+ const notifs = await user.notifications.get(followerUid);+ assert.strictEqual(notifs.unread[0].bodyLong, 'The content of test topic ');+ });++ it('should sanitize html in notification args', async () => {+ const bodyShort = tx.compile(+ 'notifications:user-posted-topic-in-category',+ 'displayName]]<img src=x onerror=alert(document.domain)>',+ 'topicTitle]]<img src=x onerror=alert(document.domain)>',+ 'Lounge]]<img src=x onerror=alert(document.domain)>' // categoy name+ );++ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });+ const notification = await notifications.create({+ type: 'new-topic-in-category',+ nid: 'new_topic:tid:1:uid:1',+ bodyShort: bodyShort,+ pid: 1,+ path: '/post/1',+ tid: 1,+ from: 1,+ });++ notifications.push(notification, [uid]);+ await sleep(2000);++ const notifData = await user.notifications.get(uid);+ assert.strictEqual(notifData.unread[0].bodyShort, '<strong>displayName]]<img src=x onerror=alert(document.domain)></strong> posted <strong>topicTitle]]<img src=x onerror=alert(document.domain)></strong> in <strong>Lounge]]<img src=x onerror=alert(document.domain)></strong>');+ });++ it('should sanitize html in notification args', async () => {+ const uid = await user.create({ username: utils.generateUUID().slice(0, 8) });++ const notification = await notifications.create({+ type: 'new-topic-in-category',+ nid: 'new_topic:tid:1:uid:1',+ bodyShort: '<img src=x onerror=alert(document.domain)>',+ bodyLong: 'bodylong <script>alert(document.domain)</script>',+ pid: 1,+ path: '/post/1',+ tid: 1,+ from: 1,+ });++ notifications.push(notification, [uid]);+ await sleep(2000);++ const notifData = await user.notifications.get(uid);+ assert.strictEqual(notifData.unread[0].bodyShort, '<img src="x" />');+ assert.strictEqual(notifData.unread[0].bodyLong, 'bodylong ');});it('should send welcome notification', async () => {diff --git a/test/translator.js b/test/translator.jsindex 4e31ff8aeb..d7da97c688 100644--- a/test/translator.js+++ b/test/translator.js@@ -122,6 +122,42 @@ describe('Translator shim', () => {});});+ describe('isTranslationKey/hasTranslationKey', () => {+ it('should detect valid translation keys', (done) => {+ assert.strictEqual(shim.isTranslationKey('[[global:home]]'), true);+ assert.strictEqual(shim.isTranslationKey('[[global:home, arg1, arg2]]'), true);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]]]]'), true);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]], arg2]]'), true);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]], [[nested:key2]]]]'), true);+ done();+ });++ it('should detect invalid translation keys', (done) => {+ assert.strictEqual(shim.isTranslationKey('[[global:home'), false);+ assert.strictEqual(shim.isTranslationKey('global:home]]'), false);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]]'), false);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]], arg2'), false);+ assert.strictEqual(shim.isTranslationKey('[[global:home, [[nested:key]], [[nested:key2]]'), false);+ done();+ });++ it('should return true if text has translation keys', (done) => {+ assert.strictEqual(shim.hasTranslationKey('[[global:home]]'), true);+ assert.strictEqual(shim.hasTranslationKey('[[global:home, arg1, arg2]]'), true);+ assert.strictEqual(shim.hasTranslationKey('testing [[global:home, arg1, arg2]] text'), true);+ assert.strictEqual(shim.hasTranslationKey(']] some text here [[namespace1:key1, arg1, arg2]] other text [[invalid]] test [[namespace2:key2]] [[invalid'), true);+ done();+ });++ it('should return false if text does not have translation keys', (done) => {+ assert.strictEqual(shim.hasTranslationKey('global:home'), false);+ assert.strictEqual(shim.hasTranslationKey('[[global:home, arg1, arg2'), false);+ assert.strictEqual(shim.hasTranslationKey('testing global:home, arg1, arg2]] text'), false);+ assert.strictEqual(shim.hasTranslationKey(']] some text here namespace1:key1, arg1, arg2]] other text invalid]] test [[invalid'), false);+ done();+ });+ });+describe('.normalizeToken', () => {it('should normalize a token into its key and arguments', (done) => {assert.deepStrictEqual(@@ -735,6 +771,30 @@ describe('Translator static methods', () => {);done();});++ it('should escape `]]` and `[[` in arguments if they are invalid translation tokens', (done) => {+ assert.strictEqual(+ Translator.compile('amazing:cool', '[[nested:valid]]', 'plainstr', 'invalid]]<img src=x onerror=alert(document.domain)>'),+ '[[amazing:cool, [[nested:valid]], plainstr, invalid]]<img src=x onerror=alert(document.domain)>]]'+ );++ assert.strictEqual(+ Translator.compile('amazing:cool', '[[nested:valid]]', 'plainstr', 'invalid[[<img src=x onerror=alert(document.domain)>'),+ '[[amazing:cool, [[nested:valid]], plainstr, invalid[[<img src=x onerror=alert(document.domain)>]]'+ );++ assert.strictEqual(+ Translator.compile('amazing:cool', '[[nested:valid]]', 'plainstr', 'invalid[[<img src=x onerror=alert(document.domain)>]]'),+ '[[amazing:cool, [[nested:valid]], plainstr, invalid[[<img src=x onerror=alert(document.domain)>]]]]'+ );++ assert.strictEqual(+ Translator.compile('amazing:cool', '[[nested:invalid]]]]', ']][[invalid:foo, <img src=x onerror=alert(document.domain)>'),+ '[[amazing:cool, [[nested:invalid]]]], ]][[invalid:foo, <img src=x onerror=alert(document.domain)>]]'+ );++ done();+ });});describe('add translation', () => {