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) } }) } }