mirror of
https://github.com/lukaszraczylo/filepuff-mcp.git
synced 2026-10-12 04:54:27 +00:00
fix(edit): re-validate path immediately before write (TOCTOU hardening)
edit_apply validated the target with IsPathAllowed in the handler, then later wrote it via os.WriteFile on the raw path after reading+parsing+diffing. A symlink swapped in during that window could redirect the write outside the workspace root. Inject the admission check into the edit Engine (NewEngineWithValidator) and re-run it inside a new writeValidatedFile helper immediately before the write — IsPathAllowed resolves symlinks at write time, so a swapped-in link now resolving outside the root is rejected. This narrows the TOCTOU window to near-zero while still permitting legitimate in-workspace symlink edits (unlike a blanket O_NOFOLLOW). writeValidatedFile also dedups the perm-preserving write shared by AST and text modes. Tests: rejected-path edit leaves the file untouched; file mode is preserved.
This commit is contained in:
@@ -54,7 +54,7 @@ func New(cfg *config.Config, logger *slog.Logger) (*Server, error) {
|
||||
logger: logger,
|
||||
parser: parserRegistry,
|
||||
matcher: query.NewMatcher(parserRegistry),
|
||||
editor: edit.NewEngine(parserRegistry),
|
||||
editor: edit.NewEngineWithValidator(parserRegistry, cfg.IsPathAllowed),
|
||||
readSem: make(chan struct{}, MaxConcurrentReads),
|
||||
querySem: make(chan struct{}, MaxConcurrentQueries),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user