Files
Pulse/internal/agentexec/policy_args_test.go
rcourtman eda6b86f6b Harden AI read-only tool sandboxing and query limits
Close read-only AI tool escapes, device-open hazards, DNS exfiltration, and unbounded allocation:

- Deny lsblk --sysroot in the shared agentexec policy and restrict lsblk, lscpu, and lsmem in the pulse_read classifier to allowlisted options.
- Confine auto-approved df, fdisk -l, and file -s operands to explicit directories or kernel block device names so character devices like /dev/watchdog cannot be opened without approval, and add a targeted df refusal hint.
- Fix BSD/macOS ps environment option parsing in psPrintsEnvironment so dash-prefixed options are never skipped as values and dashless clusters reject uppercase E.
- Turn ssFilterNamesHost into an allowlist over keywords, literals, and comparison values, drop -F/--filter, and validate prefix lengths in isLiteralNetworkHost to block DNS exfiltration.
- Stop preallocating the pulse_query search result slice by the caller-supplied limit so huge limits cannot trigger an unrecoverable out-of-memory crash.

Contract-Neutral: Security hardening of AI tool sandboxing, agentexec sessions, and Patrol/Assistant authorization without public contract delta
2026-11-24 12:28:48 +00:00

471 lines
15 KiB
Go

package agentexec
import "testing"
// Display forms of the auto-approved programs keep running without approval.
func TestDefaultPolicyAutoApprovesDisplayForms(t *testing.T) {
policy := DefaultPolicy()
for _, command := range []string{
// ip and ifconfig
"ip addr",
"ip address show",
"ip addr show dev eth0",
"ip route",
"ip route show table all",
"ip route list",
"ip route get 192.0.2.1",
"ip link",
"ip link show eth0",
"ifconfig",
"ifconfig -a",
"ifconfig eth0",
"ifconfig -a eth0",
// find
"find / -type f -size +100M",
"find /var -type f -mtime +30 -name '*.log'",
"find /var/lib -type d -mtime -1 ! -path '/var/lib/docker/*'",
"find /var -type f -size +10M \\( -name '*.gz' -o -name '*.tar' \\) -printf '%s %p\\n'",
"find /home -xdev -type f -size +1G -ls",
// du, file, last, blkid, lsblk
"du -sh /var/lib/docker",
"du -xh -d 3 /mnt/nvr",
"du -sh --exclude=*.iso /srv",
"du -sh ~",
"file /usr/bin/ls",
"file -bi /var/log/syslog",
"file -s /dev/sda1",
"file -sL /dev/nvme0n1p2",
"file --special-files /dev/dm-0 /dev/md0",
"last -n 20",
"last -F reboot",
"blkid",
"blkid -o value -s UUID /dev/sda1",
"blkid -p /dev/sda1",
"blkid -c /dev/null -o export",
"blkid --cache-file=/dev/null",
"lsblk -o NAME,SIZE,TYPE",
"lsblk -J -o NAME,SERIAL /dev/sd*",
"lsblk --sort SIZE --scsi",
// df of every filesystem, of directories and of block devices
"df",
"df -h",
"df -hT -x tmpfs -x devtmpfs",
"df -h --type=ext4 --output=source,pcent",
"df -hT /",
"df -h /var/lib/docker/ /mnt/nvr/.",
"df -i .",
"df -h -t ext4 /srv/",
"df -hB M --block-size 1K -- /var/log/",
"df -h ~/",
"df -h /dev/sda1 /dev/nvme0n1p2",
"sudo df -h /",
"fdisk -l",
"fdisk -lu /dev/sda /dev/nvme0n1",
// ss, lsof, netstat
"ss -tulpn",
"ss -tan state established '( dport = :443 )'",
"ss -s",
"ss -tn --family=inet",
"lsof -nP -iTCP -sTCP:LISTEN",
"lsof +D /var/log",
"lsof +L1",
"lsof -p 1234",
// journalctl and systemctl
"journalctl -u nginx -n 100 --no-pager",
"journalctl -xeu nginx",
"journalctl --since \"1 hour ago\" -p err",
"journalctl --since=\"1 hour ago\" --until=now",
"journalctl -b -1 -n 50",
"journalctl --disk-usage",
"journalctl --cursor=s=abc -n 10",
"journalctl --verify",
"journalctl -k --reverse",
"systemctl status nginx --no-pager",
"systemctl status nginx -n 50",
"systemctl is-active nginx",
"systemctl list-units --failed",
"service nginx status",
// sar, ps, apt-cache, dpkg, pvesh
"sar -u 1 3",
"sar -n DEV 1 1",
"sar -A",
"sar -f /var/log/sa/sa12 -u",
"sar -f /var/log/sysstat/sa12 -r",
"ps aux",
"ps -ef",
"ps auxf",
"ps -eo pid,user,%cpu,%mem,etime,cmd --sort=-%cpu",
"ps -p 1234 -o pid,etime,cmd",
"ps axo pid,user,comm",
"ps --format pid,user,etime",
"ps ax --sort etime",
"ps -aux",
"ps -eLf",
"ps -efww",
"ps -ejH",
"ps -ef -p1234",
"ps -eo pid,cmd -p1234,5678",
"apt-cache policy nginx",
"apt-cache show nginx",
"dpkg -l",
"dpkg -l nginx 'linux-image*'",
"dpkg --list",
"dpkg -L nginx",
"pvesh get /nodes --output-format json",
"pvesh get /nodes/pve/qemu/100/agent/get-osinfo",
"pvesh get /version",
"pvesh get nodes/pve/lxc/101/status/current",
"pvesh get /nodes/pve/tasks --limit 20 --errors 1",
"pvesh get /nodes/pve/disks/smart --disk /dev/sda",
"pvesh get '/nodes/pve/storage/local/status' --output-format json",
"pvesh get /storage/local --output-format json",
"pvesh get /pools",
"pvesh get /cluster/status",
"pvesh get /cluster/resources --type vm",
"pvesh get //cluster//ha/status/current/",
"sudo pvesh get /cluster/backup",
// Anchored patterns still match their commands.
"docker ps -a",
"docker images",
"docker network ls",
"docker stats --no-stream",
"docker ps --filter name=web --format 'table {{.Names}}\t{{.Status}}'",
"docker ps --format '{{json .}}'",
`docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"`,
`docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"`,
"docker logs --tail 100 -f web",
"docker inspect web",
"docker inspect -f '{{.State.Status}}' web",
"docker inspect --format='{{ .Name }} {{ .Config.Image }} {{ json .Config.Cmd }}' web",
"docker inspect --format \"{{json .HostConfig.RestartPolicy}}\" web",
"docker inspect -f '{{json .Config.Env}}' web",
"sudo docker inspect -f '{{.NetworkSettings.IPAddress}}' web",
"podman inspect -f '{{.State.Status}}' web",
"podman inspect --format '{{json .Config.Env}}' web",
"podman ps",
"podman ps -a --format '{{.Names}} {{.Status}}'",
"podman logs --tail 100 -t web",
"podman inspect --type container web",
"podman images -a",
"vmstat 1 5",
"iostat -x",
"mpstat -P ALL",
"sudo ss -tulpn",
} {
if got := policy.Evaluate(command); got != PolicyAllow {
t.Errorf("Evaluate(%q) = %q, want %q", command, got, PolicyAllow)
}
}
}
// Forms that change the system, write files, read another file's contents or
// hide their arguments need approval.
func TestDefaultPolicyRequiresApprovalForMutatingForms(t *testing.T) {
policy := DefaultPolicy()
for _, command := range []string{
// ip and ifconfig mutations
"ip link set eth0 down",
"ip link s eth0 down",
"ip link del br0",
"ip link add dummy0 type dummy",
"ip route del default",
"ip route add 10.0.0.0/8 via 192.0.2.1",
"ip route flush cache",
"ip route replace default via 192.0.2.1",
"ip route save",
"ip addr add 192.0.2.10/24 dev eth0",
"ip addr del 192.0.2.10/24 dev eth0",
"ip addr flush dev eth0",
"ip addrlabel add prefix ::/0 label 1",
"ip link show eth0 -batch /tmp/x",
"ip addr show; ip link set eth0 down",
"ifconfig eth0 down",
"ifconfig eth0 up",
"ifconfig eth0 192.0.2.10 netmask 255.255.255.0",
"ifconfig eth0 mtu 1400",
"ifconfigx",
// find actions and file output
"find / -type f -delete",
"find / -type f '-delete'",
"find / -type f -del''ete",
"find / -type f -del\\ete",
"find / -type f -DELETE",
"find / -type f -exec rm {} +",
"find / -type f -execdir id {} +",
"find / -type f -ok rm {} +",
"find / -type f -fprint /etc/cron.d/x",
"find / -type f -fprint0 /tmp/x",
"find / -type f -fprintf /tmp/x '%p'",
"find / -type f -fls /tmp/x",
"find / -type f -files0-from /etc/shadow",
"find / -type f ${x:--delete}",
"find / -type f $'-delete'",
"find / -type f {-delete,}",
"find / -type f -*",
"find / -type f *",
"find / -type f -delet?",
"du -s* /var",
// du, file, last, blkid
"du --files0-from=/etc/shadow",
"du --files0-from /etc/shadow",
"du --files0=/etc/shadow",
"du -X /etc/shadow /var",
"du -shX/etc/shadow /var",
"du --exclude-from=/etc/shadow /var",
"file -f /etc/shadow",
"file -bf /etc/shadow",
"file --files-from /etc/shadow",
"file -m /etc/shadow /bin/ls",
"file --magic-file=/etc/shadow /bin/ls",
"file --mag=/etc/shadow /bin/ls",
"file -C -m /etc/shadow",
// file -s opens and reads character devices and /proc files
"file -s /dev/watchdog",
"file -sL /dev/ttyS0",
"file -bs /proc/kmsg",
"file --special-files /dev/st0",
"file --spec /dev/ttyS0",
"file /dev/sda1 -s /dev/watchdog",
"file -s -- /dev/watchdog",
"file -s /dev/nvme0",
"file -s /dev/sd*",
"file -s /dev/sda1/../watchdog",
"sudo file -s /dev/watchdog",
// df opens every operand, and only a directory or a block device
// can be told from the text
"df /dev/watchdog",
"df -h /dev/watchdog",
"df -h -- /dev/watchdog",
"df /dev/ttyS0 /dev/st0",
"df -h / /dev/watchdog",
"df -h /tmp/link",
"df -h /var/log",
"df -h /var/log/syslog",
"df watchdog",
"df -",
"df ''",
"df /dev/sda1/../watchdog",
"df /dev/nvme0",
"df -h /mnt/*/",
"df -t ext* /",
"df -Bt /dev/watchdog",
"df -Ft /dev/watchdog",
"df --ty /dev/watchdog",
"df --type=ext4 /dev/watchdog",
"df / -t /dev/watchdog",
"sudo df /dev/watchdog",
// fdisk -l opens every operand
"fdisk -l /dev/watchdog",
"fdisk -l /dev/sda /dev/st0",
"fdisk -l -- /dev/ttyS0",
"fdisk -l /tmp/link",
"sudo fdisk -l /dev/watchdog",
"last -f /etc/shadow",
"last --file=/etc/shadow",
"last --fi /etc/shadow",
"blkid -c /etc/x",
"blkid --cache-file=/etc/x",
"blkid -g",
"lsblk --sysroot /var/lib/shop/root -no MODEL",
"lsblk -o SERIAL --sysroot=/var/lib/shop/root",
"lsblk --sysr /var/lib/shop/root",
"sudo lsblk --sysroot /var/lib/shop/root",
// ss and lsof
"ss -K dst 192.0.2.10",
"ss -tK dst 192.0.2.10",
"ss --kill dst 192.0.2.10",
"ss --ki dst 192.0.2.10",
"ss -D /tmp/x",
"ss --diag=/tmp/x",
"ss -F /etc/shadow",
"ss --filter=/etc/shadow",
"lsof -Db/tmp/x",
"lsof -D b",
"lsof -k /tmp/x",
"lsof -m /tmp/x",
"lsof +m /tmp/x",
// journalctl and systemctl
"journalctl --vacuum-time=1s",
"journalctl -q --vacuum-size=1M",
"journalctl --vac=1M",
"journalctl --rotate",
"journalctl --rot",
"journalctl --flush",
"journalctl --sync",
"journalctl --relinquish-var",
"journalctl --smart-relinquish-var",
"journalctl --setup-keys",
"journalctl --update-catalog",
"journalctl --cursor-file=/etc/x -n 10",
"journalctl --cursor-f=/etc/x",
"journalctl --file=/tmp/x.journal",
"journalctl -D /tmp/journal",
"journalctl -xD/tmp/journal",
"journalctl --directory=/tmp/journal",
"journalctl --root=/mnt",
"journalctl --image=/tmp/disk.img",
"systemctl status -H root@192.0.2.1 nginx",
"systemctl status --host=root@192.0.2.1 nginx",
"service ../../tmp/x status",
"service nginx status extra",
// sar, ps, apt-cache, dpkg, pvesh
"sar -o /tmp/x.bin 1 1",
"sar -A -o/tmp/x.bin 1 1",
"sar -uo /tmp/x.bin 1 1",
"sar -f /etc/shadow",
"sar -f /var/log/sa/../../../etc/shadow",
"sar -f /var/log/sa/.*",
"sar -f",
"sar -fx",
"ps aux e",
"ps auxe",
"ps eww -p 1",
"ps -p 1 e",
"ps -o pid e",
"ps -ex",
"ps -axe",
"ps -auxeww",
"ps -ewwx",
"ps -aux -e",
"ps -p1e",
"ps -p 1 -o pid,environ",
"ps -eo pid,environ",
"ps --format=pid,ENVIRON",
"ps -o environ",
"ps --format environ",
"ps -e -pe",
"apt-cache gen*",
"ps a*",
"ifconfig d*",
"ip addr show e*",
"file -d /etc/shadow",
"file --debug /etc/shadow",
"file -bd /etc/shadow",
"apt-cache gencaches",
"apt-cache --with-source=/etc/shadow show nginx",
"apt-cache -o Dir::Cache::pkgcache=/etc/x show nginx",
"apt-cache -oDir::Cache=/etc show nginx",
"apt-cache --option=Dir::Cache=/etc show nginx",
"apt-cache -c /etc/shadow show nginx",
"apt-cache --config-file=/etc/shadow show nginx",
"apt-cache -p /etc/x show nginx",
"apt-cache -s /etc/x show nginx",
"dpkg -l --admindir=/tmp/x",
"dpkg -l --root=/mnt",
"dpkg -la",
"dpkg -L nginx --admindir=/tmp/x",
"pvesh get /nodes/pve/qemu/100/agent/file-read --file /etc/shadow",
"pvesh get nodes/pve/qemu/100/agent//file-read --file /etc/shadow",
"pvesh get /nodes/pve/storage/pbs/file-restore/download --volume x --filepath /",
// API subtrees that return stored credentials, or are not listed
"pvesh get /cluster/acme/plugins",
"pvesh get /cluster/acme/plugins/example_plugin",
"pvesh get cluster//acme/plugins/example_plugin --output-format json",
"pvesh get /cluster/sdn/dns/powerdns",
"pvesh get /cluster/sdn/ipams/netbox",
"pvesh get /access/domains/sso",
"pvesh get /access/users",
"pvesh get /cluster/options",
"pvesh get /cluster",
"pvesh get /",
"pvesh get /clusterstatus",
"pvesh get /nodes/../cluster/acme/plugins",
"pvesh get /nodes/%2e%2e/cluster/acme/plugins",
"pvesh get /Nodes",
"pvesh get /nodes/pve/qemu/100/cloudinit/dump --type user",
"pvesh get /nodes/pve/vzdump/extractconfig --volume local:backup/vzdump-qemu-100.vma.zst",
"pvesh get /nodes/pve/report",
"pvesh get /nodes/pve/subscription",
// the path is the word after get, and the only operand
"pvesh get",
"pvesh get --output-format json /cluster/acme/plugins",
"pvesh get --output-format /nodes /cluster/acme/plugins",
"pvesh get /nodes --api_path /cluster/acme/plugins",
"pvesh get /nodes -api=/cluster/acme/plugins",
"pvesh get --noproxy /nodes",
"pvesh get /nodes /cluster/acme/plugins",
"pvesh get /nodes --output-format=json /cluster/acme/plugins",
// podman global flags after the subcommand
"podman ps --root /tmp/x",
"podman ps --runroot=/tmp/x",
"podman ps --runtime /tmp/x",
"podman ps --out /tmp/x",
"podman logs --url ssh://root@192.0.2.1/run/podman.sock web",
"podman ps --connection remote",
"podman ps -c remote",
"podman ps -r",
// docker and podman templates that compute their output
"docker inspect -f '{{range .Config.Env}}{{index (split . \"=\") 1}} {{end}}' web",
"docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' web",
"docker inspect --format '{{index .Config.Env 1}}' web",
"docker inspect -f '{{with .Config}}{{.Env}}{{end}}' web",
"docker inspect -sf '{{join .Config.Env \",\"}}' web",
"docker inspect -f='{{printf \"%.3s\" .Config.Env}}' web",
"docker inspect -f '{{json (index .Config.Env 1)}}' web",
"docker inspect -f '{{- .Config.Env -}}' web",
"docker inspect -f '{{$c := .Config}}{{$c.Env}}' web",
"docker inspect -f '{{.Config.Labels.db_password}}' web",
"docker inspect -f '{{json .Config.Labels.Token}}' web",
"docker inspect -f '{{.State.Status' web",
"docker inspect -f {{.State.Status}} web",
"docker ps --format '{{.Label \"db_password\"}}'",
`docker ps --format "{{.Names}}\\t{{.Status}}"`,
`docker inspect -f "{{index .Config.Env 1}}" web`,
"sudo docker inspect -f '{{range .Config.Env}}{{.}}{{end}}' web",
"podman inspect --format '{{range .Config.Env}}{{slice . 3}}{{end}}' web",
"podman ps --format '{{index .Labels \"token\"}}'",
// Words the policy cannot read are not guessed.
"ss -tan $OPTS",
"ps aux `echo e`",
"journalctl -u nginx ~-",
} {
if got := policy.Evaluate(command); got != PolicyRequireApproval {
t.Errorf("Evaluate(%q) = %q, want %q", command, got, PolicyRequireApproval)
}
}
}
func TestSplitPolicyWords(t *testing.T) {
for _, tc := range []struct {
command string
words []string
ok bool
}{
{"journalctl --since=\"1 hour ago\" -u x", []string{"journalctl", "--since=1 hour ago", "-u", "x"}, true},
{"find / -name '*.log' -del''ete", []string{"find", "/", "-name", "*.log", "-delete"}, true},
{"find / \\( -name a \\)", []string{"find", "/", "(", "-name", "a", ")"}, true},
{"du -sh ~/x ~", []string{"du", "-sh", "~/x", "~"}, true},
{"ps -o \"$x\"", nil, false},
{`docker ps --format "table {{.Names}}\t{{.Status}}"`, []string{"docker", "ps", "--format", `table {{.Names}}\t{{.Status}}`}, true},
{`du -sh "/srv/a\\b"`, nil, false},
{`du -sh "/srv/a\" -X/etc/shadow "`, nil, false},
{"ps {a,b}", nil, false},
{"ps 'unterminated", nil, false},
{"ps ~root", nil, false},
{"ps a#b", nil, false},
} {
words, ok := splitPolicyWords(tc.command)
if ok != tc.ok {
t.Errorf("splitPolicyWords(%q) ok = %v, want %v", tc.command, ok, tc.ok)
continue
}
if !ok {
continue
}
var got []string
for _, word := range words {
got = append(got, word.text)
}
if len(got) != len(tc.words) {
t.Errorf("splitPolicyWords(%q) = %q, want %q", tc.command, got, tc.words)
continue
}
for i := range got {
if got[i] != tc.words[i] {
t.Errorf("splitPolicyWords(%q) = %q, want %q", tc.command, got, tc.words)
break
}
}
}
}