mirror of
https://github.com/whyour/qinglong.git
synced 2026-12-01 01:39:52 +08:00
Share durable permits across manual, scheduled and recovered runs, prioritize short ready tasks with aging, and record global queue occupancy. Reduce launch, log polling, dashboard and SQLite overhead while preserving transactional receipts, cancellation, recovery and script hook compatibility. Keep ordinary CRUD in ORM and bound SQL on measured execution hot paths. Fix supervisor descendant tracking, asynchronous log draining, concurrent log directory creation, Linux fallback launches and snapshot clock rollback. Include regression coverage, SQL audits and evolution/review documentation. Validation: five review rounds; local full suite 1478 passed, 28 platform skips; dev suites 415 passed, no failures/skips; backend types and formatting passed.
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
function isWithin(root: string, target: string): boolean {
|
|
const relative = path.relative(root, target);
|
|
return (
|
|
relative === '' ||
|
|
(!relative.startsWith(`..${path.sep}`) &&
|
|
relative !== '..' &&
|
|
!path.isAbsolute(relative))
|
|
);
|
|
}
|
|
|
|
/** Resolve existing files and not-yet-created children without following an
|
|
* existing symlink outside the root. Blacklisted directories cover descendants. */
|
|
export function resolveFileAccess(
|
|
root: string,
|
|
parts: string[],
|
|
blacklist: string[] = [],
|
|
): string {
|
|
if (parts.some((part) => typeof part !== 'string' || part.includes('\0'))) {
|
|
return '';
|
|
}
|
|
const resolvedRoot = path.resolve(root);
|
|
const target = path.resolve(resolvedRoot, ...parts);
|
|
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) =>
|
|
relative
|
|
.split(path.sep)
|
|
.some((part) => blockedNames.has(part.toLowerCase()));
|
|
if (isBlocked(path.relative(resolvedRoot, target))) return '';
|
|
try {
|
|
const realRoot = fs.realpathSync(resolvedRoot);
|
|
let existing = target;
|
|
const missing: string[] = [];
|
|
while (!fs.existsSync(existing)) {
|
|
// existsSync is false for a dangling symlink; never treat one as absent.
|
|
try {
|
|
const entry = fs.lstatSync(existing);
|
|
if (entry.isSymbolicLink()) return '';
|
|
// Another task may have created this directory after existsSync.
|
|
// Canonicalize the now-existing entry below, with the same root and
|
|
// blacklist checks, rather than rejecting a legitimate concurrent run.
|
|
break;
|
|
} catch (error: any) {
|
|
if (error.code !== 'ENOENT') return '';
|
|
}
|
|
if (existing === resolvedRoot) return '';
|
|
missing.unshift(path.basename(existing));
|
|
existing = path.dirname(existing);
|
|
}
|
|
const realTarget = path.resolve(fs.realpathSync(existing), ...missing);
|
|
if (
|
|
!isWithin(realRoot, realTarget) ||
|
|
isBlocked(path.relative(realRoot, realTarget))
|
|
) {
|
|
return '';
|
|
}
|
|
return target;
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|