File size: 368 Bytes
5858652
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}