Heuristic:3641397194 wq Gpt6 Astra Path Guard Ancestor Walk
| Knowledge Sources | |
|---|---|
| Domains | Security, File_System |
| Last Updated | 2026-10-26 14:00 GMT |
Overview
Walk every ancestor directory of a target path to detect symlinks, junctions, and hardlinks before any file write, preventing path traversal attacks through filesystem indirection.
Description
The `guardPath` function performs a bottom-up ancestor walk from the target file to the filesystem root. At each level, it calls `fs.lstatSync` to check for symbolic links (including Windows junctions) and files with `nlink > 1` (hardlinks). If the native `realpathSync` of any ancestor differs from its lexical path — indicating a spelling change that could be a junction — it performs a second top-down walk of the canonical chain to catch links that the first pass might miss. This dual-pass strategy avoids caching a "safe" bit that could miss a junction swap between sibling writes.
Usage
Use this heuristic for all filesystem writes in the seat transaction layer. It is critical when writing to user configuration directories that may contain symlinks (e.g., `~/.config` linked to another location) or when an adversary could create a junction to redirect writes. Apply before any `read`, `write`, or `atomic` operation on a user-provided path.
The Insight (Rule of Thumb)
- Action: Call `guardPath(file)` before every `read`, `write`, or `atomic` operation
- Value: Reject if any ancestor is a symlink (`isSymbolicLink()`) or any file has `nlink > 1`
- Trade-off: Performance cost of `lstatSync` per ancestor per operation; legitimate symlinked home directories are rejected
- Compatibility: Windows strips `\\?\` namespace prefixes; POSIX uses raw paths
Reasoning
A symlink or junction anywhere in the path ancestry can redirect a write to an arbitrary location outside the intended directory. A single check of the target path is insufficient because the link can be at any parent level. The ancestor walk ensures no level is unchecked. The additional canonical-chain walk handles Windows 8.3 short-name expansion and other native spelling changes that could hide a junction from the first pass. Hardlinks (`nlink > 1`) are rejected because writes to a hardlinked file silently affect all links, violating the atomic transaction guarantee.
Code Evidence
Ancestor walk from `seat-transactions.js:19-39`:
function guardPath(file) {
const normalize = (value) => process.platform === 'win32' ? absolutePath(value).toLowerCase() : absolutePath(value);
for (let part = absolutePath(file); ; part = path.dirname(part)) {
let stat;
try { stat = fs.lstatSync(part); } catch (error) { if (error.code === 'ENOENT' && path.dirname(part) !== part) continue; throw error; }
if (stat.isSymbolicLink()) throw new Error('目标路径包含符号链接或目录联接,请选择普通目录');
if (stat.isFile() && stat.nlink > 1) throw new Error('目标文件包含硬链接,请先使用独立副本');
// Keep the common path at one lstat + native realpath, without caching a
// "safe" bit that could miss a junction swap between sibling writes.
if (normalize(fs.realpathSync.native(part)) !== normalize(part)) {
for (let ancestor = path.dirname(part); ; ancestor = path.dirname(ancestor)) {
if (fs.lstatSync(ancestor).isSymbolicLink()) throw new Error('目标路径包含符号链接或目录联接,请选择普通目录');
if (path.dirname(ancestor) === ancestor) break;
}
}
return;
}
}
DOS/UNC prefix stripping from `seat-transactions.js:11-17`:
function absolutePath(value) {
// DOS/UNC namespace prefixes change spelling, not directory identity. Strip
// only these known prefixes so ancestor traversal reaches the real drive or
// share root (path.dirname treats a namespaced UNC root differently).
return path.resolve(process.platform === 'win32'
? value.replace(/^\\\\\?\\UNC\\/i, '\\\\').replace(/^\\\\\?\\([a-z]:\\)/i, '$1') : value);
}
REA launcher's equivalent guard from `rea-launcher.cjs:42-54`:
function assertOrdinaryPath(file) {
const chain = [];
for (let current = path.resolve(file); ; current = path.dirname(current)) {
chain.push(current);
if (path.dirname(current) === current) break;
}
for (const current of chain.reverse()) {
const stat = lstatMaybe(current);
if (stat?.isSymbolicLink() || (stat?.isFile() && stat.nlink > 1)) {
throw new ReaError('REA_PATH_ESCAPE', 'REA 路径含符号链接、目录联接或硬链接;请使用独立的普通目录。');
}
}
}