Skip to content

Node.js based forum software built for the modern web

refactor: translate category names in selector

Barış Soner Uşaklı2a400bc8cc20
diff --git a/public/src/modules/translator.common.js b/public/src/modules/translator.common.js
index de756fdd09..35dcf8156e 100644
--- a/public/src/modules/translator.common.js
+++ b/public/src/modules/translator.common.js
@@ -266,6 +266,8 @@ module.exports = function (utils, load, warn) {
.replace(/]/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');
return out;
});
});
@@ -465,8 +467,18 @@ module.exports = function (utils, load, warn) {
Translator.escape = function escape(text) {
return typeof text === 'string' ?
text.replace(/\[\[/g, '[[').replace(/\]\]/g, ']]') :
- // text.replace(/\[\[([a-zA-Z0-9_.-]+:[a-zA-Z0-9_.-]+)\]\]/g, '[[$1]]') :
text;
+ /*
+ if (typeof text !== 'string') return text;
+
+ let previous;
+ // Keep matching the innermost unescaped brackets and converting them
+ while (text !== previous) {
+ previous = text;
+ text = text.replace(/\[\[([a-zA-Z0-9_.-]+:[^\[\]]+)\]\]/g, '[[$1]]');
+ }
+ return text;
+ */
};
/**
@@ -477,8 +489,18 @@ module.exports = function (utils, load, warn) {
Translator.unescape = function unescape(text) {
return typeof text === 'string' ?
text.replace(/]]/g, ']]').replace(/[[/g, '[[') :
- // text.replace(/[[([a-zA-Z0-9_.-]+:[a-zA-Z0-9_.-]+)]]/g, '[[$1]]') :
text;
+ /*
+ if (typeof text !== 'string') return text;
+
+ let previous;
+ // Keep matching the innermost escaped brackets and converting them back
+ while (text !== previous) {
+ previous = text;
+ text = text.replace(/[[([a-zA-Z0-9_.-]+:(?:(?![|]).)+)]]/g, '[[$1]]');
+ }
+ return text;
+ */
};
/**
diff --git a/src/controllers/admin/categories.js b/src/controllers/admin/categories.js
index 6ee4afe034..ec7e559a1b 100644
--- a/src/controllers/admin/categories.js
+++ b/src/controllers/admin/categories.js
@@ -21,7 +21,7 @@ categoriesController.get = async function (req, res, next) {
const [categoryData, parent, selectedData] = await Promise.all([
categories.getCategories([req.params.category_id]),
categories.getParents([req.params.category_id]),
- helpers.getSelectedCategory(req.params.category_id),
+ helpers.getSelectedCategory(req.params.category_id, req.uid),
]);
const category = categoryData[0];
@@ -145,26 +145,25 @@ async function buildBreadcrumbs(categoryData, url) {
categoriesController.buildBreadCrumbs = buildBreadcrumbs;
categoriesController.getAnalytics = async function (req, res) {
- const [name, analyticsData, selectedData] = await Promise.all([
- categories.getCategoryField(req.params.category_id, 'name'),
+ const [analyticsData, { selectedCategory }] = await Promise.all([
analytics.getCategoryAnalytics(req.params.category_id),
- helpers.getSelectedCategory(req.params.category_id),
+ helpers.getSelectedCategory(req.params.category_id, req.uid),
]);
+
res.render('admin/manage/category-analytics', {
- name: name,
+ name: selectedCategory?.name || '',
analytics: analyticsData,
- selectedCategory: selectedData.selectedCategory,
+ selectedCategory: selectedCategory,
});
};
categoriesController.getFederation = async function (req, res) {
const cid = parseInt(req.params.category_id, 10);
- let [_following, pending, followers, name, { selectedCategory }] = await Promise.all([
+ let [_following, pending, followers, { selectedCategory }] = await Promise.all([
db.getSortedSetMembers(`cid:${cid}:following`),
db.getSortedSetMembers(`followRequests:cid.${cid}`),
activitypub.notes.getCategoryFollowers(cid),
- categories.getCategoryField(cid, 'name'),
- helpers.getSelectedCategory(cid),
+ helpers.getSelectedCategory(cid, req.uid),
]);
const following = [..._following, ...pending].map(entry => ({
@@ -178,7 +177,7 @@ categoriesController.getFederation = async function (req, res) {
res.render('admin/manage/category-federation', {
cid: cid,
enabled: meta.config.activitypubEnabled,
- name,
+ name: selectedCategory?.name || '',
selectedCategory,
following,
followers,
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 04d6d8dede..4818e34999 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -198,7 +198,7 @@ helpers.buildCategoryBreadcrumbs = async function (cid, userLang) {
/* eslint-disable no-await-in-loop */
const data = await categories.getCategoryFields(cid, ['name', 'slug', 'parentCid', 'disabled', 'isSection']);
- const translatedName = await translateRawValue(data.name, userLang);
+ const translatedName = await helpers.translateEscapedValue(data.name, userLang);
if (!data.disabled && !data.isSection) {
breadcrumbs.unshift({
text: translatedName,
@@ -255,21 +255,21 @@ helpers.buildTitle = function (pageTitle) {
return title;
};
-// categoy names and descriptions can be tx keys, translate them safely here
-// used on category list, category page and users watched categories
-async function translateRawValue(value, lang) {
+helpers.translateEscapedValue = async function translateEscapedValue(value, lang) {
const rawValue = validator.unescape(translator.unescape(String(value || '')));
return validator.escape(await translator.translate(rawValue, lang));
-}
+};
+// categoy names and descriptions can be tx keys, translate them safely here
+// used on category list, category page and users watched categories
helpers.translateCategoryData = async function (categoryData, userLang) {
await Promise.all(categoryData.map(async (category) => {
if (category) {
- category.name = await translateRawValue(category.name, userLang);
+ category.name = await helpers.translateEscapedValue(category.name, userLang);
category.descriptionParsed = await plugins.hooks.fire(
- 'filter:parse.raw', await translateRawValue(category.description, userLang)
+ 'filter:parse.raw', await helpers.translateEscapedValue(category.description, userLang)
);
- category.description = await translateRawValue(category.description, userLang);
+ category.description = await helpers.translateEscapedValue(category.description, userLang);
}
}));
};
@@ -289,7 +289,7 @@ async function getCategoryData(cids, uid, selectedCid, states, privilege) {
helpers.getVisibleCategories({
cids, uid, states, privilege, showLinks: false,
}),
- helpers.getSelectedCategory(selectedCid),
+ helpers.getSelectedCategory(selectedCid, uid),
]);
const categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass']);
@@ -360,28 +360,30 @@ helpers.getVisibleCategories = async function (params) {
});
};
-helpers.getSelectedCategory = async function (cids) {
+helpers.getSelectedCategory = async function (cids, uid) {
if (cids && !Array.isArray(cids)) {
cids = [cids];
}
cids = cids && cids.map(cid => parseInt(cid, 10));
- let selectedCategories = await categories.getCategoriesData(cids);
+ const [selectedCategories, { userLang }] = await Promise.all([
+ categories.getCategoriesData(cids),
+ user.getSettings(uid),
+ ]);
+ let selectedCategory = null;
const selectedCids = selectedCategories.map(c => c && c.cid).filter(Boolean);
if (selectedCategories.length > 1) {
- selectedCategories = {
+ selectedCategory = {
icon: 'fa-plus',
name: '[[unread:multiple-categories-selected]]',
bgColor: '#ddd',
};
} else if (selectedCategories.length === 1 && selectedCategories[0]) {
- selectedCategories = selectedCategories[0];
- } else {
- selectedCategories = null;
+ selectedCategory = selectedCategories[0];
}
- return {
- selectedCids: selectedCids,
- selectedCategory: selectedCategories,
- };
+ if (selectedCategory) {
+ selectedCategory.name = await helpers.translateEscapedValue(selectedCategory.name, userLang);
+ }
+ return { selectedCids, selectedCategory };
};
helpers.getSelectedTag = function (tags) {
diff --git a/src/controllers/mods.js b/src/controllers/mods.js
index eebdf72b35..25ec70688f 100644
--- a/src/controllers/mods.js
+++ b/src/controllers/mods.js
@@ -90,7 +90,7 @@ modsController.flags.list = async function (req, res) {
query: req.query,
}),
analytics.getDailyStatsForSet('analytics:flags', Date.now(), 30),
- helpers.getSelectedCategory(validFilters.cid),
+ helpers.getSelectedCategory(validFilters.cid, req.uid),
]);
// Send back information for userFilter module
@@ -221,7 +221,7 @@ modsController.postQueue = async function (req, res, next) {
user.isAdministrator(req.uid),
user.isGlobalModerator(req.uid),
user.getModeratedCids(req.uid),
- helpers.getSelectedCategory(cid),
+ helpers.getSelectedCategory(cid, req.uid),
Promise.all(['global', 'admin'].map(async type => privileges[type].get(req.uid))),
]);
_privileges = { ..._privileges[0], ..._privileges[1] };
diff --git a/src/controllers/recent.js b/src/controllers/recent.js
index 73d5348c0d..2289ebc4dc 100644
--- a/src/controllers/recent.js
+++ b/src/controllers/recent.js
@@ -35,7 +35,7 @@ recentController.getData = async function (req, url, sort, selectedTerm = 'allti
const [settings, categoryData, tagData, rssToken, canPost, isPrivileged] = await Promise.all([
user.getSettings(req.uid),
- helpers.getSelectedCategory(cid),
+ helpers.getSelectedCategory(cid, req.uid),
helpers.getSelectedTag(tag),
user.auth.getFeedToken(req.uid),
privileges.categories.canPostTopic(req.uid),
diff --git a/src/controllers/tags.js b/src/controllers/tags.js
index f601f94596..03014dfa3d 100644
--- a/src/controllers/tags.js
+++ b/src/controllers/tags.js
@@ -34,7 +34,7 @@ tagsController.getTag = async function (req, res) {
const [settings, cids, categoryData, canPost, isPrivileged, rssToken, isFollowing] = await Promise.all([
user.getSettings(req.uid),
cid || categories.getCidsByPrivilege('categories:cid', req.uid, 'topics:read'),
- helpers.getSelectedCategory(cid),
+ helpers.getSelectedCategory(cid, req.uid),
privileges.categories.canPostTopic(req.uid),
user.isPrivileged(req.uid),
user.auth.getFeedToken(req.uid),
diff --git a/src/controllers/unread.js b/src/controllers/unread.js
index 871f3252c0..b993d8c569 100644
--- a/src/controllers/unread.js
+++ b/src/controllers/unread.js
@@ -19,7 +19,7 @@ unreadController.get = async function (req, res) {
const filter = req.query.filter || '';
const [categoryData, tagData, userSettings, canPost, isPrivileged] = await Promise.all([
- helpers.getSelectedCategory(cid),
+ helpers.getSelectedCategory(cid, req.uid),
helpers.getSelectedTag(tag),
user.getSettings(req.uid),
privileges.categories.canPostTopic(req.uid),
diff --git a/test/categories.js b/test/categories.js
index c3844db6bd..9cde26201a 100644
--- a/test/categories.js
+++ b/test/categories.js
@@ -902,18 +902,18 @@ describe('Categories', () => {
assert.strictEqual(data.description, '[[topic:forked-message, javascript:alert(origin), foobar]]');
assert.strictEqual(data.descriptionParsed, '[[topic:forked-message, javascript:alert(origin), foobar]]');
const { response, body } = await request.get(`${nconf.get('url')}/api/category/${category.cid}/test-category`);
- console.log(body);
+
// title comes from category.name so should be escaped as well
- assert.strictEqual(body.title, 'This topic has been merged into <a href="javascript:alert(origin)">foobar</a>');
+ assert.strictEqual(body.title, 'This topic has been merged into <a href="">foobar</a>');
// breadcrumbs should be translated & escaped too
- assert.strictEqual(body.breadcrumbs[1].text, 'This topic has been merged into <a href="javascript:alert(origin)">foobar</a>');
+ assert.strictEqual(body.breadcrumbs[1].text, 'This topic has been merged into <a href="">foobar</a>');
- assert.strictEqual(body.name, 'This topic has been merged into <a href="javascript:alert(origin)">foobar</a>');
+ assert.strictEqual(body.name, 'This topic has been merged into <a href="">foobar</a>');
- assert.strictEqual(body.description, 'This topic was forked from <a href="javascript:alert(origin)">foobar</a>');
+ assert.strictEqual(body.description, 'This topic was forked from <a href="">foobar</a>');
- assert.strictEqual(body.descriptionParsed, 'This topic was forked from <a href="javascript:alert(origin)">foobar</a>');
+ assert.strictEqual(body.descriptionParsed, 'This topic was forked from <a href="">foobar</a>');
});
});
diff --git a/test/translator.js b/test/translator.js
index 6df22ba3fa..26fbe7e28e 100644
--- a/test/translator.js
+++ b/test/translator.js
@@ -157,9 +157,9 @@ describe('new Translator(language)', () => {
it('should escape language key parameters with HTML in them', () => {
const translator = Translator.create('en-GB');
- const key = '[[global:403.login, <strong>test</strong>]]';
+ const key = '[[topic:share-mail-body, <strong>test</strong>]]';
return translator.translate(key).then((translated) => {
- assert.strictEqual(translated, 'Perhaps you should <a class="alert-link" href=\'&lt;strong&gt;test&lt;/strong&gt;/login\'>try logging in</a>?');
+ assert.strictEqual(translated, 'I thought you might be interested in this post: &lt;strong&gt;test&lt;/strong&gt;');
});
});
@@ -182,6 +182,52 @@ describe('new Translator(language)', () => {
});
});
+ it('should strip href argument if it contains a javascript: URL', async function () {
+ const translator = Translator.create('en-GB');
+
+ assert.strictEqual(
+ await translator.translate('[[topic:merged-message, javascript:alert(origin), foo]]'),
+ 'This topic has been merged into <a href="">foo</a>'
+ );
+
+ assert.strictEqual(
+ await translator.translate('[[topic:merged-message, %20%20%20javascript:alert(origin), foo]]'),
+ 'This topic has been merged into <a href="">foo</a>'
+ );
+ });
+
+ it('should not strip javascript from arguments if it\'s not a href attribute', async function () {
+ const translator = Translator.create('en-GB');
+
+ assert.strictEqual(
+ await translator.translate('[[topic:share-mail-body, javascript:alert(origin)]]'),
+ 'I thought you might be interested in this post: javascript:alert(origin)'
+ );
+ });
+
+ it('should set href to empty string if argument is not a URL', async function () {
+ const translator = Translator.create('en-GB');
+
+ assert.strictEqual(
+ await translator.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 translator.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, /topic/123, foo]]'),
+ 'This topic has been merged into <a href="/topic/123">foo</a>'
+ );
+ });
+
it('should properly escape and ignore % and \\, in arguments', () => {
const translator = Translator.create('en-GB');
@@ -223,6 +269,13 @@ describe('new Translator(language)', () => {
});
});
+ it('should not translate [[topic:merged-message some text', () => {
+ const translator = Translator.create('en-GB');
+ return translator.translate('[[topic:merged-message some text').then((translated) => {
+ assert.strictEqual('[[topic:merged-message some text', translated);
+ });
+ });
+
it('should translate keys with slashes properly', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[pages:users/latest]]').then((translated) => {
@@ -351,8 +404,39 @@ describe('Translator static methods', () => {
done();
});
+ it('should escape translation pattern that have arguments', () => {
+ assert.strictEqual(
+ Translator.escape('[[topic:merged-message, https://example.com, foo]]'),
+ '&lsqb;&lsqb;topic:merged-message, https://example.com, foo&rsqb;&rsqb;'
+ );
+
+ assert.strictEqual(
+ Translator.escape('[[topic:merged-message, [[https://example.com]], foo]]'),
+ '&lsqb;&lsqb;topic:merged-message, &lsqb;&lsqb;https://example.com&rsqb;&rsqb;, foo&rsqb;&rsqb;'
+ );
+ });
+
+ it('should unescape translation pattern that have arguments', () => {
+ assert.strictEqual(
+ Translator.unescape('&lsqb;&lsqb;topic:merged-message, https://example.com, foo&rsqb;&rsqb;'),
+ '[[topic:merged-message, https://example.com, foo]]'
+ );
+
+ assert.strictEqual(
+ Translator.unescape('&lsqb;&lsqb;topic:merged-message, &lsqb;&lsqb;https://example.com&rsqb;&rsqb;, foo&rsqb;&rsqb;'),
+ '[[topic:merged-message, [[https://example.com]], foo]]'
+ );
+ });
+
// TODO: fixing this causes other issues with escaping tx strings
/*
+ translator escape works by escaping [[ and ]] separately, if its changed to be more strict to
+ fix markdown links like [link text [test]](https://example.org)
+ then it doesnt escape unclosed tx string like `[[topic:merged-message, javascript:alert('ok'), foo more text`
+ this causes the html to break because translator goes on until it finds a closing `]]`
+ and translates the entire thing using everything between as an argument.
+ to test it out set your fullname to `[[topic:merged-message, javascript:alert('ok'), foo more text`
+
it('should not escape markdown links', (done) => {
assert.strictEqual(
Translator.escape('[link text [test]](https://example.org)'),
@@ -368,8 +452,7 @@ describe('Translator static methods', () => {
'&lsqblink text &lsqbtest&rsqb;&rsqb;(https://example.org)'
);
done();
- });
- */
+ });*/
});
describe('.compile', () => {