fix: close remaining security advisory paths

This commit is contained in:
whyour committed 2026-11-20 22:36:10 +08:00
1 parent ebe1a3644b
commit 4b3c31c0a7
5 files changed
+55 -12

No files matched your search

+19
View File
@@ -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) {
+10 -1
View File
@@ -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'));
+14 -8
View File
@@ -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('暂无权限') });