Files
sam 2928bdb682
publish / publish (push) Successful in 1m6s
fix(host): confine lookups to each host's configured path roots
Lookups run no guards and the helpers read as root, so an instruction injected
into an event could steer the agent into listing anywhere on the host. Each
host now names the roots its lookups may look under, and any other path is
refused before a command is built.
2026-11-16 00:42:48 +11:00

53 lines
1.7 KiB
Go

package main
import (
"os/exec"
"testing"
)
// TestLookupInputCannotEscapeItsArgument guards the host connector's boundary
// with the remote host: ssh hands commands to a shell, so a path from the
// model must reach the command as exactly one argument, whatever it contains.
// A path that isn't absolute, carries control characters, or lies outside the
// host's lookup roots (where an injected instruction would steer it) is
// refused before any command is built.
func TestLookupInputCannotEscapeItsArgument(t *testing.T) {
tests := []struct {
name string
path string
refused bool
}{
{name: "plain path", path: "/tmp/runner"},
{name: "spaces", path: "/tmp/my dir"},
{name: "single quote breakout", path: "/tmp/x'; printf INJECTED; printf '"},
{name: "command substitution", path: "/tmp/$(printf INJECTED)`printf INJECTED`"},
{name: "option lookalike", path: "/tmp/-rf"},
{name: "relative path", path: "tmp", refused: true},
{name: "newline", path: "/tmp\n; id", refused: true},
{name: "outside the roots", path: "/root", refused: true},
{name: "climbing out of a root", path: "/tmp/../etc/shadow", refused: true},
{name: "a root's name as a prefix", path: "/tmpfoo", refused: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p, err := checkPath(tt.path, []string{"/tmp", "/var/tmp"})
if tt.refused {
if err == nil {
t.Fatalf("checkPath(%q) accepted it", tt.path)
}
return
}
if err != nil {
t.Fatal(err)
}
out, err := exec.CommandContext(t.Context(), "sh", "-c", "printf '%s' "+quote(p)).Output()
if err != nil {
t.Fatal(err)
}
if string(out) != p {
t.Fatalf("the shell saw %q, want the single argument %q", out, p)
}
})
}
}