fix: block case-variant secret paths and task runner env overrides

This commit is contained in:
whyour committed 2026-11-03 10:04:10 +08:00
1 parent b49b0b63e6
commit a23c5195ec
5 files changed
+46 -2

No files matched your search

+4 -1
View File
@@ -24,8 +24,11 @@ export function resolveFileAccess(
const resolvedRoot = path.resolve(root); const resolvedRoot = path.resolve(root);
const target = path.resolve(resolvedRoot, ...parts); const target = path.resolve(resolvedRoot, ...parts);
if (target === resolvedRoot || !isWithin(resolvedRoot, target)) return ''; if (target === resolvedRoot || !isWithin(resolvedRoot, target)) return '';
// File systems such as default APFS can resolve a differently cased spelling
// of the same entry. Check every protected name without case sensitivity.
const blockedNames = new Set(blacklist.map((part) => part.toLowerCase()));
const isBlocked = (relative: string) => const isBlocked = (relative: string) =>
relative.split(path.sep).some((part) => blacklist.includes(part)); relative.split(path.sep).some((part) => blockedNames.has(part.toLowerCase()));
if (isBlocked(path.relative(resolvedRoot, target))) return ''; if (isBlocked(path.relative(resolvedRoot, target))) return '';
try { try {
const realRoot = fs.realpathSync(resolvedRoot); const realRoot = fs.realpathSync(resolvedRoot);
+20
View File
@@ -7,6 +7,25 @@ const reservedNames = new Set([
'TASK_BEFORE', 'TASK_BEFORE',
'TASK_AFTER', 'TASK_AFTER',
'WORK_DIR', 'WORK_DIR',
'TASK_SHELL_PARAMS',
'SCRIPT_PARAMS',
'WHICH_PROGRAM',
'ISJSORPYTHONFILE',
'EXPORTED_VARIABLES',
'FOUND_DOUBLE_DASH',
'LOG_DIR',
'LOG_PATH',
'ID',
'BEGIN_TIMESTAMP',
'TIME_FORMAT',
'MTIME_FORMAT',
'IS_MACOS',
'RANDOMDELAY',
'RANDOMDELAYFILEEXTENSIONS',
'RANDOMDELAYIGNOREDMINUTES',
'COMMAND_TIMEOUT_TIME',
'SET_U_ON',
'_TASK_EXIT_CODE',
'PATH', 'PATH',
'HOME', 'HOME',
'SHELL', 'SHELL',
@@ -40,6 +59,7 @@ export function isAllowedTaskEnvName(name: unknown): name is string {
const upper = name.toUpperCase(); const upper = name.toUpperCase();
return ( return (
!reservedNames.has(upper) && !reservedNames.has(upper) &&
!upper.startsWith('BASH_') &&
!upper.startsWith('QL') && !upper.startsWith('QL') &&
!upper.startsWith('FILE_') && !upper.startsWith('FILE_') &&
!upper.startsWith('DIR_') && !upper.startsWith('DIR_') &&
+15 -1
View File
@@ -57,6 +57,16 @@ test('environment API rejects execution controls and legacy rows stay out of tas
{ 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' }, { id: 9, name: 'work_dir', value: '/tmp/untrusted' },
{ id: 10, name: 'task_shell_params', value: 'other.sh' },
{ id: 11, name: 'which_program', value: 'bash' },
{ id: 12, name: 'script_params', value: 'other.sh' },
{ id: 13, name: 'LOG_DIR', value: '../../config' },
{ id: 14, name: 'EXPORTED_VARIABLES', value: 'PATH' },
{ id: 15, name: 'begin_timestamp', value: 'a[$(printf BAD)]' },
{ id: 16, name: 'is_macos', value: 'a[$(printf BAD)]' },
{ id: 17, name: 'RandomDelayIgnoredMinutes', value: 'a[$(printf BAD)]' },
{ id: 18, name: 'ID', value: '999' },
{ id: 19, name: 'BASH_XTRACEFD', value: '2' },
]; ];
const Env = load(path.resolve('back/services/env.ts'), { const Env = load(path.resolve('back/services/env.ts'), {
typedi: decorators, typedi: decorators,
@@ -87,6 +97,10 @@ test('environment API rejects execution controls and legacy rows stay out of tas
for (const name of [ for (const name of [
'task_before', 'PREV_NODE_OPTIONS', 'GIT_SSH_COMMAND', 'work_dir', 'task_before', 'PREV_NODE_OPTIONS', 'GIT_SSH_COMMAND', 'work_dir',
'task_shell_params', 'which_program', 'script_params', 'LOG_DIR',
'EXPORTED_VARIABLES',
'begin_timestamp', 'is_macos', 'RandomDelayIgnoredMinutes', 'ID',
'BASH_XTRACEFD',
]) { ]) {
await assert.rejects( await assert.rejects(
service.create([ service.create([
@@ -121,7 +135,7 @@ test('environment API rejects execution controls and legacy rows stay out of tas
for (const contents of Object.values(output)) { for (const contents of Object.values(output)) {
assert.doesNotMatch( assert.doesNotMatch(
contents, contents,
/task_before|NODE_OPTIONS|GIT_SSH_COMMAND|PATH|work_dir/, /task_before|NODE_OPTIONS|GIT_SSH_COMMAND|PATH|work_dir|task_shell_params|which_program|script_params|LOG_DIR|EXPORTED_VARIABLES|begin_timestamp|is_macos|RandomDelayIgnoredMinutes|ID|BASH_XTRACEFD/,
); );
assert.match(contents, /SAFE_VALUE/); assert.match(contents, /SAFE_VALUE/);
} }
+4
View File
@@ -115,7 +115,9 @@ for (const customData of [false, true]) {
); );
for (const name of [ for (const name of [
'token.json', 'token.json',
'TOKEN.JSON',
'grpc/client.key', 'grpc/client.key',
'Grpc/client.key',
'secret-link', 'secret-link',
'data/scripts/secret-link', 'data/scripts/secret-link',
'data/scripts/../config/token.json', 'data/scripts/../config/token.json',
@@ -181,7 +183,9 @@ test('log APIs allow configuration-like names while retaining directory boundari
fs.writeFileSync(hidden, 'hidden task command'); fs.writeFileSync(hidden, 'hidden task command');
for (const values of [ for (const values of [
{ path: '.tmp', file: 'own_user.list', filename: 'own_user.list' }, { path: '.tmp', file: 'own_user.list', filename: 'own_user.list' },
{ path: '.TMP', file: 'own_user.list', filename: 'own_user.list' },
{ path: '', file: '.tmp/own_user.list', filename: '.tmp/own_user.list' }, { path: '', file: '.tmp/own_user.list', filename: '.tmp/own_user.list' },
{ path: '', file: '.Tmp/own_user.list', filename: '.Tmp/own_user.list' },
]) { ]) {
for (const [method, route] of [ for (const [method, route] of [
['get', '/detail'], ['get', '/detail'],
+3
View File
@@ -24,11 +24,14 @@ test('file access enforces directory boundaries, blacklist descendants and symli
'../config-other/secret', '../config-other/secret',
'/etc/passwd', '/etc/passwd',
'grpc/client.key', 'grpc/client.key',
'GRPC/client.key',
'alias/client.key', 'alias/client.key',
'outside/new.txt', 'outside/new.txt',
'dangling', 'dangling',
'auth.json', 'auth.json',
'AUTH.JSON',
'nested/auth.json', 'nested/auth.json',
'nested/Auth.Json',
'', '',
]) { ]) {
assert.equal( assert.equal(