The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import path from "pathe"; |
| 2 | |
| 3 | /** |
| 4 | * Test lexical containment, including equality, using an explicit absolute base |
| 5 | * for relative paths. Does not follow symlinks or consult the working directory. |
| 6 | */ |
| 7 | export const isPathWithin = (directory: string, candidate: string, base: string): boolean => { |
| 8 | const relative = path.relative(path.resolve(base, directory), path.resolve(base, candidate)); |
| 9 | return ( |
| 10 | relative === "" || |
| 11 | (!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith("../")) |
| 12 | ); |
| 13 | }; |