Files
zero/internal/notify/slack_test.go
KRATOS abcb1107d9 wedge stage 11: GitHub Action + Slack/webhook notifier (#182)
* stage 11: GitHub Action + Slack/webhook notifier

Add a distribution channel and an output sink for unattended ZERO runs.

internal/notify:
- Add a Sink interface and Notifier fan-out. Notify now emits the terminal
  bell/OSC-9 sequence AND forwards every eligible event to attached sinks.
  Sinks fire even with no terminal writer (headless CI) and are gated by the
  same mode/focus policy. Sink invocation happens outside the lock and a sink
  panic is isolated, so a misbehaving or slow sink can never crash or stall the
  run. New(w, cfg) stays backward compatible.
- Add a WebhookSink (slack.go) that POSTs a JSON payload
  {text, type, message, summary?, links?} to a Slack incoming webhook or any
  generic webhook. Delivery is best-effort and fails soft: non-2xx and
  transport errors are logged (redacted) and swallowed. The message, summary,
  links, and the webhook URL are run through redaction before send/log so
  tokens never leak. The default HTTP transport honors HTTP(S)_PROXY, so a
  sandboxed run's scoped-egress policy is respected. An empty URL is inert.

action.yml:
- Composite GitHub Action wrapping zero exec. Provider-agnostic: provider,
  api-key, and api-key-env are inputs (no provider is hardcoded). Installs the
  pinned ZERO release via the bundled checksum-verifying installer, runs the
  prompt in the checked-out repo, surfaces ZERO's exit code as the step status,
  captures stdout, and can post a summary to the PR or to Slack. Safety
  defaults: auto=low, sandbox always active, never passes
  --skip-permissions-unsafe; secrets are passed via env and never echoed.

docs/GITHUB_ACTION.md: input/output tables, copy-paste workflows (issue-labeled
triage and nightly dep-upgrade PR), security notes, and the notifier docs.

.github/workflows/zero-action-smoke.yml: validates action.yml parses, declares
the documented inputs/outputs, keeps the conservative defaults, never enables
unsafe mode, and shell-syntax-checks the embedded run blocks.

* stage 11: reword action.yml comment so the unsafe-flag validation passes

The composite step's shell comment contained the literal '--skip-permissions-unsafe', which the action-yml validation (assert the string never appears in the serialized doc, to prove the flag is never passed) flagged even though it was only descriptive. Reworded the comment to drop the literal; the action still never passes the flag and the check stays strict.

* stage 11: address review for action env/inputs and webhook URL redaction

- action.yml: reject prompt+prompt-file when both set; trim only surrounding
  whitespace from add-dir entries (preserve internal spaces); export api-key
  in-process (validated env name) so the run sees it; treat a commit-SHA
  action_ref as not-a-tag and fall back to latest.
- notify: redact the webhook URL in outbound payload fields (not just logs);
  copy caller-owned Links/ExtraSecrets slices in the constructor.

* stage 11: disable credential persistence in action smoke checkout
2026-06-13 10:42:53 +05:30

256 lines
8.0 KiB
Go

package notify
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
)
// captureLog returns a thread-safe Logf and a getter for the joined output.
func captureLog() (func(string, ...any), func() string) {
var mu sync.Mutex
var lines []string
logf := func(format string, args ...any) {
mu.Lock()
defer mu.Unlock()
lines = append(lines, fmt.Sprintf(format, args...))
}
get := func() string {
mu.Lock()
defer mu.Unlock()
return strings.Join(lines, "\n")
}
return logf, get
}
func TestWebhookSinkPostsExpectedJSON(t *testing.T) {
type received struct {
method string
contentType string
body webhookPayload
raw string
}
got := make(chan received, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
var payload webhookPayload
_ = json.Unmarshal(raw, &payload)
got <- received{method: r.Method, contentType: r.Header.Get("Content-Type"), body: payload, raw: string(raw)}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
sink := NewWebhookSink(WebhookConfig{URL: server.URL})
sink.Emit(Completion, "Zero: ready")
select {
case r := <-got:
if r.method != http.MethodPost {
t.Fatalf("method = %q, want POST", r.method)
}
if !strings.HasPrefix(r.contentType, "application/json") {
t.Fatalf("content-type = %q, want application/json", r.contentType)
}
if r.body.Type != "completion" {
t.Fatalf("payload type = %q, want completion", r.body.Type)
}
if r.body.Message != "Zero: ready" {
t.Fatalf("payload message = %q, want %q", r.body.Message, "Zero: ready")
}
// Slack incoming-webhook compatibility: a human-readable "text" field must
// be present so the message renders in a Slack channel.
if strings.TrimSpace(r.body.Text) == "" {
t.Fatalf("payload text was empty; raw=%s", r.raw)
}
case <-time.After(2 * time.Second):
t.Fatal("webhook server never received a request")
}
}
func TestWebhookSinkIncludesSummaryAndLinks(t *testing.T) {
got := make(chan webhookPayload, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var payload webhookPayload
_ = json.NewDecoder(r.Body).Decode(&payload)
got <- payload
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
sink := NewWebhookSink(WebhookConfig{
URL: server.URL,
Summary: "verify failed after 3 retries",
Links: []WebhookLink{{Label: "Run", URL: "https://example.test/run/1"}},
})
sink.Emit(AwaitingInput, "Zero: needs input")
select {
case payload := <-got:
if payload.Type != "awaiting_input" {
t.Fatalf("type = %q, want awaiting_input", payload.Type)
}
if payload.Summary != "verify failed after 3 retries" {
t.Fatalf("summary = %q", payload.Summary)
}
if len(payload.Links) != 1 || payload.Links[0].URL != "https://example.test/run/1" {
t.Fatalf("links = %+v", payload.Links)
}
case <-time.After(2 * time.Second):
t.Fatal("webhook server never received a request")
}
}
func TestWebhookSinkNon2xxDoesNotPanicAndLogs(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "boom", http.StatusInternalServerError)
}))
defer server.Close()
logf, getLog := captureLog()
sink := NewWebhookSink(WebhookConfig{URL: server.URL, Logf: logf})
// Must not panic and must not block the caller fatally.
sink.Emit(Completion, "hi")
if log := getLog(); !strings.Contains(strings.ToLower(log), "webhook") || !strings.Contains(log, "500") {
t.Fatalf("expected a logged 500 webhook failure, got %q", log)
}
}
func TestWebhookSinkTransportErrorIsLoggedNotFatal(t *testing.T) {
logf, getLog := captureLog()
// An unroutable URL forces a transport error; Emit must swallow it.
sink := NewWebhookSink(WebhookConfig{
URL: "http://127.0.0.1:0/never",
Logf: logf,
Client: &http.Client{Timeout: 200 * time.Millisecond},
})
sink.Emit(Completion, "hi")
if log := getLog(); !strings.Contains(strings.ToLower(log), "webhook") {
t.Fatalf("expected a logged webhook transport error, got %q", log)
}
}
func TestWebhookSinkRedactsSecretsInPayloadAndLog(t *testing.T) {
const token = "xoxb-123456789012-abcdefghijklmno"
got := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
got <- string(raw)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
logf, getLog := captureLog()
sink := NewWebhookSink(WebhookConfig{URL: server.URL, Logf: logf})
sink.Emit(Completion, "leaking "+token+" oops")
select {
case raw := <-got:
if strings.Contains(raw, token) {
t.Fatalf("token leaked into webhook payload: %s", raw)
}
if !strings.Contains(raw, "[REDACTED]") {
t.Fatalf("expected redaction marker in payload, got %s", raw)
}
case <-time.After(2 * time.Second):
t.Fatal("webhook server never received a request")
}
if log := getLog(); strings.Contains(log, token) {
t.Fatalf("token leaked into log: %s", log)
}
}
func TestWebhookSinkRedactsWebhookURLInLog(t *testing.T) {
// A failing webhook whose URL itself carries a secret-looking token must not
// echo that token when the failure is logged.
logf, getLog := captureLog()
const secretURL = "http://127.0.0.1:0/services/T000/B000/xoxb-123456789012-secrettoken"
sink := NewWebhookSink(WebhookConfig{
URL: secretURL,
Logf: logf,
Client: &http.Client{Timeout: 200 * time.Millisecond},
})
sink.Emit(Completion, "hi")
if log := getLog(); strings.Contains(log, "xoxb-123456789012-secrettoken") {
t.Fatalf("webhook URL token leaked into log: %s", log)
}
}
func TestWebhookSinkEmptyURLIsNoop(t *testing.T) {
// A sink with no URL configured must do nothing (and never panic).
sink := NewWebhookSink(WebhookConfig{})
sink.Emit(Completion, "hi") // no server, no panic, returns.
}
func TestWebhookSinkRedactsWebhookURLInPayload(t *testing.T) {
// If the message or summary echoes the webhook URL (which carries a secret
// token), that URL must be redacted before it is sent in the payload.
got := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
got <- string(raw)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
sink := NewWebhookSink(WebhookConfig{
URL: server.URL,
Summary: "see " + server.URL + " for the run",
Links: []WebhookLink{{Label: "Hook", URL: server.URL}},
})
sink.Emit(Completion, "delivering to "+server.URL)
select {
case raw := <-got:
if strings.Contains(raw, server.URL) {
t.Fatalf("webhook URL leaked into payload: %s", raw)
}
if !strings.Contains(raw, "[REDACTED]") {
t.Fatalf("expected redaction marker in payload, got %s", raw)
}
case <-time.After(2 * time.Second):
t.Fatal("webhook server never received a request")
}
}
func TestWebhookSinkCopiesConfigSlices(t *testing.T) {
// The sink must own its links/secrets so a caller mutating the config slices
// after construction cannot alter (or race with) future payloads.
const token = "xoxb-123456789012-abcdefghijklmno"
got := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
raw, _ := io.ReadAll(r.Body)
got <- string(raw)
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
links := []WebhookLink{{Label: "Run", URL: "https://example.test/run/1"}}
secrets := []string{token}
sink := NewWebhookSink(WebhookConfig{URL: server.URL, Links: links, ExtraSecrets: secrets})
// Mutate the caller-owned slices after construction.
links[0] = WebhookLink{Label: "Hijacked", URL: "https://attacker.test"}
secrets[0] = "no-longer-redacted"
sink.Emit(Completion, "leaking "+token+" oops")
select {
case raw := <-got:
if strings.Contains(raw, "attacker.test") || strings.Contains(raw, "Hijacked") {
t.Fatalf("mutated link leaked into payload: %s", raw)
}
if strings.Contains(raw, token) {
t.Fatalf("token leaked despite original secret config: %s", raw)
}
case <-time.After(2 * time.Second):
t.Fatal("webhook server never received a request")
}
}