fix: prevent safety guard from blocking commands containing URLs

The absolutePathPattern regex in the workspace safety check was matching
URL path components (e.g., //github.com) as absolute file paths, causing
commands like "agent-browser open https://github.com" to be incorrectly
blocked.

This fix:
- Adds a urlPattern to match HTTP(S) URLs
- Strips URLs from commands before path safety checks
- Preserves all existing safety checks for actual file paths

Fixes #1203

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hobostay 2026-03-12 16:43:54 +08:00
parent 6460a0a7c7
commit 5f69bc7c6c
No known key found for this signature in database

View file

@ -81,6 +81,9 @@ var (
// absolutePathPattern matches absolute file paths in commands (Unix and Windows).
absolutePathPattern = regexp.MustCompile(`[A-Za-z]:\\[^\\\"']+|/[^\s\"']+`)
// urlPattern matches HTTP(S) URLs to exclude them from path safety checks.
urlPattern = regexp.MustCompile(`https?://[^\s\"']+`)
// safePaths are kernel pseudo-devices that are always safe to reference in
// commands, regardless of workspace restriction. They contain no user data
// and cannot cause destructive writes.
@ -373,7 +376,11 @@ func (t *ExecTool) guardCommand(command, cwd string) string {
return ""
}
matches := absolutePathPattern.FindAllString(cmd, -1)
// Remove URLs from the command before matching paths to avoid false positives.
// For example, "agent-browser open https://github.com" should not trigger
// a path safety check on the URL's path component.
cmdWithoutURLs := urlPattern.ReplaceAllString(cmd, "")
matches := absolutePathPattern.FindAllString(cmdWithoutURLs, -1)
for _, raw := range matches {
p, err := filepath.Abs(raw)