mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-02 10:09:02 +01:00
pulse_read runs diagnostic commands on monitored hosts for Patrol investigations and Pulse Mobile chat without a per-call approval, so keep it to local, non-secret reads. Database clients and network clients now go through the governed control path; ping stays for bounded probes of IP addresses. The sensitive-path check parses each command word by word and covers more credential stores and process-environment output, and ps -e is refused on targets where it prints environments. Redaction recognises more secret shapes, such as prefixed environment-style keys and credentials in any URL scheme. The agent runs Pulse-issued commands without its own PULSE_* configuration in their environment, and unapproved reads also drop credential-named variables. Contract notes in ai-runtime.md and agent-lifecycle.md.
243 lines
14 KiB
Go
243 lines
14 KiB
Go
package tools
|
|
|
|
import "testing"
|
|
|
|
// This file provides white-box table tests for the pure string-routing hint
|
|
// helpers in tools_query.go:
|
|
// - GetReadOnlyViolationHint (top-level routing)
|
|
// - isPhase1GuardrailFailure (keyword classification)
|
|
// - getPhase1Hint (structural guardrail switch)
|
|
// - getSQLHint (SQL client refusal hint)
|
|
//
|
|
// All four functions are pure (no I/O) and return exact strings/bools, so every
|
|
// case asserts the exact returned value rather than just "no panic".
|
|
|
|
func TestGetReadOnlyViolationHint(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
command string
|
|
result IntentResult
|
|
want string
|
|
}{
|
|
// Empty / boundary inputs.
|
|
{
|
|
name: "empty command and empty reason returns empty base hint",
|
|
command: "",
|
|
result: IntentResult{},
|
|
want: "",
|
|
},
|
|
{
|
|
name: "phase1 reason with empty command still routes to phase1 hint",
|
|
command: "",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "sudo escalates command privileges"},
|
|
want: "sudo escalates command privileges. Read-only execution does not accept sudo; privileged operations require the governed control path.",
|
|
},
|
|
{
|
|
name: "sql cli command with empty reason routes to sql default hint",
|
|
command: "sqlite3 db",
|
|
result: IntentResult{},
|
|
want: ". pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
|
|
// Phase 1 guardrail routing.
|
|
{
|
|
name: "phase1 redirect reason routes to phase1 redirect hint",
|
|
command: "echo hi > /tmp/x",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "output redirection can overwrite files"},
|
|
want: "output redirection can overwrite files. Read-only execution does not accept redirects (>, >>, <, <<, <<<).",
|
|
},
|
|
|
|
// Phase 1 precedence over SQL CLI detection.
|
|
{
|
|
name: "phase1 takes precedence over sql cli command match",
|
|
command: "sudo sqlite3 db.db \"SELECT 1\"",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "sudo escalates command privileges"},
|
|
want: "sudo escalates command privileges. Read-only execution does not accept sudo; privileged operations require the governed control path.",
|
|
},
|
|
|
|
// SQL CLI routing for each recognised CLI binary.
|
|
{
|
|
name: "sqlite3 cli with write keyword routes to sql write hint",
|
|
command: "sqlite3 db.db \"INSERT INTO t VALUES(1)\"",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "content inspection: SQL contains write/control keyword: insert"},
|
|
want: "content inspection: SQL contains write/control keyword: insert. pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
{
|
|
name: "mysql cli with statement-inspection reason routes to sql hint",
|
|
command: "mysql db",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "content inspection: " + sqlClientNotReadOnlyReason},
|
|
want: "content inspection: " + sqlClientNotReadOnlyReason + ". pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
{
|
|
name: "mariadb cli with neutral reason routes to sql hint",
|
|
command: "mariadb db -e \"SELECT 1\"",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "content inspection"},
|
|
want: "content inspection. pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
{
|
|
name: "psql cli with control keyword routes to sql write hint",
|
|
command: "psql -c \"BEGIN\"",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "content inspection: SQL contains write/control keyword: begin"},
|
|
want: "content inspection: SQL contains write/control keyword: begin. pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
|
|
// SQL CLI precedence over the unknown-command fallback.
|
|
{
|
|
name: "sql cli command beats unknown reason fallback",
|
|
command: "sqlite3 db",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "unknown command"},
|
|
want: "unknown command. pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
|
|
// Unknown-command fallback (non-SQL, non-phase1).
|
|
{
|
|
name: "non-sql unknown reason routes to self-contained hint",
|
|
command: "foobar",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "unknown command is not on the read-only allowlist"},
|
|
want: "unknown command is not on the read-only allowlist. Try a self-contained form: no pipes, no redirects, single statement. If this is a read-only operation, consider using a known read-only command instead.",
|
|
},
|
|
{
|
|
name: "non-sql no-inspector reason routes to self-contained hint",
|
|
command: "foobar",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "no inspector matched this command"},
|
|
want: "no inspector matched this command. Try a self-contained form: no pipes, no redirects, single statement. If this is a read-only operation, consider using a known read-only command instead.",
|
|
},
|
|
|
|
// Plain fallback (no routing matches): base hint returned unchanged.
|
|
{
|
|
name: "non-sql unmatched reason returns base hint unchanged",
|
|
command: "foobar",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "random reason"},
|
|
want: "random reason",
|
|
},
|
|
|
|
// Documents actual (surprising) behaviour for interactive_repl blocks.
|
|
// The interactive_repl NonInteractiveBlock reason is not in the phase1
|
|
// keyword list, so routing falls through to SQL/unknown/default paths.
|
|
{
|
|
name: "bare mysql interactive_repl block routes to sql hint",
|
|
command: "mysql",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "[interactive_repl] command opens interactive session; pulse_read requires bounded non-interactive commands"},
|
|
want: "[interactive_repl] command opens interactive session; pulse_read requires bounded non-interactive commands. pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead.",
|
|
},
|
|
{
|
|
name: "python interactive_repl block returns base hint unchanged",
|
|
command: "python",
|
|
result: IntentResult{Intent: IntentWriteOrUnknown, Reason: "[interactive_repl] command opens interactive session; pulse_read requires bounded non-interactive commands"},
|
|
want: "[interactive_repl] command opens interactive session; pulse_read requires bounded non-interactive commands",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := GetReadOnlyViolationHint(tc.command, tc.result)
|
|
if got != tc.want {
|
|
t.Fatalf("GetReadOnlyViolationHint(command=%q, reason=%q) =\n %q\nwant\n %q",
|
|
tc.command, tc.result.Reason, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsPhase1GuardrailFailure(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
reason string
|
|
want bool
|
|
}{
|
|
// Phase 1 structural keywords (case-sensitive substring match).
|
|
{name: "sudo keyword", reason: "sudo escalates command privileges", want: true},
|
|
{name: "redirect substring inside redirection", reason: "output redirection can overwrite files", want: true},
|
|
{name: "redirect exact substring", reason: "has redirect here", want: true},
|
|
{name: "tee keyword", reason: "tee can write to files", want: true},
|
|
{name: "substitution keyword", reason: "command substitution can execute arbitrary commands", want: true},
|
|
{name: "chaining keyword", reason: "shell chaining detected outside quotes", want: true},
|
|
{name: "piped input keyword", reason: "piped input to dual-use tool prevents content inspection", want: true},
|
|
|
|
// NonInteractiveOnly keywords.
|
|
{name: "TTY uppercase keyword", reason: "[tty_flag] interactive/TTY flags require terminal", want: true},
|
|
{name: "terminal keyword alone", reason: "requires terminal interaction", want: true},
|
|
{name: "pager keyword", reason: "[pager] pager/editor tools require terminal", want: true},
|
|
{name: "editor keyword alone", reason: "editor tools need a terminal", want: true},
|
|
{name: "indefinitely keyword", reason: "live monitoring tools run indefinitely", want: true},
|
|
{name: "unbounded keyword", reason: "[unbounded_stream] follow mode without bound", want: true},
|
|
{name: "streaming keyword alone", reason: "unbounded streaming detected", want: true},
|
|
|
|
// Negative cases (case-sensitive, spacing-sensitive, unmatched).
|
|
{name: "empty reason returns false", reason: "", want: false},
|
|
{name: "tty lowercase does not match (case sensitive)", reason: "tty flag", want: false},
|
|
{name: "SUDO uppercase does not match (case sensitive)", reason: "SUDO escalation", want: false},
|
|
{name: "pipedinput without space does not match", reason: "pipedinput detected", want: false},
|
|
{name: "unrelated reason returns false", reason: "unknown command is not on the read-only allowlist", want: false},
|
|
{name: "interactive_repl real reason not classified as phase1", reason: "[interactive_repl] command opens interactive session; pulse_read requires bounded non-interactive commands", want: false},
|
|
{name: "content inspection read-only reason not classified as phase1", reason: "content inspection: read-only", want: false},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := isPhase1GuardrailFailure(tc.reason)
|
|
if got != tc.want {
|
|
t.Fatalf("isPhase1GuardrailFailure(%q) = %v, want %v", tc.reason, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetPhase1Hint(t *testing.T) {
|
|
const base = "REASON" // arbitrary non-empty base hint to verify concatenation
|
|
tests := []struct {
|
|
name string
|
|
reason string
|
|
baseHint string
|
|
want string
|
|
}{
|
|
// One case per switch arm (in source order).
|
|
{name: "sudo arm", reason: "sudo escalates", baseHint: base, want: base + ". Read-only execution does not accept sudo; privileged operations require the governed control path."},
|
|
{name: "redirect arm via redirection substring", reason: "output redirection detected", baseHint: base, want: base + ". Read-only execution does not accept redirects (>, >>, <, <<, <<<)."},
|
|
{name: "tee arm", reason: "tee can write to files", baseHint: base, want: base + ". Read-only execution does not accept tee because tee writes to files."},
|
|
{name: "substitution arm", reason: "command substitution detected", baseHint: base, want: base + ". Read-only execution does not accept $() or backticks."},
|
|
{name: "chaining arm", reason: "shell chaining detected", baseHint: base, want: base + ". Run commands separately instead of chaining with ; && ||."},
|
|
{name: "piped input arm", reason: "piped input to dual-use tool", baseHint: base, want: base + ". Read-only execution does not pipe input into interpreters or database clients."},
|
|
{name: "TTY arm", reason: "interactive/TTY flags", baseHint: base, want: base + ". Remove -it/--tty/--interactive flags. Use non-interactive form: docker exec container cmd (not docker exec -it)."},
|
|
{name: "terminal arm without TTY", reason: "requires terminal interaction", baseHint: base, want: base + ". Remove -it/--tty/--interactive flags. Use non-interactive form: docker exec container cmd (not docker exec -it)."},
|
|
{name: "pager arm", reason: "pager tools", baseHint: base, want: base + ". Use cat, head -n, or tail -n instead of interactive tools."},
|
|
{name: "editor arm without pager", reason: "editor tools", baseHint: base, want: base + ". Use cat, head -n, or tail -n instead of interactive tools."},
|
|
{name: "indefinitely arm", reason: "runs indefinitely", baseHint: base, want: base + ". Use bounded alternatives: ps aux (not top), journalctl -n 100 (not watch)."},
|
|
{name: "unbounded arm", reason: "unbounded stream", baseHint: base, want: base + ". Add line limit: journalctl -n 100 -f or tail -n 50 -f, or wrap with timeout."},
|
|
{name: "streaming arm without unbounded", reason: "streaming follow mode", baseHint: base, want: base + ". Add line limit: journalctl -n 100 -f or tail -n 50 -f, or wrap with timeout."},
|
|
|
|
// default / fallthrough arm.
|
|
{name: "default arm with unmatched reason", reason: "unknown phase1 failure", baseHint: base, want: base + ". Read-only execution does not accept redirects, chaining, sudo, or subshells."},
|
|
{name: "default arm with empty reason", reason: "", baseHint: base, want: base + ". Read-only execution does not accept redirects, chaining, sudo, or subshells."},
|
|
|
|
// Switch-order precedence (first matching case wins).
|
|
{name: "precedence sudo before redirect", reason: "sudo and redirect", baseHint: base, want: base + ". Read-only execution does not accept sudo; privileged operations require the governed control path."},
|
|
{name: "precedence redirect before tee", reason: "redirect and tee", baseHint: base, want: base + ". Read-only execution does not accept redirects (>, >>, <, <<, <<<)."},
|
|
{name: "precedence piped input before TTY", reason: "piped input and TTY flags", baseHint: base, want: base + ". Read-only execution does not pipe input into interpreters or database clients."},
|
|
{name: "precedence indefinitely before unbounded", reason: "indefinitely and unbounded", baseHint: base, want: base + ". Use bounded alternatives: ps aux (not top), journalctl -n 100 (not watch)."},
|
|
|
|
// Empty base hint (still gets the suffix).
|
|
{name: "empty base hint sudo arm", reason: "sudo", baseHint: "", want: ". Read-only execution does not accept sudo; privileged operations require the governed control path."},
|
|
{name: "empty base hint default arm", reason: "unmatched", baseHint: "", want: ". Read-only execution does not accept redirects, chaining, sudo, or subshells."},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := getPhase1Hint(tc.reason, tc.baseHint)
|
|
if got != tc.want {
|
|
t.Fatalf("getPhase1Hint(%q, %q) =\n %q\nwant\n %q",
|
|
tc.reason, tc.baseHint, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetSQLHint(t *testing.T) {
|
|
for _, base := range []string{"REASON", ""} {
|
|
want := base + ". pulse_read does not run database clients; use the governed control path, or read the service's logs and status instead."
|
|
if got := getSQLHint(base); got != want {
|
|
t.Fatalf("getSQLHint(%q) =\n %q\nwant\n %q", base, got, want)
|
|
}
|
|
}
|
|
}
|