( skillDir: string, filePath: string )
| 28 | } |
| 29 | |
| 30 | function resolveSkillFilePath( |
| 31 | skillDir: string, |
| 32 | filePath: string |
| 33 | ): { |
| 34 | resolvedPath: string; |
| 35 | normalizedRelativePath: string; |
| 36 | } { |
| 37 | if (!filePath) { |
| 38 | throw new Error("filePath is required"); |
| 39 | } |
| 40 | |
| 41 | if (isAbsolutePathAny(filePath) || filePath.startsWith("~")) { |
| 42 | throw new Error(`Invalid filePath (must be relative to the skill directory): ${filePath}`); |
| 43 | } |
| 44 | |
| 45 | if (filePath.startsWith("..")) { |
| 46 | throw new Error(`Invalid filePath (path traversal): ${filePath}`); |
| 47 | } |
| 48 | |
| 49 | const resolvedPath = path.resolve(skillDir, filePath); |
| 50 | const relativePath = path.relative(skillDir, resolvedPath); |
| 51 | |
| 52 | if (relativePath === "" || relativePath === ".") { |
| 53 | throw new Error(`Invalid filePath (expected a file, got directory): ${filePath}`); |
| 54 | } |
| 55 | |
| 56 | if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) { |
| 57 | throw new Error(`Invalid filePath (path traversal): ${filePath}`); |
| 58 | } |
| 59 | |
| 60 | return { |
| 61 | resolvedPath, |
| 62 | normalizedRelativePath: relativePath.replaceAll(path.sep, "/"), |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | async function lstatIfExists(targetPath: string): Promise<Stats | null> { |
| 67 | try { |
no test coverage detected