From b49b0b63e61e3810b9dd8f56d47e4f35f7d27f3f Mon Sep 17 00:00:00 2001 From: whyour Date: Tue, 3 Nov 2026 09:32:40 +0800 Subject: [PATCH] fix(security): protect system app and task file boundaries --- back/services/log.ts | 2 +- back/services/open.ts | 10 ++++++++++ back/shared/taskEnvName.ts | 1 + test/back/advisory-input-security.test.cjs | 10 ++++++++-- test/back/file-access-scope.test.cjs | 16 ++++++++++++++++ test/back/systemTokenRenewal.test.cjs | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/back/services/log.ts b/back/services/log.ts index 57987a36..2434cdd7 100644 --- a/back/services/log.ts +++ b/back/services/log.ts @@ -8,6 +8,6 @@ export default class LogService { constructor(@Inject('logger') private logger: winston.Logger) {} public checkFilePath(filePath: string, fileName: string) { - return resolveFileAccess(config.logPath, [filePath || '', fileName]); + return resolveFileAccess(config.logPath, [filePath || '', fileName], ['.tmp']); } } diff --git a/back/services/open.ts b/back/services/open.ts index e39486a6..6339d64e 100644 --- a/back/services/open.ts +++ b/back/services/open.ts @@ -92,6 +92,10 @@ export default class OpenService { private async updateDb(payload: Partial): Promise { return this.withAppsLock(async () => { + const target = await AppModel.findOne({ where: { id: payload.id } }); + if (target?.name === 'system' || payload.name === 'system') { + throw Object.assign(new Error(t('暂无权限')), { status: 403 }); + } await AppModel.update(payload, { where: { id: payload.id } }); const apps = await this.find({}); await shareStore.updateApps(apps); @@ -109,6 +113,12 @@ export default class OpenService { public async remove(ids: number[]) { return this.withAppsLock(async () => { + const systemApp = await AppModel.findOne({ + where: { id: ids, name: 'system' }, + }); + if (systemApp) { + throw Object.assign(new Error(t('暂无权限')), { status: 403 }); + } await AppModel.destroy({ where: { id: ids } }); const apps = await this.find({}); await shareStore.updateApps(apps); diff --git a/back/shared/taskEnvName.ts b/back/shared/taskEnvName.ts index 3d5f8aec..a6ed36d4 100644 --- a/back/shared/taskEnvName.ts +++ b/back/shared/taskEnvName.ts @@ -6,6 +6,7 @@ import { t } from './i18n'; const reservedNames = new Set([ 'TASK_BEFORE', 'TASK_AFTER', + 'WORK_DIR', 'PATH', 'HOME', 'SHELL', diff --git a/test/back/advisory-input-security.test.cjs b/test/back/advisory-input-security.test.cjs index 6327a680..d9446176 100644 --- a/test/back/advisory-input-security.test.cjs +++ b/test/back/advisory-input-security.test.cjs @@ -56,6 +56,7 @@ test('environment API rejects execution controls and legacy rows stay out of tas { id: 6, name: 'file_task_before', value: '/tmp/untrusted.sh' }, { id: 7, name: 'SAFE_VALUE', value: 'kept' }, { id: 8, name: 'bad\nname', value: 'untrusted' }, + { id: 9, name: 'work_dir', value: '/tmp/untrusted' }, ]; const Env = load(path.resolve('back/services/env.ts'), { typedi: decorators, @@ -84,7 +85,9 @@ test('environment API rejects execution controls and legacy rows stay out of tas service.getDb = async () => ({ id: 1, name: 'SAFE_VALUE', value: 'kept' }); service.updateDb = async () => { writes++; return {}; }; - for (const name of ['task_before', 'PREV_NODE_OPTIONS', 'GIT_SSH_COMMAND']) { + for (const name of [ + 'task_before', 'PREV_NODE_OPTIONS', 'GIT_SSH_COMMAND', 'work_dir', + ]) { await assert.rejects( service.create([ { name: 'SAFE_VALUE', value: 'ok' }, @@ -116,7 +119,10 @@ test('environment API rejects execution controls and legacy rows stay out of tas assert.equal(listed[0].securityBlocked, true); assert.equal(listed[6].securityBlocked, false); for (const contents of Object.values(output)) { - assert.doesNotMatch(contents, /task_before|NODE_OPTIONS|GIT_SSH_COMMAND|PATH/); + assert.doesNotMatch( + contents, + /task_before|NODE_OPTIONS|GIT_SSH_COMMAND|PATH|work_dir/, + ); assert.match(contents, /SAFE_VALUE/); } }); diff --git a/test/back/file-access-scope.test.cjs b/test/back/file-access-scope.test.cjs index f1dfd99d..a1100c11 100644 --- a/test/back/file-access-scope.test.cjs +++ b/test/back/file-access-scope.test.cjs @@ -176,5 +176,21 @@ test('log APIs allow configuration-like names while retaining directory boundari ]) assert.equal((await invoke(method, route, values)).code, 403, filename); } + const hidden = path.join(config.logPath, '.tmp', 'own_user.list'); + fs.mkdirSync(path.dirname(hidden), { recursive: true }); + fs.writeFileSync(hidden, 'hidden task command'); + for (const values of [ + { path: '.tmp', file: 'own_user.list', filename: 'own_user.list' }, + { path: '', file: '.tmp/own_user.list', filename: '.tmp/own_user.list' }, + ]) { + for (const [method, route] of [ + ['get', '/detail'], + ['post', '/download'], + ['delete', '/'], + ]) { + assert.equal((await invoke(method, route, values)).code, 403); + } + } + assert.equal(fs.readFileSync(hidden, 'utf8'), 'hidden task command'); assert.equal(fs.readFileSync(secret, 'utf8'), 'PANEL-SECRET'); }); diff --git a/test/back/systemTokenRenewal.test.cjs b/test/back/systemTokenRenewal.test.cjs index ad185674..99251413 100644 --- a/test/back/systemTokenRenewal.test.cjs +++ b/test/back/systemTokenRenewal.test.cjs @@ -397,6 +397,22 @@ for (const mutation of ['resetSecret', 'remove', 'update', 'create']) { }); } +test('application mutations cannot change or expose the built-in system app', async (t) => { + const state = appsFixture(t); + for (const change of [ + () => state.service.resetSecret(1), + () => state.service.update({ id: 1, name: 'system', scopes: ['crons'] }), + () => state.service.update({ id: 2, name: 'system', scopes: ['crons'] }), + () => state.service.remove([1, 2]), + ]) { + await assert.rejects(change(), (error) => error.status === 403); + } + assert.equal(state.apps().length, 2); + assert.equal(state.apps()[0].client_secret, 'system-secret'); + assert.equal(state.apps()[0].tokens[0].value, 'system-token'); + assert.deepEqual(state.cached(), state.apps()); +}); + test('system initialization preserves active tokens while migrating dashboard scope', async (t) => { const state = appsFixture(t); await AppModel.update({ scopes: ['crons', 'system'] }, { where: { id: 1 } });