mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 18:18:52 +08:00
fix(security): protect system app and task file boundaries
This commit is contained in:
1 parent
487b213dd8
commit
b49b0b63e6
6 files changed
+52
-3
No files matched your search
@@ -8,6 +8,6 @@ export default class LogService {
|
|||||||
constructor(@Inject('logger') private logger: winston.Logger) {}
|
constructor(@Inject('logger') private logger: winston.Logger) {}
|
||||||
|
|
||||||
public checkFilePath(filePath: string, fileName: string) {
|
public checkFilePath(filePath: string, fileName: string) {
|
||||||
return resolveFileAccess(config.logPath, [filePath || '', fileName]);
|
return resolveFileAccess(config.logPath, [filePath || '', fileName], ['.tmp']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,10 @@ export default class OpenService {
|
|||||||
|
|
||||||
private async updateDb(payload: Partial<App>): Promise<App> {
|
private async updateDb(payload: Partial<App>): Promise<App> {
|
||||||
return this.withAppsLock(async () => {
|
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 } });
|
await AppModel.update(payload, { where: { id: payload.id } });
|
||||||
const apps = await this.find({});
|
const apps = await this.find({});
|
||||||
await shareStore.updateApps(apps);
|
await shareStore.updateApps(apps);
|
||||||
@@ -109,6 +113,12 @@ export default class OpenService {
|
|||||||
|
|
||||||
public async remove(ids: number[]) {
|
public async remove(ids: number[]) {
|
||||||
return this.withAppsLock(async () => {
|
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 } });
|
await AppModel.destroy({ where: { id: ids } });
|
||||||
const apps = await this.find({});
|
const apps = await this.find({});
|
||||||
await shareStore.updateApps(apps);
|
await shareStore.updateApps(apps);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { t } from './i18n';
|
|||||||
const reservedNames = new Set([
|
const reservedNames = new Set([
|
||||||
'TASK_BEFORE',
|
'TASK_BEFORE',
|
||||||
'TASK_AFTER',
|
'TASK_AFTER',
|
||||||
|
'WORK_DIR',
|
||||||
'PATH',
|
'PATH',
|
||||||
'HOME',
|
'HOME',
|
||||||
'SHELL',
|
'SHELL',
|
||||||
|
|||||||
@@ -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: 6, name: 'file_task_before', value: '/tmp/untrusted.sh' },
|
||||||
{ id: 7, name: 'SAFE_VALUE', value: 'kept' },
|
{ id: 7, name: 'SAFE_VALUE', value: 'kept' },
|
||||||
{ id: 8, name: 'bad\nname', value: 'untrusted' },
|
{ id: 8, name: 'bad\nname', value: 'untrusted' },
|
||||||
|
{ id: 9, name: 'work_dir', value: '/tmp/untrusted' },
|
||||||
];
|
];
|
||||||
const Env = load(path.resolve('back/services/env.ts'), {
|
const Env = load(path.resolve('back/services/env.ts'), {
|
||||||
typedi: decorators,
|
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.getDb = async () => ({ id: 1, name: 'SAFE_VALUE', value: 'kept' });
|
||||||
service.updateDb = async () => { writes++; return {}; };
|
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(
|
await assert.rejects(
|
||||||
service.create([
|
service.create([
|
||||||
{ name: 'SAFE_VALUE', value: 'ok' },
|
{ 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[0].securityBlocked, true);
|
||||||
assert.equal(listed[6].securityBlocked, false);
|
assert.equal(listed[6].securityBlocked, false);
|
||||||
for (const contents of Object.values(output)) {
|
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/);
|
assert.match(contents, /SAFE_VALUE/);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
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');
|
assert.equal(fs.readFileSync(secret, 'utf8'), 'PANEL-SECRET');
|
||||||
});
|
});
|
||||||
@@ -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) => {
|
test('system initialization preserves active tokens while migrating dashboard scope', async (t) => {
|
||||||
const state = appsFixture(t);
|
const state = appsFixture(t);
|
||||||
await AppModel.update({ scopes: ['crons', 'system'] }, { where: { id: 1 } });
|
await AppModel.update({ scopes: ['crons', 'system'] }, { where: { id: 1 } });
|
||||||
|
|||||||
Reference in new issue
Block a user