From 4f3565e5ed42642011e67ee82591a37b5c22cee9 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Thu, 5 Nov 2026 18:14:55 +0100 Subject: [PATCH 1/2] fix: confine panel images to assets/, restore the config editor read, load locales via require --- CHANGELOG.md | 12 ++++++++++++ src/dashboard/routes.js | 10 +++------- src/utils/locales.js | 29 +++++++++++++++++++++++++++-- src/utils/panel.js | 9 ++++++++- tests/config-route.test.js | 33 +++++++++++++++++++++++++++++++++ tests/panel-assets.test.js | 12 ++++++++++++ 6 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 tests/config-route.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 44c737e..5f166ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **Security: a panel image path can no longer leave `assets/`.** A value such + as `"../.env"` in `panel.logo.file` or `panel.banner.file` made the panel post + that file, the bot token included, to Discord as an attachment. Anyone with + `config.edit` in the dashboard could use it to read the `.env`, which is + otherwise owner only. Only a plain file name inside `assets/` (or + `assets/default/`) is accepted now. Affects every earlier version. +- The bot config editor showed no file: `GET /config/:file` answered 404 for + `config.jsonc`, `snippets.jsonc` and `.env` since the customer locale layer + went in. It reads the files directly again. +- The bot loads its locales through `require()` again, as before the customer + locale layer. Only the dashboard editor reads the raw files. + - The startup log printed `API key valid → business` in lower case, because the label map had no entry for the Business tier. It prints `Business` now. diff --git a/src/dashboard/routes.js b/src/dashboard/routes.js index 5339429..ae6b2df 100644 --- a/src/dashboard/routes.js +++ b/src/dashboard/routes.js @@ -279,13 +279,9 @@ function registerRoutes(api, { config, supervisor, requirePermission, invalidate return res.status(403).json({ error: 'Only the server owner can access the .env file.' }); } - let result; - try { - result = localeStore.readLocaleForEditor(LOCALES_DIR, path.basename(target)); - } catch { - return res.status(404).json({ error: 'File not found.' }); - } - res.json(result); + const content = await fs.readFile(target, 'utf-8').catch(() => null); + if (content === null) return res.status(404).json({ error: 'File not found.' }); + res.json({ content }); })); api.put('/config/:file', requirePermission('config.edit'), asyncRoute(async (req, res) => { diff --git a/src/utils/locales.js b/src/utils/locales.js index 76eaa36..7045942 100644 --- a/src/utils/locales.js +++ b/src/utils/locales.js @@ -143,11 +143,36 @@ function loadLocale(code, { dir = LOCALES_DIR, warn } = {}) { throw new Error(`Invalid locale code: ${code}`); } const fileName = `${code}.json`; - const shipped = JSON.parse(fs.readFileSync(path.join(dir, fileName), 'utf-8')); - const custom = readCustomFile(customPathFor(fileName, dir), warn); + const shipped = requireFresh(path.join(dir, fileName)); + const custom = requireCustomFile(customPathFor(fileName, dir), warn); return custom ? mergeLocale(shipped, custom) : shipped; } +// The bot loads its locales through require(), as it did before the customer +// layer existed. The cache entry is dropped first, so an edit saved in the +// dashboard is picked up on the next load instead of after a restart. The +// dashboard editor below reads with fs, since it needs the raw text. +function requireFresh(file) { + const resolved = require.resolve(file); + delete require.cache[resolved]; + return require(resolved); +} + +function requireCustomFile(file, warn = (m) => logger.warn(m)) { + let parsed; + try { + parsed = requireFresh(file); + } catch (err) { + if (err.code !== 'MODULE_NOT_FOUND') warn(`[Locale] Ignoring ${file}: ${err.message}. Using the shipped texts.`); + return null; + } + if (!isPlainObject(parsed)) { + warn(`[Locale] Ignoring ${file}: not a JSON object. Using the shipped texts.`); + return null; + } + return parsed; +} + // ── Dashboard editor helpers ───────────────────────────────────────────────── // The dashboard resolves the file NAME against the real directory listing first // (routes.js) and passes in the matched entry, so every path below is built from diff --git a/src/utils/panel.js b/src/utils/panel.js index 13cb8db..f675201 100644 --- a/src/utils/panel.js +++ b/src/utils/panel.js @@ -24,7 +24,14 @@ const ASSETS_DIR = path.resolve(__dirname, '../../assets'); */ function resolvePanelAsset(file, assetsDir = ASSETS_DIR) { if (typeof file !== 'string' || file === '') return null; - for (const candidate of [path.resolve(assetsDir, file), path.resolve(assetsDir, 'default', file)]) { + // Only a plain file name inside assets/ is allowed. Without this, a config + // value like "../.env" made the panel post the bot's .env (token included) + // to Discord as an attachment. + if (file !== path.basename(file) || file === '.' || file === '..' || file.includes('\\') || file.includes('/')) return null; + const root = path.resolve(assetsDir); + for (const candidate of [path.resolve(root, file), path.resolve(root, 'default', file)]) { + const rel = path.relative(root, candidate); + if (rel.startsWith('..') || path.isAbsolute(rel)) continue; if (fs.statSync(candidate, { throwIfNoEntry: false })?.isFile()) return candidate; } return null; diff --git a/tests/config-route.test.js b/tests/config-route.test.js new file mode 100644 index 0000000..b87566f --- /dev/null +++ b/tests/config-route.test.js @@ -0,0 +1,33 @@ +'use strict'; + +// The bot config editor reads config/*.jsonc and .env through GET /config/:file. +// PR #46 accidentally routed that read through the locale loader, so every file +// answered 404 and the editor stayed empty. + +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); +const express = require('express'); +const { registerRoutes } = require('../src/dashboard/routes'); + +const SNIPPETS = path.resolve(__dirname, '../config/snippets.jsonc'); + +test('GET /config/snippets returns the file content', async (t) => { + const existed = fs.existsSync(SNIPPETS); + const original = existed ? fs.readFileSync(SNIPPETS, 'utf-8') : '// test snippets\n{}\n'; + if (!existed) fs.writeFileSync(SNIPPETS, original); + t.after(() => { if (!existed) fs.rmSync(SNIPPETS, { force: true }); }); + + const app = express(); + const api = express.Router(); + app.use((req, _res, next) => { req.auth = { isOwner: true, userId: '1' }; next(); }); + registerRoutes(api, { config: { guildId: '1' }, supervisor: {}, requirePermission: () => (_req, _res, next) => next(), invalidateMemberCache: () => {} }); + app.use('/api', api); + const server = await new Promise(r => { const s = app.listen(0, '127.0.0.1', () => r(s)); }); + t.after(() => server.close()); + + const res = await fetch(`http://127.0.0.1:${server.address().port}/api/config/snippets`); + assert.equal(res.status, 200); + assert.deepEqual(await res.json(), { content: original }); +}); diff --git a/tests/panel-assets.test.js b/tests/panel-assets.test.js index b9f921e..f4034a0 100644 --- a/tests/panel-assets.test.js +++ b/tests/panel-assets.test.js @@ -42,3 +42,15 @@ test('the repository ships the default logo for panel.logo.file "logo.png"', () assert.ok(fs.statSync(path.resolve(__dirname, '../assets/default/logo.png')).isFile()); assert.ok(resolvePanelAsset('logo.png')); }); + +test('resolvePanelAsset: a path outside assets/ is never resolved', () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tb-assets-')); + fs.mkdirSync(path.join(dir, 'assets')); + fs.writeFileSync(path.join(dir, '.env'), 'TOKEN=secret'); + fs.writeFileSync(path.join(dir, 'assets', 'logo.png'), 'x'); + const assets = path.join(dir, 'assets'); + for (const bad of ['../.env', '..\.env', '/etc/passwd', path.join(dir, '.env'), '..', '.', 'default/../../.env']) { + assert.equal(resolvePanelAsset(bad, assets), null, bad); + } + assert.equal(resolvePanelAsset('logo.png', assets), path.join(assets, 'logo.png')); +}); From 44da4a75c393c1c8f37525d0efa102bae3c8552a Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Thu, 5 Nov 2026 18:17:10 +0100 Subject: [PATCH 2/2] test: create the snippets fixture without a check-then-write race --- tests/config-route.test.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/config-route.test.js b/tests/config-route.test.js index b87566f..596d6a6 100644 --- a/tests/config-route.test.js +++ b/tests/config-route.test.js @@ -14,10 +14,18 @@ const { registerRoutes } = require('../src/dashboard/routes'); const SNIPPETS = path.resolve(__dirname, '../config/snippets.jsonc'); test('GET /config/snippets returns the file content', async (t) => { - const existed = fs.existsSync(SNIPPETS); - const original = existed ? fs.readFileSync(SNIPPETS, 'utf-8') : '// test snippets\n{}\n'; - if (!existed) fs.writeFileSync(SNIPPETS, original); - t.after(() => { if (!existed) fs.rmSync(SNIPPETS, { force: true }); }); + // Use the local snippets file if there is one; otherwise create it ('wx' fails + // instead of overwriting if it appeared in between) and remove it afterwards. + let original = '// test snippets\n{}\n'; + let created = false; + try { + fs.writeFileSync(SNIPPETS, original, { encoding: 'utf-8', flag: 'wx' }); + created = true; + } catch (err) { + if (err.code !== 'EEXIST') throw err; + original = fs.readFileSync(SNIPPETS, 'utf-8'); + } + t.after(() => { if (created) fs.rmSync(SNIPPETS, { force: true }); }); const app = express(); const api = express.Router();