Node.js based forum software built for the modern web
fix: validate theme id on admin.themes.set
Barış Soner Uşaklı24c9d560c2ee
diff --git a/src/cli/reset.js b/src/cli/reset.jsindex bac1183e11..65d8f4f2c8 100644--- a/src/cli/reset.js+++ b/src/cli/reset.js@@ -147,6 +147,7 @@ async function resetPlugins() {process.exit(1);}await db.delete('plugins:active');+ await db.setObjectField('config', 'theme:id', '');winston.info('[reset] All Plugins De-activated');}diff --git a/src/meta/themes.js b/src/meta/themes.jsindex 198b02b267..2b58791fc5 100644--- a/src/meta/themes.js+++ b/src/meta/themes.js@@ -88,55 +88,59 @@ async function getThemes(themePath) {Themes.set = async (data) => {switch (data.type) {case 'local': {+ if (!themeNamePattern.test(data.id)) {+ throw new Error('[[error:invalid-theme-id]]');+ }const current = await Meta.configs.get('theme:id');+ if (current && current === data.id) {+ return;+ }++ const themesPath = nconf.get('themes_path');+ const pathToThemeJson = path.join(themesPath, data.id, 'theme.json');+ if (!file.isPathInside(themesPath, pathToThemeJson)) {+ throw new Error('[[error:invalid-theme-id]]');+ }++ let config = await fs.promises.readFile(pathToThemeJson, 'utf8');+ config = JSON.parse(config);+ const activePluginsConfig = nconf.get('plugins:active');+ if (activePluginsConfig && !activePluginsConfig.includes(current)) {+ // This prevents changing theme when configuration doesn't include it, but allows it otherwise+ winston.error(`When defining active plugins in configuration, changing themes requires adding the theme '${data.id}' to the list of active plugins before updating it in the ACP`);+ throw new Error('[[error:theme-not-set-in-configuration]]');+ }+const score = await db.sortedSetScore('plugins:active', current);await db.sortedSetRemove('plugins:active', current);await db.sortedSetAdd('plugins:active', score || 0, data.id);- if (current !== data.id) {- const themesPath = nconf.get('themes_path');- const pathToThemeJson = path.join(themesPath, data.id, 'theme.json');- if (!file.isPathInside(themesPath, pathToThemeJson)) {- throw new Error('[[error:invalid-theme-id]]');- }-- let config = await fs.promises.readFile(pathToThemeJson, 'utf8');- config = JSON.parse(config);- const activePluginsConfig = nconf.get('plugins:active');- if (!activePluginsConfig) {- const score = await db.sortedSetScore('plugins:active', current);- await db.sortedSetRemove('plugins:active', current);- await db.sortedSetAdd('plugins:active', score || 0, data.id);- } else if (!activePluginsConfig.includes(data.id)) {- // This prevents changing theme when configuration doesn't include it, but allows it otherwise- winston.error(`When defining active plugins in configuration, changing themes requires adding the theme '${data.id}' to the list of active plugins before updating it in the ACP`);- throw new Error('[[error:theme-not-set-in-configuration]]');- }-- // Re-set the themes path (for when NodeBB is reloaded)- Themes.setPath(config);-- await Meta.configs.setMultiple({- 'theme:type': data.type,- 'theme:id': data.id,- 'theme:staticDir': config.staticDir ? config.staticDir : '',- 'theme:templates': config.templates ? config.templates : '',- 'theme:src': '',- bootswatchSkin: '',- });-- await events.log({- type: 'theme-set',- uid: parseInt(data.uid, 10) || 0,- ip: data.ip || '127.0.0.1',- text: data.id,- });-- Meta.reloadRequired = true;- }+ // Re-set the themes path (for when NodeBB is reloaded)+ Themes.setPath(config);++ await Meta.configs.setMultiple({+ 'theme:type': data.type,+ 'theme:id': data.id,+ 'theme:staticDir': config.staticDir ? config.staticDir : '',+ 'theme:templates': config.templates ? config.templates : '',+ 'theme:src': '',+ bootswatchSkin: '',+ });++ await events.log({+ type: 'theme-set',+ uid: parseInt(data.uid, 10) || 0,+ ip: data.ip || '127.0.0.1',+ text: data.id,+ });++ Meta.reloadRequired = true;break;}case 'bootswatch':+ if (!Meta.css.supportedSkins.includes(data.id.toLowerCase())) {+ throw new Error('[[error:invalid-skin-id]]');+ }await Meta.configs.setMultiple({'theme:src': data.src,bootswatchSkin: data.id.toLowerCase(),diff --git a/src/plugins/data.js b/src/plugins/data.jsindex 0078fa34b8..965d995600 100644--- a/src/plugins/data.js+++ b/src/plugins/data.js@@ -8,7 +8,7 @@ const nconf = require('nconf');const db = require('../database');const file = require('../file');-const { paths } = require('../constants');+const { paths, pluginNamePattern } = require('../constants');const Data = module.exports;@@ -25,8 +25,9 @@ async function getActiveIds() {Data.getPluginPaths = async function () {const plugins = await getActiveIds();- const pluginPaths = plugins.filter(plugin => plugin && typeof plugin === 'string')- .map(plugin => path.join(paths.nodeModules, plugin));+ const pluginPaths = plugins.filter(+ p => typeof p === 'string' && pluginNamePattern.test(p)+ ).map(plugin => path.join(paths.nodeModules, plugin));const exists = await Promise.all(pluginPaths.map(file.exists));exists.forEach((exists, i) => {if (!exists) {