isPathWithinAsync
isPathWithinAsync(
root,path,options?):Promise<boolean>
Defined in: packages/node/src/fs/path-within.ts:228
Whether path stays inside root once both are resolved through symbolic
links.
Resolves each with realpath, falling back to the longest existing prefix
when the path does not exist yet, then applies the segment comparison
isPathWithin describes. A link inside the root pointing out of it is
therefore reported as outside.
Resolving is not opening, and the gap between them is exploitable. A path
that answers true here can be replaced by a symbolic link before the caller
opens it — the check and the use are two syscalls, and nothing holds the
filesystem still between them. Where that matters, open the file first and
validate the handle (fd-relative operations, O_NOFOLLOW), or make the
root a directory an attacker cannot write to. This function narrows the
window; it does not close it.
Costs one realpath per path, plus one per missing ancestor when the path
does not exist.
Parameters
Section titled “Parameters”string
The directory the path must stay inside.
string
The path to check.
options?
Section titled “options?”Resolution base, root-equality and case-folding policy.
Returns
Section titled “Returns”Promise<boolean>
A promise for whether the real path is inside the real root.
Throws
Section titled “Throws”Error Any filesystem error other than a missing path — a check that could not look never answers “within”.