Skip to content

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.

string

The directory the path must stay inside.

string

The path to check.

PathWithinOptions

Resolution base, root-equality and case-folding policy.

Promise<boolean>

A promise for whether the real path is inside the real root.

Error Any filesystem error other than a missing path — a check that could not look never answers “within”.