1134344433 / src /lib /security.js
Twan07's picture
Upload 22 files
5858652 verified
raw
history blame contribute delete
368 Bytes
import path from "node:path";
export function safeResolve(rootDir, relativePath = "") {
const resolvedPath = path.resolve(rootDir, relativePath || ".");
const normalizedRoot = path.resolve(rootDir);
if (resolvedPath !== normalizedRoot && !resolvedPath.startsWith(normalizedRoot + path.sep)) {
throw new Error("Invalid path");
}
return resolvedPath;
}