fix(exec): revalidate working_dir before command start
This commit is contained in:
parent
1da0f2b555
commit
c94de4c893
1 changed files with 19 additions and 0 deletions
|
|
@ -219,6 +219,25 @@ func (t *ExecTool) Execute(ctx context.Context, args map[string]any) *ToolResult
|
||||||
return ErrorResult(guardError)
|
return ErrorResult(guardError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Re-resolve symlinks immediately before execution to shrink the TOCTOU window
|
||||||
|
// between validation and cmd.Dir assignment.
|
||||||
|
if t.restrictToWorkspace && t.workingDir != "" && cwd != t.workingDir {
|
||||||
|
resolved, err := filepath.EvalSymlinks(cwd)
|
||||||
|
if err != nil {
|
||||||
|
return ErrorResult(fmt.Sprintf("Command blocked by safety guard (path resolution failed: %v)", err))
|
||||||
|
}
|
||||||
|
absWorkspace, _ := filepath.Abs(t.workingDir)
|
||||||
|
wsResolved, _ := filepath.EvalSymlinks(absWorkspace)
|
||||||
|
if wsResolved == "" {
|
||||||
|
wsResolved = absWorkspace
|
||||||
|
}
|
||||||
|
rel, err := filepath.Rel(wsResolved, resolved)
|
||||||
|
if err != nil || !filepath.IsLocal(rel) {
|
||||||
|
return ErrorResult("Command blocked by safety guard (working directory escaped workspace)")
|
||||||
|
}
|
||||||
|
cwd = resolved
|
||||||
|
}
|
||||||
|
|
||||||
// timeout == 0 means no timeout
|
// timeout == 0 means no timeout
|
||||||
var cmdCtx context.Context
|
var cmdCtx context.Context
|
||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue