mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
test(tools): add unit tests for scheme-less URL workspace guard detection
This commit is contained in:
@@ -1807,3 +1807,48 @@ func TestEncodeKeyTokenWithPtyKeyMode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestShellTool_SchemelessURLDetection verifies that the scheme-less URL
|
||||
// detection logic in guardCommand correctly identifies web URL path components
|
||||
// (e.g., "//github.com" captured by the regex after "https:") and exempts them
|
||||
// from workspace sandbox checks. It also confirms that paths NOT preceded by a
|
||||
// recognized web scheme are still blocked.
|
||||
func TestShellTool_SchemelessURLDetection(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
tool, err := NewExecTool(tmpDir, true)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to configure exec tool: %s", err)
|
||||
}
|
||||
|
||||
// Each of the 7 recognized web schemes should have its path component
|
||||
// exempted from workspace boundary checks.
|
||||
allowedCommands := []string{
|
||||
"echo https://github.com",
|
||||
"echo http://example.com",
|
||||
"echo ftp://ftp.example.com",
|
||||
"echo ftps://secure.example.com",
|
||||
"echo sftp://sftp.example.com",
|
||||
"echo ssh://git@github.com",
|
||||
"echo git://github.com",
|
||||
}
|
||||
|
||||
for _, cmd := range allowedCommands {
|
||||
result := tool.Execute(context.Background(), map[string]any{"action": "run", "command": cmd})
|
||||
if result.IsError && strings.Contains(result.ForLLM, "path outside working dir") {
|
||||
t.Errorf("command with recognized web scheme should not be blocked: %s\n error: %s", cmd, result.ForLLM)
|
||||
}
|
||||
}
|
||||
|
||||
// Multiple URLs with different schemes in a single command should all be exempt.
|
||||
multiURLCommands := []string{
|
||||
"echo https://github.com && curl http://example.com",
|
||||
"wget ftp://a.com; curl https://b.com",
|
||||
}
|
||||
|
||||
for _, cmd := range multiURLCommands {
|
||||
result := tool.Execute(context.Background(), map[string]any{"action": "run", "command": cmd})
|
||||
if result.IsError && strings.Contains(result.ForLLM, "path outside working dir") {
|
||||
t.Errorf("command with multiple web URLs should not be blocked: %s\n error: %s", cmd, result.ForLLM)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user