* Strip the workingDir prefix from an absolute path to produce a relative * path for the readFile callback. If the path is outside the workingDir, * returns `null` to signal that the file should not be accessible.
(path: string, workingDir?: string)
| 107 | * returns `null` to signal that the file should not be accessible. |
| 108 | */ |
| 109 | function stripWorkingDir(path: string, workingDir?: string): string | null { |
| 110 | // Normalize first to resolve `.` and `..` segments (e.g. /foo/../a.txt -> /a.txt) |
| 111 | const normalized = normalize(path); |
| 112 | if (!workingDir) { |
| 113 | return normalized; |
| 114 | } |
| 115 | const normalizedDir = normalize(workingDir); |
| 116 | const prefix = normalizedDir.endsWith('/') |
| 117 | ? normalizedDir |
| 118 | : normalizedDir + '/'; |
| 119 | if (normalized.startsWith(prefix)) { |
| 120 | return normalized.slice(prefix.length); |
| 121 | } |
| 122 | // Handle exact match (path is the workingDir itself) |
| 123 | if (normalized === normalizedDir) { |
| 124 | return ''; |
| 125 | } |
| 126 | // Path is outside workingDir -- do not expose it to the callback. |
| 127 | return null; |
| 128 | } |