fix(host): confine lookups to each host's configured path roots
publish / publish (push) Successful in 1m6s

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.
This commit is contained in:
sam committed 2026-11-16 00:42:48 +11:00
1 parent d74535c0d3
commit 2928bdb682
3 files changed
+34 -10

No files matched your search

+21 -4
View File
@@ -26,6 +26,10 @@ type Target struct {
// Helpers runs the lookups that walk directories through the root-owned
// helpers in target/, so they see what the account alone can't read.
Helpers bool `json:"helpers"`
// Paths are the roots lookups may look under. Lookups run no guards and
// may read as root, so a path outside them is refused: an instruction
// injected into an event can't steer the agent into the rest of the host.
Paths []string `json:"paths"`
key ssh.PublicKey
}
@@ -48,6 +52,12 @@ func newHost(cfg connector.Config) (*host, error) {
return nil, fmt.Errorf("host %q: host_key: %w", t.Name, err)
}
t.key = key
for i, root := range t.Paths {
if !path.IsAbs(root) {
return nil, fmt.Errorf("host %q: path root %q must be absolute", t.Name, root)
}
t.Paths[i] = path.Clean(root)
}
h.targets[t.Name] = t
}
pem, pass := cfg.Secrets["ssh.private_key"], cfg.Secrets["ssh.private_key_passphrase"]
@@ -102,7 +112,7 @@ func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jso
if !ok {
return nil, false, fmt.Errorf("host %q isn't configured", in.Host)
}
p, err := checkPath(in.Path)
p, err := checkPath(in.Path, target.Paths)
if err != nil {
return nil, false, err
}
@@ -161,15 +171,22 @@ func lookupCommand(name, p string, limit int, olderThan *int, helpers bool) (str
// helperDir is where target/install.sh puts the helpers, run through sudo.
const helperDir = "sudo -n /usr/local/libexec/archie/"
// checkPath accepts an absolute path with no control characters, cleaned.
func checkPath(p string) (string, error) {
// checkPath accepts an absolute path with no control characters that, once
// cleaned, is one of roots or lies under one.
func checkPath(p string, roots []string) (string, error) {
if !path.IsAbs(p) {
return "", fmt.Errorf("path %q must be absolute", p)
}
if strings.ContainsFunc(p, func(r rune) bool { return r < 0x20 || r == 0x7f }) {
return "", errors.New("path contains control characters")
}
return path.Clean(p), nil
clean := path.Clean(p)
for _, root := range roots {
if clean == root || strings.HasPrefix(clean, strings.TrimSuffix(root, "/")+"/") {
return clean, nil
}
}
return "", fmt.Errorf("path %q is outside this host's lookup paths %v", clean, roots)
}
// quote makes s one single-quoted shell word.
+10 -6
View File
@@ -5,10 +5,11 @@ import (
"testing"
)
// TestLookupInputCannotEscapeItsArgument guards the host connector's only
// boundary with the remote shell: ssh hands commands to a shell, so a path
// from the model must reach the command as exactly one argument, whatever it
// contains, and a path that isn't absolute or carries control characters is
// 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 {
@@ -20,13 +21,16 @@ func TestLookupInputCannotEscapeItsArgument(t *testing.T) {
{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: "/-rf"},
{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)
p, err := checkPath(tt.path, []string{"/tmp", "/var/tmp"})
if tt.refused {
if err == nil {
t.Fatalf("checkPath(%q) accepted it", tt.path)
+3
View File
@@ -10,3 +10,6 @@
and run fixed commands: sudo can't safely allow `du` or `find` themselves,
since `find` can delete and execute.
- The host's public key, as `ssh-keyscan` prints it, in the `hosts` setting.
- The roots lookups may look under, as `paths` in the `hosts` setting (for
example `["/tmp", "/var/tmp"]`). Lookups run no guards and may read as root,
so any path outside them is refused.