Skip to content

Commit 545bb2c

Browse files
committed
Add symlink check to readlinkSync
1 parent 9ced043 commit 545bb2c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/vfs/sync.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,18 @@ export function readlinkSync(
655655
path: fs.PathLike,
656656
options?: fs.EncodingOption | BufferEncoding | fs.BufferEncodingOption
657657
): Buffer | string {
658-
const value: Buffer = Buffer.from(_readFileSync.call(this, path, 'r', true));
658+
using handle = _openSync.call(this, normalizePath(path), { flag: 'r', mode: 0o644, preserveSymlinks: true });
659+
if (!isSymbolicLink(handle.inode)) throw new ErrnoError(Errno.EINVAL, 'Not a symbolic link: ' + path);
660+
const size = handle.inode.size;
661+
const data = Buffer.alloc(size);
662+
handle.read(data, 0, size, 0);
663+
659664
const encoding = typeof options == 'object' ? options?.encoding : options;
660665
if (encoding == 'buffer') {
661-
return value;
666+
return data;
662667
}
663668
// always defaults to utf-8 to avoid wrangler (cloudflare) worker "unknown encoding" exception
664-
return value.toString(encoding ?? 'utf-8');
669+
return data.toString(encoding ?? 'utf-8');
665670
}
666671
readlinkSync satisfies typeof fs.readlinkSync;
667672

0 commit comments

Comments
 (0)