MCPcopy
hub / github.com/21st-dev/1code / delete

Function delete

src/main/lib/git/security/secure-fs.ts:352–389  ·  view source on GitHub ↗

* Delete a file or directory within a worktree. * * SECURITY: Validates the real path is within worktree before deletion. * - Symlinks: Deletes the link itself (safe - link lives in worktree) * - Files/dirs: Validates realpath then deletes * * This prevents symlink escape attacks where a

(worktreePath: string, filePath: string)

Source from the content-addressed store, hash-verified

350 * `/Users/victim/file`.
351 */
352 async delete(worktreePath: string, filePath: string): Promise<void> {
353 assertRegisteredWorktree(worktreePath);
354 // allowRoot: false prevents deleting the worktree itself
355 const fullPath = resolvePathInWorktree(worktreePath, filePath, {
356 allowRoot: false,
357 });
358
359 let stats: Stats;
360 try {
361 stats = await lstat(fullPath);
362 } catch (error) {
363 // File doesn't exist - idempotent delete, nothing to do
364 if (
365 error instanceof Error &&
366 "code" in error &&
367 error.code === "ENOENT"
368 ) {
369 return;
370 }
371 throw error;
372 }
373
374 if (stats.isSymbolicLink()) {
375 // Symlink - safe to delete the link itself (it lives in the worktree).
376 // Don't use recursive as we're just removing the symlink file.
377 await rm(fullPath);
378 return;
379 }
380
381 // Regular file or directory - validate realpath is within worktree.
382 // This catches path traversal via symlinked parent components:
383 // e.g., `docs -> /victim`, delete `docs/file` → realpath is `/victim/file`
384 await assertRealpathInWorktree(worktreePath, fullPath);
385
386 // Safe to delete - realpath confirmed within worktree.
387 // Note: Symlinks INSIDE a directory are safe - rm deletes the links, not targets.
388 await rm(fullPath, { recursive: true, force: true });
389 },
390
391 /**
392 * Get file stats within a worktree.

Callers

nothing calls this directly

Calls 4

assertRegisteredWorktreeFunction · 0.90
resolvePathInWorktreeFunction · 0.90
lstatFunction · 0.85
assertRealpathInWorktreeFunction · 0.85

Tested by

no test coverage detected