MCPcopy
hub / github.com/coder/mux / assertContained

Method assertContained

src/node/services/memoryService.ts:387–417  ·  view source on GitHub ↗
(relPath: string)

Source from the content-addressed store, hash-verified

385 }
386
387 async assertContained(relPath: string): Promise<void> {
388 let realRoot: string;
389 try {
390 realRoot = await fsPromises.realpath(this.physicalRoot);
391 } catch {
392 // Missing root (read paths never create it): nothing exists under a
393 // nonexistent root, so there is nothing to escape — lookups simply
394 // report "not found". Write paths ensureRoot first, so they get here
395 // only with an existing root.
396 return;
397 }
398 // Walk up from the target to the deepest existing ancestor, then realpath it.
399 let candidate = this.abs(relPath);
400 for (;;) {
401 try {
402 const real = await fsPromises.realpath(candidate);
403 if (!isPathWithinRoot(realRoot, real, path)) {
404 throw new MemoryCommandError(
405 `Path escapes the memory root (symlinks are not allowed to point outside)`
406 );
407 }
408 return;
409 } catch (error) {
410 if (error instanceof MemoryCommandError) throw error;
411 const parent = path.dirname(candidate);
412 // The root exists (realpath above succeeded), so the walk terminates at it.
413 assert(parent !== candidate, "containment walk must terminate at the memory root");
414 candidate = parent;
415 }
416 }
417 }
418}
419
420// ---------------------------------------------------------------------------

Callers

nothing calls this directly

Calls 3

absMethod · 0.95
isPathWithinRootFunction · 0.85
assertFunction · 0.50

Tested by

no test coverage detected