(target: string, allowedRoots: Set<string>)
| 52 | } |
| 53 | |
| 54 | export function isFilePathAllowed(target: string, allowedRoots: Set<string>): boolean { |
| 55 | for (const root of allowedRoots) { |
| 56 | const useWindowsRules = isWindowsAbsolutePath(target) || isWindowsAbsolutePath(root); |
| 57 | const resolver = useWindowsRules ? path.win32 : path; |
| 58 | const sep = useWindowsRules ? "\\" : path.sep; |
| 59 | const normalized = resolver.resolve(target); |
| 60 | const normalizedRoot = resolver.resolve(root); |
| 61 | const comparable = useWindowsRules ? normalized.toLowerCase() : normalized; |
| 62 | const comparableRoot = useWindowsRules ? normalizedRoot.toLowerCase() : normalizedRoot; |
| 63 | const rootWithSep = comparableRoot.endsWith(sep) ? comparableRoot : comparableRoot + sep; |
| 64 | if (comparable === comparableRoot || comparable.startsWith(rootWithSep)) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | return false; |
| 69 | } |
no test coverage detected