Skip to content

Node.js based forum software built for the modern web

refactor: tx.escape

Barış Soner Uşaklıc7368f22588e
diff --git a/.husky/commit-msg b/.husky/commit-msg
index e8511eaeaf..ee7716e431 100755
--- a/.husky/commit-msg
+++ b/.husky/commit-msg
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
-npx --no-install commitlint --edit $1
+npx.cmd --no-install commitlint --edit $1
diff --git a/.husky/pre-commit b/.husky/pre-commit
index d37daa075e..16ba6284c1 100755
--- a/.husky/pre-commit
+++ b/.husky/pre-commit
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
-npx --no-install lint-staged
+npx.cmd --no-install lint-staged
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index eba906b5b2..cf5221c0c6 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -287,7 +287,9 @@ ajaxify.widgets = { render: render };
.replace('{browserTitle}', function () { return config.browserTitle; });
// Allow translation strings in title on ajaxify (#5927)
- title = translator.unescape(title);
+ if (!ajaxify.data.template.topic && !ajaxify.data.template.category) {
+ title = translator.unescape(title);
+ }
const data = { title: title };
hooks.fire('action:ajaxify.updateTitle', data);
translator.translate(data.title, function (translated) {
diff --git a/src/categories/data.js b/src/categories/data.js
index 943aaa6a7d..69d361f842 100644
--- a/src/categories/data.js
+++ b/src/categories/data.js
@@ -6,6 +6,7 @@ const db = require('../database');
const meta = require('../meta');
const plugins = require('../plugins');
const utils = require('../utils');
+const translator = require('../translator');
const intFields = [
'cid', 'parentCid', 'disabled', 'isSection', 'order',
@@ -154,11 +155,16 @@ function modifyCategory(category, fields) {
category.totalTopicCount = category.topic_count;
}
+ if (hasField('name')) {
+ category.name = translator.escape(category.name);
+ }
+
if (hasField('description')) {
- category.descriptionParsed = category.descriptionParsed || category.description;
+ category.description = translator.escape(category.description);
+ category.descriptionParsed = translator.escape(category.descriptionParsed || category.description);
}
if (category.nickname) {
- category.name = category.nickname;
+ category.name = translator.escape(category.nickname);
}
}
diff --git a/src/controllers/category.js b/src/controllers/category.js
index dd6caeea3b..bf8a9b24eb 100644
--- a/src/controllers/category.js
+++ b/src/controllers/category.js
@@ -14,7 +14,6 @@ const activitypub = require('../activitypub');
const pagination = require('../pagination');
const helpers = require('./helpers');
const utils = require('../utils');
-const translator = require('../translator');
const analytics = require('../analytics');
const categoryController = module.exports;
@@ -140,9 +139,8 @@ categoryController.get = async function (req, res, next) {
});
}
- categoryData.title = translator.escape(categoryData.name);
+ categoryData.title = categoryData.name;
categoryData.selectCategoryLabel = '[[category:subcategories]]';
- categoryData.description = translator.escape(categoryData.description);
categoryData.privileges = userPrivileges;
categoryData.showSelect = userPrivileges.editable;
categoryData.showTopicTools = userPrivileges.editable;
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 29167fcfb9..e5effc02b5 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -16,6 +16,7 @@ const pagination = require('../pagination');
const utils = require('../utils');
const analytics = require('../analytics');
const activitypub = require('../activitypub');
+const translator = require('../translator');
const topicsController = module.exports;
@@ -231,29 +232,22 @@ async function addOldCategory(topicData, userPrivileges) {
}
async function addTags(topicData, req, res, currentPage, postAtIndex) {
- let description = '';
- if (postAtIndex && postAtIndex.content) {
- description = utils.stripHTMLTags(utils.decodeHTMLEntities(postAtIndex.content)).trim();
- }
-
- if (description.length > 160) {
- description = `${description.slice(0, 157)}...`;
- }
- description = description.replace(/\n/g, ' ').trim();
-
let mainPost = topicData.posts.find(p => parseInt(p.index, 10) === 0);
if (!mainPost) {
mainPost = await posts.getPostData(topicData.mainPid);
}
+ const title = getTitleFromTopic(topicData);
res.locals.metaTags = [
{
name: 'title',
- content: topicData.titleRaw,
+ content: title,
+ noEscape: true,
},
{
property: 'og:title',
- content: topicData.titleRaw,
+ content: title,
+ noEscape: true,
},
{
property: 'og:type',
@@ -273,15 +267,18 @@ async function addTags(topicData, req, res, currentPage, postAtIndex) {
},
];
- if (description && description.length) {
+ const description = getDescriptionFromPost(postAtIndex);
+ if (description) {
res.locals.metaTags.push(
{
name: 'description',
content: description,
+ noEscape: true,
},
{
property: 'og:description',
content: description,
+ noEscape: true,
},
);
}
@@ -329,6 +326,27 @@ async function addTags(topicData, req, res, currentPage, postAtIndex) {
}
}
+function getTitleFromTopic(topic) {
+ // titleRaw is trasnslator.escape'd
+ const title = translator.unescape(String(topic.titleRaw));
+ return translator.escape(utils.escapeHTML(title));
+}
+
+function getDescriptionFromPost(post) {
+ let description = '';
+ if (post && post.content) {
+ // posts/parse.js calls translator.escape on post.content, unescape first then re-escape after stripping html tags
+ const content = translator.unescape(String(post.content));
+ description = utils.stripHTMLTags(utils.decodeHTMLEntities(content)).trim();
+ if (description.length > 160) {
+ description = `${description.slice(0, 157)}...`;
+ }
+ description = translator.escape(utils.escapeHTML(description));
+ description = description.replace(/\n/g, ' ').trim();
+ }
+ return description;
+}
+
async function addOGImageTags(res, topicData, postAtIndex) {
const uploads = postAtIndex ? await posts.uploads.listWithSizes(postAtIndex.pid) : [];
const images = uploads.filter(Boolean);
@@ -344,7 +362,7 @@ async function addOGImageTags(res, topicData, postAtIndex) {
images.push(topicData.category.backgroundImage);
}
if (postAtIndex && postAtIndex.user && postAtIndex.user.picture) {
- images.push(postAtIndex.user.picture);
+ images.push(validator.unescape(postAtIndex.user.picture));
}
images.forEach(path => addOGImageTag(res, path));
}
@@ -369,11 +387,9 @@ function addOGImageTag(res, image) {
res.locals.metaTags.push({
property: 'og:image',
content: imageUrl,
- noEscape: true,
}, {
property: 'og:image:url',
content: imageUrl,
- noEscape: true,
});
if (isObject && image.width && image.height) {
diff --git a/src/meta/tags.js b/src/meta/tags.js
index 97651b66fc..dd91621017 100644
--- a/src/meta/tags.js
+++ b/src/meta/tags.js
@@ -6,6 +6,7 @@ const winston = require('winston');
const plugins = require('../plugins');
const Meta = require('./index');
const utils = require('../utils');
+const translator = require('../translator');
const Tags = module.exports;
@@ -82,12 +83,12 @@ Tags.parse = async (req, data, meta, link) => {
addTouchIcons(defaultLinks);
}
- const results = await utils.promiseParallel({
- tags: plugins.hooks.fire('filter:meta.getMetaTags', { req: req, data: data, tags: defaultTags }),
- links: plugins.hooks.fire('filter:meta.getLinkTags', { req: req, data: data, links: defaultLinks }),
- });
+ const [{ tags }, { links }] = await Promise.all([
+ plugins.hooks.fire('filter:meta.getMetaTags', { req, data, tags: defaultTags }),
+ plugins.hooks.fire('filter:meta.getLinkTags', { req, data, links: defaultLinks }),
+ ]);
- meta = results.tags.tags.concat(meta || []).map((tag) => {
+ meta = tags.concat(meta || []).map((tag) => {
if (!tag || typeof tag.content !== 'string') {
winston.warn('Invalid meta tag. ', tag);
return tag;
@@ -96,7 +97,7 @@ Tags.parse = async (req, data, meta, link) => {
if (!tag.noEscape) {
const attributes = Object.keys(tag);
attributes.forEach((attr) => {
- tag[attr] = utils.escapeHTML(String(tag[attr]));
+ tag[attr] = translator.escape(utils.escapeHTML(String(tag[attr])));
});
}
@@ -111,7 +112,7 @@ Tags.parse = async (req, data, meta, link) => {
addIfNotExists(meta, 'name', 'description', Meta.config.description);
addIfNotExists(meta, 'property', 'og:description', Meta.config.description);
- link = results.links.links.concat(link || []);
+ link = links.concat(link || []);
if (isAPI) {
const whitelist = ['canonical', 'alternate', 'up'];
link = link.filter(link => whitelist.some(val => val === link.rel));
@@ -201,7 +202,7 @@ function addIfNotExists(meta, keyName, tagName, value) {
if (!exists && value) {
meta.push({
- content: utils.escapeHTML(String(value)),
+ content: translator.escape(utils.escapeHTML(String(value))),
[keyName]: tagName,
});
}
diff --git a/src/middleware/render.js b/src/middleware/render.js
index 72b3d5ac78..8ab928b14a 100644
--- a/src/middleware/render.js
+++ b/src/middleware/render.js
@@ -172,7 +172,10 @@ module.exports = function (middleware) {
templateValues.configJSON = jsesc(JSON.stringify(res.locals.config), { isScriptContext: true });
- const title = translator.unescape(utils.stripHTMLTags(options.title || ''));
+ const title = options.template.topic ?
+ utils.stripHTMLTags(options.title || '') : // don't translate topic titles
+ translator.unescape(utils.stripHTMLTags(options.title || ''));
+
const results = await utils.promiseParallel({
isAdmin: user.isAdministrator(req.uid),
isGlobalMod: user.isGlobalModerator(req.uid),