fix(filesystem): enforce symlink and decoded-text admission

This commit is contained in:
morluto
2026-10-13 13:39:57 +08:00
parent 2187e16f14
commit ec0b081371
4 changed files with 127 additions and 9 deletions
+11 -1
View File
@@ -1,6 +1,6 @@
import { constants } from "node:fs";
import type { BigIntStats, Stats } from "node:fs";
import { open, type FileHandle } from "node:fs/promises";
import { lstat, open, type FileHandle } from "node:fs/promises";
import { OwnedFileHandle } from "./OwnedFileHandle.js";
@@ -74,6 +74,16 @@ export const openRegularFile = async (
options.signal?.throwIfAborted();
if (!metadata.isFile())
throw new NonRegularFileReadError(path, metadata.isDirectory());
// O_NOFOLLOW is unavailable on Windows. Recheck the name only on hosts
// where open could have followed a symlink, while the descriptor is owned.
if (options.symlinks === "reject" && (constants.O_NOFOLLOW ?? 0) === 0) {
const named = await lstat(path);
options.signal?.throwIfAborted();
if (named.isSymbolicLink())
throw new NonRegularFileReadError(path, false);
if (!sameRegularFileState(metadata, named))
throw new RegularFileChangedError(path);
}
return handle;
} catch (cause: unknown) {
try {