From 12dfa1ae4915d129d8a876e9ccbc203e78630237 Mon Sep 17 00:00:00 2001 From: Terncode Date: Mon, 16 Nov 2026 18:34:12 +0100 Subject: [PATCH] Fix command injection and path traversal in tools api --- src/ts/server/api/admin.ts | 1 + src/ts/server/routes/api-tools.ts | 75 ++++++++++++++++++++++--------- src/ts/server/serverUtils.ts | 16 ++++++- 3 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/ts/server/api/admin.ts b/src/ts/server/api/admin.ts index ca7854d..ab7bbb4 100644 --- a/src/ts/server/api/admin.ts +++ b/src/ts/server/api/admin.ts @@ -108,6 +108,7 @@ export async function getChat(search: string, date: string, caseInsensitive: boo const query = search .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') + .replace(/`/g, '\\`') .replace(/\./g, '\\.') .replace(/\*/g, '\\*') .replace(/\$/g, '\\*') diff --git a/src/ts/server/routes/api-tools.ts b/src/ts/server/routes/api-tools.ts index e4b1823..3919a93 100644 --- a/src/ts/server/routes/api-tools.ts +++ b/src/ts/server/routes/api-tools.ts @@ -1,10 +1,10 @@ import { Router } from 'express'; import fs from 'fs'; import * as path from 'path'; -import { repeat } from 'lodash'; +import { times } from 'lodash'; import { randomString } from '../../common/stringUtils'; -import { execAsync } from '../serverUtils'; -import { offline as createOffline, handleJSON, auth } from '../requestUtils'; +import { execFileAsync } from '../serverUtils'; +import { offline as createOffline, handleJSON, handleError, auth, limit } from '../requestUtils'; import * as paths from '../paths'; import { findAllCharacters, IAccount } from '../db'; import { createGetAccountCharacters } from '../api/account'; @@ -14,6 +14,15 @@ import { ToolsMapInfo } from '../../components/tools/tools-map/tools-map'; import { flatten } from '../../common/utils'; import { serializeMap } from '../serverMap'; +function isValidAnimationFileName(name: unknown): name is string { + return typeof name === 'string' && /^[a-z0-9]{10}(\.png|\.gif)?$/.test(name); +} + +function toIntInRange(value: unknown, defaultValue: number, min: number, max: number) { + const number = typeof value === 'number' ? value : (typeof value === 'string' ? parseInt(value, 10) : NaN); + return isFinite(number) ? Math.min(Math.max(Math.floor(number), min), max) : defaultValue; +} + export default function (server: ServerConfig, settings: Settings, world: World | undefined) { const offline = createOffline(settings); const app = Router(); @@ -25,38 +34,62 @@ export default function (server: ServerConfig, settings: Settings, world: World }); app.get<{ id: string }>('/animation/:id', offline, (req, res) => { - const filePath = path.join(paths.store, req.params.id); + const id = req.params.id; - res.sendFile(filePath); + if (!isValidAnimationFileName(id)) { + res.sendStatus(404); + return; + } + + res.sendFile(id, { root: paths.store }, error => { + if (error && !res.headersSent) { + res.sendStatus(404); + } + }); }); - app.post('/animation', offline, (req, res) => { + app.post('/animation', offline, limit(30, 60), (req, res) => { + const animation = req.body.animation; + + if (!animation || typeof animation !== 'object') { + res.sendStatus(400); + return; + } + const name = randomString(10); const filePath = path.join(paths.store, name); - fs.writeFileAsync(filePath, req.body.animation, 'utf8') - .then(() => res.send({ name })); + fs.writeFileAsync(filePath, JSON.stringify(animation), 'utf8') + .then(() => res.send({ name })) + .catch(handleError(server, req, res)); }); - app.post('/animation-gif', offline, (req, res) => { - const image: string = req.body.image; - const width: number = req.body.width || 80; - const height: number = req.body.height || 80; - const fps: number = req.body.fps || 24; - const remove: number = req.body.remove || 0; + app.post('/animation-gif', offline, limit(30, 60), (req, res) => { + const image = req.body.image; + const width = toIntInRange(req.body.width, 80, 1, 4096); + const height = toIntInRange(req.body.height, 80, 1, 4096); + const fps = toIntInRange(req.body.fps, 24, 1, 100); + const remove = toIntInRange(req.body.remove, 0, 0, 1000); + + if (typeof image !== 'string' || !/^data:image\/(png|gif);base64,/.test(image)) { + res.sendStatus(400); + return; + } const name = randomString(10); const filePath = path.join(paths.store, name + '.png'); - const header = 'data:image/gif;base64,'; - const buffer = Buffer.from(image.substr(header.length), 'base64'); + const buffer = Buffer.from(image.substr(image.indexOf(',') + 1), 'base64'); const magick = /^win/.test(process.platform) ? 'magick' : 'convert'; - const command = `${magick} -dispose Background -delay ${100 / fps} -loop 0 "${filePath}" -crop ${width}x${height} ` - + `+repage${repeat(' +delete', remove)} "${filePath.replace(/png$/, 'gif')}"`; - console.log('executing ' + command); + const args = [ + '-dispose', 'Background', '-delay', `${100 / fps}`, '-loop', '0', filePath, + '-crop', `${width}x${height}`, '+repage', ...times(remove, () => '+delete'), + filePath.replace(/png$/, 'gif'), + ]; fs.writeFileAsync(filePath, buffer) - .then(() => execAsync(command)) - .then(() => res.send({ name })); + .then(() => execFileAsync(magick, args)) + .then(() => res.send({ name })) + .catch(handleError(server, req, res)); }); app.get('/maps', offline, (_, res) => { diff --git a/src/ts/server/serverUtils.ts b/src/ts/server/serverUtils.ts index dedb90d..5494c86 100644 --- a/src/ts/server/serverUtils.ts +++ b/src/ts/server/serverUtils.ts @@ -1,5 +1,5 @@ import fs from 'fs'; -import { exec, ExecOptions } from 'child_process'; +import { exec, execFile, ExecOptions } from 'child_process'; import { noop } from 'lodash'; import { Server } from 'ag-sockets'; import { AccountData, SocialSite, PonyObject, AccountDataFlags } from '../common/interfaces'; @@ -88,6 +88,20 @@ export function execAsync(command: string, options?: ExecOptions) { }); } +/* istanbul ignore next */ +export function execFileAsync(file: string, args: string[], options?: ExecOptions) { + return new Promise<{ stdout: string; stderr: string; }>((resolve, reject) => { + execFile(file, args, options || {}, (error, stdout, stderr) => { + if (error) { + reject(error); + } + else { + resolve({ stdout: stdout.toString(), stderr: stderr.toString() }); + } + }); + }); +} + /* istanbul ignore next */ export async function logErrorToFile(message: string, data: any) { const fileName = `error-${Date.now()}.json`;