(relativeKey: string)
| 265 | * Throws PathTraversalError if the key is malicious (PSR M22186). |
| 266 | */ |
| 267 | export async function validateTeamMemKey(relativeKey: string): Promise<string> { |
| 268 | sanitizePathKey(relativeKey) |
| 269 | const teamDir = getTeamMemPath() |
| 270 | const fullPath = join(teamDir, relativeKey) |
| 271 | // First pass: normalize .. segments and check string-level containment. |
| 272 | const resolvedPath = resolve(fullPath) |
| 273 | if (!resolvedPath.startsWith(teamDir)) { |
| 274 | throw new PathTraversalError( |
| 275 | `Key escapes team memory directory: "${relativeKey}"`, |
| 276 | ) |
| 277 | } |
| 278 | // Second pass: resolve symlinks and verify real containment. |
| 279 | const realPath = await realpathDeepestExisting(resolvedPath) |
| 280 | if (!(await isRealPathWithinTeamDir(realPath))) { |
| 281 | throw new PathTraversalError( |
| 282 | `Key escapes team memory directory via symlink: "${relativeKey}"`, |
| 283 | ) |
| 284 | } |
| 285 | return resolvedPath |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Check if a file path is within the team memory directory |
no test coverage detected