From fb17b55dd237fa0a30bf08bbaa26ebc1c74542ec Mon Sep 17 00:00:00 2001 From: QuietyAwe <90510260+QuietyAwe@users.noreply.github.com> Date: Sat, 28 Feb 2026 19:11:26 +0800 Subject: [PATCH] feat(tools): apply configurable timeout in ExecTool --- pkg/tools/shell.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkg/tools/shell.go b/pkg/tools/shell.go index ad1664b5b..8b961488f 100644 --- a/pkg/tools/shell.go +++ b/pkg/tools/shell.go @@ -69,15 +69,16 @@ var defaultDenyPatterns = []*regexp.Regexp{ regexp.MustCompile(`\bsource\s+.*\.sh\b`), } -func NewExecTool(workingDir string, restrict bool) *ExecTool { +func NewExecTool(workingDir string, restrict bool) (*ExecTool, error) { return NewExecToolWithConfig(workingDir, restrict, nil) } -func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Config) *ExecTool { +func NewExecToolWithConfig(workingDir string, restrict bool, cfg *config.Config) (*ExecTool, error) { denyPatterns := make([]*regexp.Regexp, 0) + timeout := 60 * time.Second // default timeout - if config != nil { - execConfig := config.Tools.Exec + if cfg != nil { + execConfig := cfg.Tools.Exec enableDenyPatterns := execConfig.EnableDenyPatterns if enableDenyPatterns { denyPatterns = append(denyPatterns, defaultDenyPatterns...) @@ -86,8 +87,7 @@ func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Conf for _, pattern := range execConfig.CustomDenyPatterns { re, err := regexp.Compile(pattern) if err != nil { - fmt.Printf("Invalid custom deny pattern %q: %v\n", pattern, err) - continue + return nil, fmt.Errorf("invalid custom deny pattern %q: %w", pattern, err) } denyPatterns = append(denyPatterns, re) } @@ -96,17 +96,22 @@ func NewExecToolWithConfig(workingDir string, restrict bool, config *config.Conf // If deny patterns are disabled, we won't add any patterns, allowing all commands. fmt.Println("Warning: deny patterns are disabled. All commands will be allowed.") } + + // Apply configured timeout if set (0 means use default) + if execConfig.TimeoutSeconds > 0 { + timeout = time.Duration(execConfig.TimeoutSeconds) * time.Second + } } else { denyPatterns = append(denyPatterns, defaultDenyPatterns...) } return &ExecTool{ workingDir: workingDir, - timeout: 60 * time.Second, + timeout: timeout, denyPatterns: denyPatterns, allowPatterns: nil, restrictToWorkspace: restrict, - } + }, nil } func (t *ExecTool) Name() string { @@ -329,4 +334,4 @@ func (t *ExecTool) SetAllowPatterns(patterns []string) error { t.allowPatterns = append(t.allowPatterns, re) } return nil -} +} \ No newline at end of file