refactor: modularize backend security and observability

This commit is contained in:
237899745
2026-07-27 13:06:54 +08:00
parent f90311e68c
commit dce1e3622a
25 changed files with 1633 additions and 849 deletions

View File

@@ -0,0 +1,34 @@
const path = require('path');
function isPathInside(parent, child) {
const relativePath = path.relative(parent, child);
return relativePath === '' || (!relativePath.startsWith('..') && !path.isAbsolute(relativePath));
}
function normalizeVirtualPath(rawPath) {
if (typeof rawPath !== 'string') return null;
let decoded = rawPath;
try {
decoded = decodeURIComponent(rawPath);
} catch {
// The traversal checks below still apply to malformed encoded input.
}
if (decoded.includes('\x00') || decoded.toLowerCase().includes('%00')) return null;
const unifiedPath = decoded.replace(/\\/g, '/');
if (/(^|\/)\.\.(\/|$)/.test(unifiedPath)) return null;
let normalized = path.posix.normalize(unifiedPath);
if (normalized === '' || normalized === '.') normalized = '/';
if (!normalized.startsWith('/')) normalized = `/${normalized}`;
normalized = normalized.replace(/\/+$/g, '');
return normalized || '/';
}
module.exports = {
isPathInside,
normalizeVirtualPath
};