Node.js based forum software built for the modern web
fix: translator.escape chat messages & teasers
Barış Soner Uşaklı7ef400b311f2
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.jsindex 81299cbce8..9b78d7559e 100644--- a/public/src/modules/translator.common.js+++ b/public/src/modules/translator.common.js@@ -274,7 +274,7 @@ module.exports = function (utils, load, warn) {};function validateHrefAttributes(translated) {- return translated.replace(/href="([^"]*)"/gi, (match, href) => {+ return translated.replace(/href\s*=\s*(["'])(.*?)\1/gi, (match, quote, href) => {return isSafeHref(href) ? match : 'href=""';});}diff --git a/src/messaging/index.js b/src/messaging/index.jsindex 10b90bbef9..25020f483c 100644--- a/src/messaging/index.js+++ b/src/messaging/index.js@@ -90,7 +90,8 @@ async function canGet(hook, callerUid, uid) {}Messaging.parse = async (message, fromuid, uid, roomId, isNew) => {- const parsed = await plugins.hooks.fire('filter:parse.raw', String(message || ''));+ let parsed = await plugins.hooks.fire('filter:parse.raw', String(message || ''));+ parsed = translator.escape(parsed);let messageData = {message: message,parsed: parsed,@@ -348,9 +349,9 @@ Messaging.getTeasers = async (uid, roomIds) => {if (userMap[teaser.fromuid]) {teaser.user = userMap[teaser.fromuid];}- teaser.content = validator.escape(+ teaser.content = translator.escape(validator.escape(String(utils.stripHTMLTags(utils.decodeHTMLEntities(teaser.content)))- );+ ));teaser.roomId = parseInt(roomId, 10);const payload = await plugins.hooks.fire('filter:messaging.getTeaser', { teaser: teaser });return payload.teaser;diff --git a/src/middleware/render.js b/src/middleware/render.jsindex fee9461c69..e1e3cb33e6 100644--- a/src/middleware/render.js+++ b/src/middleware/render.js@@ -170,7 +170,7 @@ module.exports = function (middleware) {widgets: options.widgets,};- templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });+ templateValues.configJSON = jsesc(translator.escape(JSON.stringify(res.locals.config)), { isScriptContext: true });const title = utils.stripHTMLTags(String(options.title || ''));@@ -226,7 +226,7 @@ module.exports = function (middleware) {templateValues.showModMenu = results.user.isAdmin || results.user.isGlobalMod || results.user.isMod;templateValues.canChat = (results.privileges.chat || results.privileges['chat:privileged']) && meta.config.disableChat !== 1;templateValues.user = results.user;- templateValues.userJSON = jsesc(JSON.stringify(results.user), { isScriptContext: true });+ templateValues.userJSON = jsesc(translator.escape(JSON.stringify(results.user)), { isScriptContext: true });templateValues.useCustomCSS = meta.config.useCustomCSS && meta.config.customCSS;templateValues.customCSS = templateValues.useCustomCSS ? (meta.config.renderedCustomCSS || '') : '';templateValues.useCustomHTML = meta.config.useCustomHTML;@@ -288,13 +288,13 @@ module.exports = function (middleware) {res.locals.config.isRTL = results.languageDirection === 'rtl';const templateValues = {config: res.locals.config,- configJSON: jsesc(JSON.stringify(res.locals.config), { isScriptContext: true }),+ configJSON: jsesc(translator.escape(JSON.stringify(res.locals.config)), { isScriptContext: true }),relative_path: res.locals.config.relative_path,adminConfigJSON: encodeURIComponent(JSON.stringify(results.configs)),metaTags: results.tags.meta,linkTags: results.tags.link,user: userData,- userJSON: jsesc(JSON.stringify(userData), { isScriptContext: true }),+ userJSON: jsesc(translator.escape(JSON.stringify(userData)), { isScriptContext: true }),plugins: results.custom_header.plugins,authentication: results.custom_header.authentication,scripts: results.scripts,diff --git a/test/messaging.js b/test/messaging.jsindex a52d594448..356eb84d9e 100644--- a/test/messaging.js+++ b/test/messaging.js@@ -589,6 +589,17 @@ describe('Messaging Library', () => {assert.equal(rooms[0].teaser.content, '<svg/onload=alert(document.location);');});+ it('should not translate chat messages if they have translation keys', async () => {+ await callv3API('post', `/chats/${roomId}`, { roomId: roomId, message: '[[global:404.login]]' }, 'foo');+ const { rooms } = await api.chats.list(+ { uid: mocks.users.foo.uid }, { start: 0, stop: 9, uid: mocks.users.foo.uid }+ );+ const room = await api.chats.get({ uid: mocks.users.foo.uid }, { uid: mocks.users.foo.uid, roomId });+ const txEscaped = translator.escape('[[global:404.login]]');+ assert.strictEqual(room.messages[room.messages.length - 1].content, txEscaped);+ assert.strictEqual(rooms[0].teaser.content, txEscaped);+ });+it('should escape chatWithMessage', async () => {const oldValue = meta.config.showFullnameAsDisplayName;meta.config.showFullnameAsDisplayName = true;diff --git a/test/translator.js b/test/translator.jsindex 201671e29f..b2503b260d 100644--- a/test/translator.js+++ b/test/translator.js@@ -194,6 +194,11 @@ describe('new Translator(language)', () => {await translator.translate('[[topic:merged-message, %20%20%20javascript:alert(origin), foo]]'),'This topic has been merged into <a href="">foo</a>');++ assert.strictEqual(+ await translator.translate('[[global:403.login, javascript:alert(origin)]]'),+ 'Perhaps you should <a class="alert-link" href="">try logging in</a>?'+ );});it('should not strip javascript from arguments if it\'s not a href attribute', async function () {