diff --git a/back/api/config.ts b/back/api/config.ts index d58afc25..170d2603 100644 --- a/back/api/config.ts +++ b/back/api/config.ts @@ -10,7 +10,16 @@ import ConfigService from '../services/config'; import { writeFileWithLock } from '../shared/utils'; import { resolveFileAccess } from '../shared/fileAccess'; import { rateLimit } from 'express-rate-limit'; +import path from 'path'; const route = Router(); +const openApiProtectedFiles = new Set([ + 'config.sh', + 'task_before.sh', + 'task_after.sh', + 'task_before.js', + 'task_before.py', + 'extra.sh', +]); export default (app: Router) => { app.use('/configs', route); @@ -85,6 +94,16 @@ export default (app: Router) => { if (!normalized) { return res.send({ code: 403, message: t('文件无法访问') }); } + // OpenAPI config scopes grant data-file access, not executable hooks. + if (/(?:^|\/)open\/configs(?:\/|\?|$)/.test(req.originalUrl || '')) { + const realPath = await fs.realpath(normalized).catch(() => normalized); + if ( + openApiProtectedFiles.has(path.basename(normalized).toLowerCase()) || + openApiProtectedFiles.has(path.basename(realPath).toLowerCase()) + ) { + return res.send({ code: 403, message: t('文件无法访问') }); + } + } await writeFileWithLock(normalized, content); res.send({ code: 200, message: t('保存成功') }); } catch (e) { diff --git a/back/api/dashboard.ts b/back/api/dashboard.ts index 238bf7a5..9a0f50ea 100644 --- a/back/api/dashboard.ts +++ b/back/api/dashboard.ts @@ -208,7 +208,16 @@ export default (app: Router) => { '/trend', async (req: Request, res: Response, next: NextFunction) => { try { - const days = parseInt(req.query.days as string) || 7; + const requestedDays = req.query.days; + const days = + requestedDays === undefined + ? 7 + : typeof requestedDays === 'string' && /^[1-9]\d{0,2}$/.test(requestedDays) + ? Number(requestedDays) + : NaN; + if (!Number.isInteger(days) || days > 365) { + return res.status(400).send({ code: 400, message: t('无效的天数') }); + } const dates: string[] = []; for (let i = days - 1; i >= 0; i--) { dates.push(dayjs().subtract(i, 'day').format('YYYY-MM-DD')); diff --git a/back/api/script.ts b/back/api/script.ts index 53182a17..908ca3a0 100644 --- a/back/api/script.ts +++ b/back/api/script.ts @@ -50,8 +50,13 @@ function isPathAllowed(targetPath: string): boolean { // Return the checked, normalized value so filesystem calls never reuse the // original request path. Keep the root boundary visible to static analysis. -function writablePath(targetPath: string): string { +function writablePath(targetPath: string, req?: Request): string { const normalized = resolve(targetPath); + // An OpenAPI scripts token must never write into the configuration tree. + if ( + /(?:^|\/)open\/scripts(?:\/|\?|$)/.test(req?.originalUrl || '') && + !resolveFileAccess(config.scriptPath, [normalized]) + ) return ''; if (!isPathAllowed(normalized)) return ''; for (const writableRoot of config.writePathList) { const root = resolve(writableRoot) + sep; @@ -325,7 +330,7 @@ export default (app: Router) => { } if (req.file) { - const uploadPath = writablePath(join(path, filename)); + const uploadPath = writablePath(join(path, filename), req); const temporaryPath = uploadedPath(req.file.path); if (!uploadPath || !temporaryPath) { return res.send({ code: 403, message: t('暂无权限') }); @@ -336,7 +341,7 @@ export default (app: Router) => { } if (directory) { - const dirPath = writablePath(join(path, directory)); + const dirPath = writablePath(join(path, directory), req); if (!dirPath) { return res.send({ code: 403, message: t('暂无权限') }); } @@ -347,8 +352,8 @@ export default (app: Router) => { if (!originFilename) { originFilename = filename; } - const originFilePath = writablePath(join(path, originFilename)); - const filePath = writablePath(join(path, filename)); + const originFilePath = writablePath(join(path, originFilename), req); + const filePath = writablePath(join(path, filename), req); if (!filePath || !originFilePath) { return res.send({ code: 403, message: t('暂无权限') }); } @@ -549,7 +554,7 @@ export default (app: Router) => { } const { name, ext } = parse(filename); const filePath = writablePath( - join(config.scriptPath, path, `${name}.swap${ext}`), + join(config.scriptPath, path, `${name}.swap${ext}`), req, ); if (!filePath) { return res.send({ code: 403, message: t('暂无权限') }); @@ -582,7 +587,7 @@ export default (app: Router) => { } const { name, ext } = parse(filename); const filePath = writablePath( - join(config.scriptPath, path, `${name}.swap${ext}`), + join(config.scriptPath, path, `${name}.swap${ext}`), req, ); if (!filePath) { return res.send({ code: 403, message: t('暂无权限') }); @@ -630,9 +635,10 @@ export default (app: Router) => { if (!path) { path = ''; } - const filePath = writablePath(join(config.scriptPath, path, filename)); + const filePath = writablePath(join(config.scriptPath, path, filename), req); const newPath = writablePath( join(config.scriptPath, path, newFilename), + req, ); if (!filePath || !newPath) { return res.send({ code: 403, message: t('暂无权限') }); diff --git a/back/shared/taskEnvName.ts b/back/shared/taskEnvName.ts index 42742145..10e9ecef 100644 --- a/back/shared/taskEnvName.ts +++ b/back/shared/taskEnvName.ts @@ -40,6 +40,10 @@ const reservedNames = new Set([ 'PS4', 'NODE_OPTIONS', 'NODE_PATH', + 'PERL5OPT', + 'PERL5DB', + 'PERL5LIB', + 'PERLLIB', 'PREV_NODE_OPTIONS', 'PREV_NODE_PATH', 'PREV_PYTHONPATH', diff --git a/shell/otask.sh b/shell/otask.sh index c3d7f19d..091a381b 100755 --- a/shell/otask.sh +++ b/shell/otask.sh @@ -167,13 +167,18 @@ run_normal() { run_task_command ${which_program:+"$which_program"} "$file_param" "${script_params[@]}" } +safe_perl() { + # Task environment values must not configure Perl before helper code runs. + env -u PERL5OPT -u PERL5DB -u PERL5LIB -u PERLLIB -u PERL5SHELL -u PERL_LOCAL_LIB_ROOT perl "$@" +} + handle_env_split() { if [[ ! $num_param ]]; then num_param="1-max" fi env_str_to_array - local tempArr=$(echo $num_param | sed "s/-max/-${array_length}/g" | sed "s/max-/${array_length}-/g" | perl -pe "s|(\d+)(-\|~\|_)(\d+)|{\1..\3}|g") + local tempArr=$(echo $num_param | sed "s/-max/-${array_length}/g" | sed "s/max-/${array_length}-/g" | safe_perl -pe "s|(\d+)(-\|~\|_)(\d+)|{\1..\3}|g") local runArr=($(eval echo $tempArr)) array_run=($(awk -v RS=' ' '!a[$1]++' <<<"${runArr[*]}")) } @@ -182,7 +187,7 @@ handle_env_split() { run_concurrent() { local file_param="$1" local env_param="$2" - local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1') + local num_param=$(echo "$3" | safe_perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1') if [[ ! $env_param ]]; then t '\n缺少并发运行的环境变量参数' exit 1 @@ -219,7 +224,7 @@ run_concurrent() { run_designated() { local file_param="$1" local env_param="$2" - local num_param=$(echo "$3" | perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1') + local num_param=$(echo "$3" | safe_perl -pe "s|.*$2(.*)|\1|" | awk '{$1=$1};1') if [[ ! $env_param ]]; then t '\n缺少单独运行的参数 task xxx.js desi Test' exit 1