Files
Pulse/internal/ai/tools/tools_query_readonly_test.go
rcourtman 8196a5534f Judge Assistant read commands by their arguments, not first word
pulse_read runs commands on monitored hosts, usually as root, without
approval at every control level, as long as the classifier calls them
read-only. The classifier approved a command from its first word and
checked arguments only for find, curl and timeout. So date -s, sort -o,
journalctl --vacuum-time/--rotate, fuser -k, dmesg -C, sar -o, ss -K,
hostname NAME, ifconfig/arp/ip setters, smartctl SCT setters and many
other state-changing forms passed as known read-only, and route add was
blocked only because "add " matched the dd substring rule.

Every allowlisted program with mutating flags or subcommands now has an
explicit safe-forms rule, and unknown options fail closed. Commands
are parsed as literal shell words. Expansions, redirects other than the
fixed fd ones, command chaining and globs on validated programs are
refused, and every pipeline stage must itself be read-only. route is
judged on purpose. Ordinary diagnostics (journalctl -u ... --since,
dmesg -T, ss -tlnp, ip addr, sar 1 1, docker ps/logs, smartctl -a,
systemctl status) stay read-only. Plain blkid now needs -p, because
without it blkid updates its cache.
2026-10-17 09:07:46 +01:00

260 lines
9.5 KiB
Go

package tools
import "testing"
func TestExecutionIntent_ArgumentAwareReadCommands(t *testing.T) {
tests := []struct {
command string
readOnly bool
}{
{`date -s @1000000000`, false},
{`date --set=@1000000000`, false},
{`date 101612002026`, false},
{`sort -o /tmp/x /etc/hostname`, false},
{`sort -no/tmp/x /etc/hostname`, false},
{`sort --output=/tmp/x /etc/hostname`, false},
{`sort --compress-program=sh /etc/hostname`, false},
{`sort -o /tmp/x --random-source="mysql unused" /etc/hostname`, false},
{`uniq /etc/hostname /tmp/x`, false},
{`journalctl --vacuum-time=1s`, false},
{`journalctl --rotate`, false},
{`journalctl --flush`, false},
{`journalctl --sync`, false},
{`journalctl --relinquish-var`, false},
{`journalctl --setup-keys`, false},
{`journalctl --smart-relinquish-var`, false},
{`journalctl --synchronize-on-exit=yes -n 10`, false},
{`journalctl --cursor-file=/tmp/x -n 10`, false},
{`journalctl --update-catalog`, false},
{`fuser -k /tmp/x`, false},
{`fuser -mk /tmp/x`, false},
{`fuser -k "/tmp/mysql target"`, false},
{`dmesg -C`, false},
{`dmesg -c`, false},
{`dmesg -n 1`, false},
{`dmesg -D`, false},
{`dmesg -E`, false},
{`sar -o /tmp/x.bin 1 1`, false},
{`sar -A -o/tmp/x.bin 1 1`, false},
{`hostname changed`, false},
{`hostname -F /tmp/name`, false},
{`uname -S changed`, false},
{`ifconfig eth0 down`, false},
{`ifconfig eth0 192.0.2.10`, false},
{`route add default gw 192.0.2.1`, false},
{`route del default`, false},
{`arp -s 192.0.2.10 00:11:22:33:44:55`, false},
{`ip addr add 192.0.2.10/24 dev eth0`, false},
{`ip -4 route del default`, false},
{`ip link set eth0 down`, false},
{`ip neigh flush all`, false},
{`ip route restore`, false},
{`ip route save`, false},
{`ip addresslabel add prefix ::1`, false},
{`ss -K dst 192.0.2.10`, false},
{`ss -tK dst 192.0.2.10`, false},
{`ss -D /tmp/x`, false},
{`ss --diag /tmp/x`, false},
{`ss --diag=/tmp/x`, false},
{`ss --diag "/tmp/mysql capture"`, false},
{`lastlog -C -u root`, false},
{`lastlog -S -u root`, false},
{`file -C -m /tmp/magic`, false},
{`file --preserve-date /etc/hostname`, false},
{`df --sync`, false},
{`dmidecode --dump-bin /tmp/x`, false},
{`hwinfo --log /tmp/x`, false},
{`inxi --output json --output-file /tmp/x`, false},
{`inxi -U`, false},
{`inxi -c94`, false},
{`inxi --color=99`, false},
{`hddtemp -d /dev/sda`, false},
{`sensors -s`, false},
{`smartctl -s on /dev/sda`, false},
{`smartctl -t short /dev/sda`, false},
{`smartctl -X /dev/sda`, false},
{`smartctl -l sataphy,reset /dev/sda`, false},
{`smartctl --log=scterc,70,70 /dev/sda`, false},
{`smartctl -al sataphy,reset /dev/sda`, false},
{`nvme format /dev/nvme0n1`, false},
{`nvme set-feature /dev/nvme0 -f 1 -v 1`, false},
{`nvme get-log /dev/nvme0 --raw-binary --log-len=16 --log-id=1 --output-file=/tmp/x`, false},
{`ffprobe -o /tmp/x video.mp4`, false},
{`ffprobe -report video.mp4`, false},
{`mediainfo --LogFile=/tmp/x video.mp4`, false},
{`exiftool -Artist=changed image.jpg`, false},
{`exiftool -o /tmp/x image.jpg`, false},
{`exiftool -w txt image.jpg`, false},
{`exiftool -config /tmp/config image.jpg`, false},
{`exiftool -config "/tmp/mysql config" image.jpg`, false},
{`exiftool -if 'system("id")' image.jpg`, false},
{`lsof -Db/tmp/x`, false},
{`lsof -Du/tmp/x`, false},
{`blkid -w /tmp/x`, false},
{`systemctl status --job-mode=flush nginx`, false},
{`systemctl list-jobs --after`, true},
{`systemctl restart nginx`, false},
{`systemctl list-unit-files --now`, false},
{`systemctl showevil nginx`, false},
{`qm guest cmd 100 fsfreeze-freeze`, false},
{`pvesh get /nodes --output-format json --output-file /tmp/x`, false},
{`apt-cache gencaches`, false},
{`rpm -q --eval '%{lua:os.execute("id")}'`, false},
{`zpool status -c /tmp/script`, false},
{`kubectl get pods --raw=/api --output-watch-events --kubeconfig=/tmp/config`, false},
{`kubectl cluster-info dump --output-directory=/tmp/x`, false},
{`nc -zv -o /tmp/x example.com 22`, false},
{`nc -z -e /bin/sh example.com 22`, false},
{`nc -- -z 22`, false},
{`nc -s-z example.com 22`, false},
{`nslookup`, false},
{`nslookup - 192.0.2.1`, false},
{`printf 'exit\n' | nslookup`, false},
{`sysctl -w kernel.hostname=changed`, false},
{`sysctl kernel.hostname=changed`, false},
{`timedatectl set-time 2026-01-01`, false},
{`tee /tmp/x`, false},
{`touch /tmp/x`, false},
{`rm -rf /tmp/x`, false},
{`sed e id /etc/hostname`, false},
{`sed e /etc/hostname "mysql file"`, false},
{`unknown-command "mysql SELECT 1"`, false},
{`"mysql fake" -e "SELECT 1"`, false},
{`'"mysql"' "SELECT 1"`, false},
{`'"curl"' https://example.com`, false},
{`'"wget"' --spider https://example.com`, false},
{`'"timeout"' 5s ls`, false},
{`cat f | sed e`, false},
{`echo x > f`, false},
{`cat /etc/hostname 2>/dev/null.tmp`, false},
{`id && touch f`, false},
{`curl -T f url`, false},
{`curl --trace /tmp/x https://example.com`, false},
{`curl --trace-ascii /tmp/x https://example.com`, false},
{`curl --libcurl /tmp/x https://example.com`, false},
{`curl -D /tmp/x https://example.com`, false},
{`curl -\T f https://example.com`, false},
{`wget --spider --save-cookies=/tmp/x https://example.com`, false},
{`wget --spider --execute='output_document=/tmp/x' https://example.com`, false},
{`wget -- --spider https://example.com`, false},
{`find / -delete`, false},
{`find / -de\lete`, false},
{`awk 'BEGIN{system("id")}'`, false},
{`cat f | date -s @1000000000`, false},
{`cat f | sort -o /tmp/x | head`, false},
{`cat f|uniq /tmp/x /tmp/y`, false},
{`timeout 5s journalctl --rotate`, false},
{`date '-s' @1000000000`, false},
{`date -\s @1000000000`, false},
{`sort file*`, false},
{`uniq $FILES`, false},
{`env FOO=$PAYLOAD`, false},
{`env -u *`, false},
{`'journalctl' -f`, false},
{`dme\sg -w`, false},
{`date --unknown-option`, false},
{`journalctl --rot`, false},
{`sort --out=/tmp/x file`, false},
{`sort -k`, false},
{`date -d '-1 day' +%s`, true},
{`date`, true},
{`date -u +%FT%TZ`, true},
{`date -d 'yesterday' +%s`, true},
{`date --iso-8601=seconds`, true},
{`sort file`, true},
{`sort -nr -k 2 file`, true},
{`uniq -c file`, true},
{`journalctl -u x --since "10 min ago" -n 100 --no-pager`, true},
{`journalctl -o json --output-fields=MESSAGE -n10`, true},
{`journalctl -b --since '-1 hour'`, true},
{`journalctl -b -1 -n 50`, true},
{`dmesg -T`, true},
{`dmesg --level=err,warn`, true},
{`hostname -f`, true},
{`uname -a`, true},
{`ip addr`, true},
{`ip -4 route show table all`, true},
{`ip -6 neigh show`, true},
{`ip link show dev eth0`, true},
{`ss -tlnp`, true},
{`ss -t state established`, true},
{`sar 1 1`, true},
{`sar -n DEV 1 1`, true},
{`iostat -o JSON 1 1`, true},
{`mpstat -o JSON 1 1`, true},
{`lsblk -o NAME,SIZE,TYPE`, true},
{`ifconfig -a`, true},
{`ifconfig eth0`, true},
{`route -n`, true},
{`arp -an`, true},
{`lsof -nP -iTCP:443`, true},
{`lsof -i`, true},
{`fuser -v /tmp/x`, true},
{`nslookup example.com`, true},
{`nslookup -type=AAAA example.com 192.0.2.1`, true},
{`dig +short example.com`, true},
{`hddtemp -n /dev/sda`, true},
{`hwinfo --short`, true},
{`inxi -Fz`, true},
{`inxi -c 0 -F`, true},
{`blkid -p /dev/sda`, true},
{`file /etc/hostname`, true},
{`df -hT`, true},
{`lastlog -u root`, true},
{`dmidecode -t memory`, true},
{`sensors -j`, true},
{`smartctl -a /dev/sda`, true},
{`smartctl -al selftest /dev/sda`, true},
{`nvme smart-log /dev/nvme0 -o json`, true},
{`ffprobe -v error -show_format -show_streams -of json video.mp4`, true},
{`mediainfo --Output=JSON video.mp4`, true},
{`exiftool -json image.jpg`, true},
{`systemctl status nginx --no-pager`, true},
{`systemctl list-units --all`, true},
{`docker ps -a`, true},
{`docker logs --tail 50 app`, true},
{`docker logs -f --tail 50 app`, true},
{`docker network inspect bridge`, true},
{`pct config 100`, true},
{`qm guest cmd 100 network-get-interfaces`, true},
{`pvesh get /nodes --output-format json`, true},
{`zpool status -v`, true},
{`zpool status -x`, true},
{`zfs get all rpool`, true},
{`dpkg -l`, true},
{`rpm -qa`, true},
{`kubectl get pods -A -o wide`, true},
{`kubectl logs pod --tail=100`, true},
{`nc -w 3 -zv example.com 22`, true},
{`curl -fsSI --connect-timeout 5 https://example.com`, true},
{`wget -q --spider https://example.com`, true},
{`cat /var/log/*.log | grep error | sort -u | head -20`, true},
{`cat /etc/hostname 2>/dev/null`, true},
{`cat /etc/hostname 2>&1`, true},
{`sort '2>/dev/null'`, true},
{`ss -tuln | grep -E '3000|9090|22'`, true},
{`timeout 5s journalctl -f`, true},
}
for _, tt := range tests {
t.Run(tt.command, func(t *testing.T) {
got := ClassifyExecutionIntent(tt.command)
if readOnly := got.Intent != IntentWriteOrUnknown; readOnly != tt.readOnly {
t.Errorf("ClassifyExecutionIntent(%q) = %v (%s), want readOnly=%t", tt.command, got.Intent, got.Reason, tt.readOnly)
}
})
}
}
func TestExecutionIntent_RouteOperationBoundary(t *testing.T) {
command := "route add default gw 192.0.2.1"
if reason := matchesWritePatterns(command, command); reason != "" {
t.Fatalf("route operation matched an unrelated write pattern: %s", reason)
}
if isReadOnlyByConstruction(command) {
t.Fatal("route add must fail argument validation")
}
if reason := matchesWritePatterns("dd if=/dev/zero of=/tmp/x", "dd if=/dev/zero of=/tmp/x"); reason != "disk write" {
t.Fatalf("dd classification = %q, want disk write", reason)
}
}