Files
sam 125457a391
publish / publish (push) Successful in 1m21s
feat(connector): declare privileged effects on every lookup and action (#2071)
Regenerate the connector SDK from the host contract, which adds the
effects_declared, privilege_change and secret_access fields. Every lookup and
action in this repository now states them, and a Semaphore template states
them in its effect settings or is unavailable in chat.
2026-11-21 21:29:49 +11:00

365 lines
16 KiB
Go

package main
import (
"context"
"encoding/json/jsontext"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/samcharles93/archipelago/sdk/connector"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// fakeHost is a host whose runner records commands and never touches a
// network.
func fakeHost(t *testing.T, res execResult) (*host, *[]string) {
t.Helper()
target := Target{
Name: "web-1", Environment: "production", Helpers: true,
Paths: []string{"/srv/app", "/var/tmp"},
Disposable: []string{"/var/tmp/cache"},
Timers: []string{"backup.timer"},
ConfigFiles: []string{"/srv/app/app.conf"},
}
if err := target.checkActionSettings(); err != nil {
t.Fatal(err)
}
var ran []string
h := &host{targets: map[string]Target{"web-1": target}, runner: func(_ context.Context, _ Target, command string) execResult {
ran = append(ran, command)
return res
}}
return h, &ran
}
// TestActionInputIsConfined guards the host connector's write boundary: an
// action that names a directory outside the host's roots, a pattern that
// leaves its directory, a timer or file off the allow-lists, or text that
// could break out of its shell argument is refused before any command is
// built, so nothing reaches the host.
func TestActionInputIsConfined(t *testing.T) {
tests := []struct {
name string
action string
input string
}{
{"directory outside the roots", actionDeleteFiles, `{"host":"web-1","directory":"/etc","pattern":"*.log","older_than_hours":24}`},
{"directory climbing out of a root", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app/../../etc","pattern":"*.log","older_than_hours":24}`},
{"a root's name as a prefix", actionDeleteFiles, `{"host":"web-1","directory":"/srv/application","pattern":"*.log","older_than_hours":24}`},
{"relative directory", actionDeleteFiles, `{"host":"web-1","directory":"srv/app","pattern":"*.log","older_than_hours":24}`},
{"newline in directory", actionDeleteFiles, "{\"host\":\"web-1\",\"directory\":\"/srv/app\\n/etc\",\"pattern\":\"*.log\",\"older_than_hours\":24}"},
{"pattern with a path separator", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"../*","older_than_hours":24}`},
{"pattern with a backslash", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"a\\b","older_than_hours":24}`},
{"empty pattern", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"","older_than_hours":24}`},
{"newline in pattern", actionDeleteFiles, "{\"host\":\"web-1\",\"directory\":\"/srv/app\",\"pattern\":\"a\\nb\",\"older_than_hours\":24}"},
{"no age", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"*.log"}`},
{"age under an hour", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"*.log","older_than_hours":0}`},
{"unknown host", actionDeleteFiles, `{"host":"web-9","directory":"/srv/app","pattern":"*.log","older_than_hours":24}`},
{"unknown member", actionDeleteFiles, `{"host":"web-1","directory":"/srv/app","pattern":"*.log","older_than_hours":24,"extra":"x"}`},
{"timer off the allow-list", actionDisableTimer, `{"host":"web-1","timer":"apt-daily.timer"}`},
{"timer name with an injection", actionDisableTimer, `{"host":"web-1","timer":"backup.timer; id"}`},
{"timer enable off the allow-list", actionEnableTimer, `{"host":"web-1","timer":"other.timer"}`},
{"config file off the allow-list", actionSetConfig, `{"host":"web-1","file":"/etc/shadow","key":"A","value":"b"}`},
{"config file climbing to another", actionSetConfig, `{"host":"web-1","file":"/srv/app/../../etc/shadow","key":"A","value":"b"}`},
{"config key with an equals sign", actionSetConfig, `{"host":"web-1","file":"/srv/app/app.conf","key":"A=b","value":"b"}`},
{"config key with a space", actionSetConfig, `{"host":"web-1","file":"/srv/app/app.conf","key":"A b","value":"b"}`},
{"config value with a newline", actionSetConfig, "{\"host\":\"web-1\",\"file\":\"/srv/app/app.conf\",\"key\":\"A\",\"value\":\"b\\nC=d\"}"},
{"config value too long", actionSetConfig, `{"host":"web-1","file":"/srv/app/app.conf","key":"A","value":"` + strings.Repeat("v", maxValueLen+1) + `"}`},
{"unknown action", "run_command", `{"host":"web-1","command":"id"}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, ran := fakeHost(t, execResult{started: true})
if _, err := h.Resolve(t.Context(), tt.action, jsontext.Value(tt.input)); err == nil {
t.Fatal("Resolve accepted it")
}
if _, unknown, err := h.Invoke(t.Context(), tt.action, jsontext.Value(tt.input), "k1"); err == nil || unknown {
t.Fatalf("Invoke err=%v unknown=%v, want a plain refusal", err, unknown)
}
if len(*ran) != 0 {
t.Fatalf("a command was built for a refused action: %v", *ran)
}
})
}
}
// TestSettingsAreChecked guards the per-host settings the actions trust: a
// disposable directory outside the host's roots would let a claim of
// recoverability cover anything, and a timer without the sudo helpers has no
// way to run.
func TestSettingsAreChecked(t *testing.T) {
tests := []struct {
name string
t Target
}{
{"disposable outside the roots", Target{Name: "web-1", Paths: []string{"/srv/app"}, Disposable: []string{"/etc"}}},
{"disposable is the filesystem root", Target{Name: "web-1", Paths: []string{"/"}, Disposable: []string{"/"}}},
{"timer that isn't a timer", Target{Name: "web-1", Helpers: true, Timers: []string{"sshd.service"}}},
{"timers without helpers", Target{Name: "web-1", Timers: []string{"backup.timer"}}},
{"relative config file", Target{Name: "web-1", ConfigFiles: []string{"app.conf"}}},
{"host name with a space", Target{Name: "web 1"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.t.checkActionSettings(); err == nil {
t.Fatal("accepted")
}
})
}
}
// TestDisposableDirectoryIsRecoverable guards the reversibility claim: a
// directory under a disposable one resolves as disposable and recoverable,
// any other under the roots as irreversible, and the shell guard of a
// disposable delete only admits the disposable directories, so a symlink
// can't carry the claim elsewhere.
func TestDisposableDirectoryIsRecoverable(t *testing.T) {
h := &host{targets: map[string]Target{"web-1": {Name: "web-1", Environment: "staging", Paths: []string{"/srv/app", "/var/tmp"}, Disposable: []string{"/var/tmp/cache"}}}}
tests := []struct {
dir string
disposable bool
reversible connectorv1.Reversibility
}{
{"/var/tmp/cache", true, connectorv1.Reversibility_REVERSIBILITY_RECOVERABLE},
{"/var/tmp/cache/pip", true, connectorv1.Reversibility_REVERSIBILITY_RECOVERABLE},
{"/var/tmp/cache2", false, connectorv1.Reversibility_REVERSIBILITY_IRREVERSIBLE},
{"/var/tmp", false, connectorv1.Reversibility_REVERSIBILITY_IRREVERSIBLE},
{"/srv/app/logs", false, connectorv1.Reversibility_REVERSIBILITY_IRREVERSIBLE},
}
for _, tt := range tests {
t.Run(tt.dir, func(t *testing.T) {
in := jsontext.Value(`{"host":"web-1","directory":"` + tt.dir + `","pattern":"*","older_than_hours":2}`)
res, err := h.Resolve(t.Context(), actionDeleteFiles, in)
if err != nil {
t.Fatal(err)
}
got := res.GetTarget()
if got.GetKind() != "host" || got.GetId() != "web-1" || got.GetEnvironment() != "staging" || got.GetDisposable() != tt.disposable {
t.Fatalf("target = %v", got)
}
if res.GetEffect().GetReversibility() != tt.reversible || res.GetEffect().GetReadOnly() {
t.Fatalf("effect = %v", res.GetEffect())
}
p, err := h.plan(actionDeleteFiles, in)
if err != nil {
t.Fatal(err)
}
cmd := p.command("k")
if strings.Contains(cmd, "/srv/app") && tt.disposable || tt.disposable && !strings.Contains(cmd, "'/var/tmp/cache'") || tt.disposable && strings.Contains(cmd, "'/var/tmp'") {
t.Fatalf("disposable delete guarded by the wrong roots: %s", cmd)
}
})
}
}
// TestGeneratedCommandsKeepArguments guards command injection: whatever an
// accepted pattern, key or value contains, the command stays one well-formed
// shell command (syntax-checked, never run) with the text inside quotes.
func TestGeneratedCommandsKeepArguments(t *testing.T) {
h, _ := fakeHost(t, execResult{started: true})
h.targets["web-1"] = func() Target { x := h.targets["web-1"]; x.Helpers = false; x.Timers = nil; return x }()
evil := `x'; touch pwned; echo '$(id)` + "`id`"
inputs := []struct{ action, in string }{
{actionDeleteFiles, `{"host":"web-1","directory":"/srv/app/it's","pattern":` + string(mustJSON(t, evil)) + `,"older_than_hours":3}`},
{actionSetConfig, `{"host":"web-1","file":"/srv/app/app.conf","key":"A_b","value":` + string(mustJSON(t, evil)) + `}`},
}
for _, tt := range inputs {
t.Run(tt.action, func(t *testing.T) {
p, err := h.plan(tt.action, jsontext.Value(tt.in))
if err != nil {
t.Fatal(err)
}
cmd := p.command("k")
if out, err := exec.CommandContext(t.Context(), "sh", "-n", "-c", cmd).CombinedOutput(); err != nil {
t.Fatalf("not a well-formed command: %v: %s", err, out)
}
// The payload must appear only as one quoted word.
if !strings.Contains(cmd, quote(evil)) || strings.Count(cmd, "touch pwned") != 1 {
t.Fatalf("payload isn't one quoted word: %s", cmd)
}
})
}
}
func mustJSON(t *testing.T, s string) []byte {
t.Helper()
b, err := jsontext.AppendQuote(nil, s)
if err != nil {
t.Fatal(err)
}
return b
}
// TestConfineResolvesSymlinks guards the shell-side half of confinement: a
// directory that is lexically inside a root but a symlink out of it is
// refused once resolved. Only the read-only guard runs.
func TestConfineResolvesSymlinks(t *testing.T) {
base := t.TempDir()
root, outside := filepath.Join(base, "root"), filepath.Join(base, "outside")
for _, d := range []string{filepath.Join(root, "inner"), outside} {
if err := os.MkdirAll(d, 0o700); err != nil {
t.Fatal(err)
}
}
if err := os.Symlink(outside, filepath.Join(root, "escape")); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
dir string
ok bool
}{
{"inside", filepath.Join(root, "inner"), true},
{"the root itself", root, true},
{"symlink out of the root", filepath.Join(root, "escape"), false},
{"outside entirely", outside, false},
{"missing", filepath.Join(root, "nope"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
script := confine(tt.dir, []string{root}) + `printf '%s' "$d"`
out, err := exec.CommandContext(t.Context(), "sh", "-c", script).Output()
if tt.ok && err != nil {
t.Fatalf("refused: %v", err)
}
if !tt.ok && err == nil {
t.Fatalf("admitted %s as %s", tt.dir, out)
}
})
}
}
// TestInvokeOutcomes guards what an operator is told about a run: a dropped
// session is an unknown outcome (the work may be half done), a run that never
// started or exited non-zero is a plain failure, and a success reports what
// was done.
func TestInvokeOutcomes(t *testing.T) {
del := jsontext.Value(`{"host":"web-1","directory":"/srv/app/logs","pattern":"*.log","older_than_hours":24}`)
tests := []struct {
name string
res execResult
wantUnknown bool
wantErr bool
wantOut string
}{
{"deleted two files", execResult{started: true, exited: true, out: []byte("100\n250\n")}, false, false, `"bytes_deleted":350,"directory":"/srv/app/logs","files_deleted":2`},
{"nothing to delete", execResult{started: true, exited: true}, false, false, `"files_deleted":0`},
{"session dropped", execResult{started: true, out: []byte("100\n"), err: errors.New("wait: remote command exited without exit status or exit signal")}, true, true, ""},
{"exit status", execResult{started: true, exited: true, status: 1, out: []byte("100\n"), err: errors.New("exit")}, false, true, ""},
{"never started", execResult{err: errors.New("reach web-1: refused")}, false, true, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, _ := fakeHost(t, tt.res)
out, unknown, err := h.Invoke(t.Context(), actionDeleteFiles, del, "k1")
if (err != nil) != tt.wantErr || unknown != tt.wantUnknown {
t.Fatalf("err=%v unknown=%v", err, unknown)
}
if tt.wantOut != "" && !strings.Contains(string(out), tt.wantOut) {
t.Fatalf("output %s lacks %s", out, tt.wantOut)
}
})
}
h, _ := fakeHost(t, execResult{started: true, exited: true})
if _, _, err := h.Invoke(t.Context(), actionDeleteFiles, del, ""); err == nil {
t.Fatal("ran without an idempotency key")
}
}
// TestDeleteRefusesFilesystemRoot guards a host whose roots include "/": a
// delete there may reach any directory under it but never "/" itself.
func TestDeleteRefusesFilesystemRoot(t *testing.T) {
h := &host{targets: map[string]Target{"web-1": {Name: "web-1", Paths: []string{"/"}}}}
for _, dir := range []string{"/", "//", "/srv/.."} {
in := jsontext.Value(`{"host":"web-1","directory":"` + dir + `","pattern":"*","older_than_hours":2}`)
if _, err := h.Resolve(t.Context(), actionDeleteFiles, in); err == nil {
t.Fatalf("accepted %q", dir)
}
}
in := jsontext.Value(`{"host":"web-1","directory":"/srv","pattern":"*","older_than_hours":2}`)
if _, err := h.Resolve(t.Context(), actionDeleteFiles, in); err != nil {
t.Fatal(err)
}
}
// TestCommandsDoWhatTheyReport runs the generated shell on files in a
// temporary directory: delete_files removes only old regular files matching
// the glob and reports their sizes, and set_config_key rewrites one key
// literally (backslashes and ampersands included), keeps a backup, and is a
// no-op the second time.
func TestCommandsDoWhatTheyReport(t *testing.T) {
dir := t.TempDir()
write := func(name, body string, age time.Duration) string {
t.Helper()
p := filepath.Join(dir, name)
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
when := time.Now().Add(-age)
if err := os.Chtimes(p, when, when); err != nil {
t.Fatal(err)
}
return p
}
exists := func(p string) bool { _, err := os.Lstat(p); return err == nil }
run := func(command string) string {
t.Helper()
out, err := exec.CommandContext(t.Context(), "sh", "-c", command).Output()
if err != nil {
t.Fatalf("%v: %s", err, command)
}
return string(out)
}
oldLog := write("a.log", "12345", 48*time.Hour)
newLog := write("b.log", "x", time.Minute)
oldTxt := write("c.txt", "x", 48*time.Hour)
link := filepath.Join(dir, "d.log")
if err := os.Symlink(oldTxt, link); err != nil {
t.Fatal(err)
}
files, bytes := sumSizes([]byte(run(deleteCommand(dir, "*.log", "120", []string{dir}))))
if files != 1 || bytes != 5 || exists(oldLog) || !exists(newLog) || !exists(oldTxt) || !exists(link) {
t.Fatalf("deleted %d files, %d bytes; old log gone=%v, new log kept=%v, txt kept=%v, link kept=%v",
files, bytes, !exists(oldLog), exists(newLog), exists(oldTxt), exists(link))
}
conf := write("app.conf", "# keep\nA = 1\nB=2\n", time.Minute)
set := func(key, value string) string {
return strings.TrimSpace(run(setKeyCommand(conf, conf+".bak-"+key, key, value)))
}
value := `a\n&b $x`
if got := set("A", value); got != "changed "+conf+".bak-A" {
t.Fatalf("first set printed %q", got)
}
body, _ := os.ReadFile(conf)
if want := "# keep\nA=" + value + "\nB=2\n"; string(body) != want {
t.Fatalf("config = %q, want %q", body, want)
}
if backup, _ := os.ReadFile(conf + ".bak-A"); string(backup) != "# keep\nA = 1\nB=2\n" {
t.Fatalf("backup = %q", backup)
}
if got := set("A", value); got != "unchanged" {
t.Fatalf("second set printed %q", got)
}
set("C", "3")
if body, _ = os.ReadFile(conf); !strings.HasSuffix(string(body), "B=2\nC=3\n") {
t.Fatalf("appended config = %q", body)
}
}
// TestEveryToolDeclaresItsEffects guards the chat prohibition: chat offers only
// lookups and actions that state whether they change privileges or access
// secrets.
func TestEveryToolDeclaresItsEffects(t *testing.T) {
h, _ := fakeHost(t, execResult{})
if names := connector.UndeclaredEffects(h.Describe()); len(names) > 0 {
t.Fatalf("no declared effects for %v", names)
}
}