MCPcopy Create free account
hub / github.com/Noumena-Network/code / buildDownloadPath

Function buildDownloadPath

src/services/api/filesApi.ts:188–211  ·  view source on GitHub ↗
(
  basePath: string,
  sessionId: string,
  relativePath: string,
)

Source from the content-addressed store, hash-verified

186 * Returns null if the path is invalid (e.g., path traversal).
187 */
188export function buildDownloadPath(
189 basePath: string,
190 sessionId: string,
191 relativePath: string,
192): string | null {
193 const normalized = path.normalize(relativePath)
194 if (normalized.startsWith('..')) {
195 logDebugError(
196 `Invalid file path: ${relativePath}. Path must not traverse above workspace`,
197 )
198 return null
199 }
200
201 const uploadsBase = path.join(basePath, sessionId, 'uploads')
202 const redundantPrefixes = [
203 path.join(basePath, sessionId, 'uploads') + path.sep,
204 path.sep + 'uploads' + path.sep,
205 ]
206 const matchedPrefix = redundantPrefixes.find(p => normalized.startsWith(p))
207 const cleanPath = matchedPrefix
208 ? normalized.slice(matchedPrefix.length)
209 : normalized
210 return path.join(uploadsBase, cleanPath)
211}
212
213/**
214 * Downloads a file and saves it to the session-specific workspace directory

Callers 2

filesApi.test.tsFile · 0.85
downloadAndSaveFileFunction · 0.85

Calls 1

logDebugErrorFunction · 0.85

Tested by

no test coverage detected