File size: 377 Bytes
f02fdcc
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
import path from "path";

export function safeResolve(basePath: string, filePath: string): string {
  const resolvedBase = path.resolve(basePath);
  const resolvedTarget = path.resolve(resolvedBase, filePath);
  if (resolvedTarget === resolvedBase || resolvedTarget.startsWith(resolvedBase + path.sep)) {
    return resolvedTarget;
  }
  throw new Error("Invalid file path");
}