( path: string, label = "File path", )
| 64 | } |
| 65 | |
| 66 | export function normalizeGeneratedFilePath( |
| 67 | path: string, |
| 68 | label = "File path", |
| 69 | ): string { |
| 70 | // Treat a backslash as a path separator BEFORE the per-segment "." / ".." |
| 71 | // rejection below. Obsidian's own `normalizePath` converts "\\" -> "/" before |
| 72 | // any path is touched on disk, so a formatted name like "..\\..\\..\\evil" |
| 73 | // would otherwise survive this guard as ONE non-".." segment yet still be |
| 74 | // written by `vault.create` as the traversal "../../../evil" — an out-of-vault |
| 75 | // write. Converting here makes the guard see the real segments (so "..\\" is |
| 76 | // rejected) and keeps this normalizer's view of the path identical to what |
| 77 | // Obsidian writes. The vault-containment assertion at the create sink |
| 78 | // (createFileWithInput -> escapesVaultBoundary) is the authoritative boundary |
| 79 | // for absolute / drive / UNC paths. |
| 80 | const segments = path.replace(/\\/g, "/").split("/"); |
| 81 | return segments |
| 82 | .map((segment, index) => { |
| 83 | if ( |
| 84 | segment.length === 0 |
| 85 | && index !== 0 |
| 86 | && index !== segments.length - 1 |
| 87 | ) { |
| 88 | throw new Error( |
| 89 | `${label} contains an empty path segment after formatting.`, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | return normalizeGeneratedFilePathSegment(segment, label); |
| 94 | }) |
| 95 | .join("/"); |
| 96 | } |
| 97 | |
| 98 | function normalizeGeneratedFilePathSegment( |
| 99 | segment: string, |
no test coverage detected