MCPcopy Create free account
hub / github.com/chhoumann/quickadd / normalizeGeneratedFilePath

Function normalizeGeneratedFilePath

src/utils/generatedFilePath.ts:66–96  ·  view source on GitHub ↗
(
	path: string,
	label = "File path",
)

Source from the content-addressed store, hash-verified

64}
65
66export 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
98function normalizeGeneratedFilePathSegment(
99 segment: string,

Callers 10

captureTargetExistsMethod · 0.90
runMethod · 0.90
resolveCaptureTargetFunction · 0.90

Calls 1

Tested by

no test coverage detected