Files
uptimesebi/internal/api/evidence_test.go
Sebastian UnterschützandClaude Opus 5.5 126d3a2fce feat: show monitor editors what the checker saw
The API keeps the latest success per monitor and region and failures,
thinned to one per five minutes while the error stays the same, for
UPTIME_RETENTION_EVIDENCE (30 days). Monitor pages link evidence from
the recent checks; a page shows timing, redirect chain with headers,
certificate and the body as text. Readers see none of it, since
responses may contain personal data or secrets.

Closes #79

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 12:09:42 +01:00

106 lines
4.0 KiB
Go

package api_test
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
)
func TestEvidence(t *testing.T) {
e := newEnv(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go e.api.RunScheduler(ctx)
var healthy atomic.Bool
site := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "/app", http.StatusFound)
return
}
http.SetCookie(w, &http.Cookie{Name: "sid", Value: "cookie-secret"})
if !healthy.Load() {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`<script>alert("pwned")</script> upstream down`))
return
}
_, _ = w.Write([]byte("all good"))
}))
defer site.Close()
tok, region := e.platformChecker(t, uniq("eu-"))
e.startChecker(t, tok)
owner := e.browser(t)
owner.signup("o@"+uniq("ev")+".test", uniq("ev-"))
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
"name": "Shop", "type": "http", "target": site.URL + "/", "interval_seconds": 10, "timeout_ms": 2000,
"confirmations": 1, "regions": []string{region}, "config": map[string]any{"verify_tls": false, "headers": map[string]string{"X-Token": "header-secret"}},
})
if st != http.StatusCreated {
t.Fatalf("create monitor = %d %v", st, body)
}
id := body["id"].(string)
evidence := func() []any {
_, b := owner.do("GET", "/api/v1/org/monitors/"+id+"/evidence", nil)
list, _ := b["evidence"].([]any)
return list
}
waitFor(t, 15*time.Second, "evidence of the failure", func() bool { return len(evidence()) > 0 })
first := evidence()[0].(map[string]any)
_, detail := owner.do("GET", "/api/v1/org/monitors/"+id+"/evidence/"+first["id"].(string), nil)
raw := mustJSON(detail)
for _, want := range []string{`"status":302`, `"status":502`, `"not_after"`, `upstream down`} {
if !strings.Contains(raw, want) {
t.Errorf("evidence lacks %s: %.600s", want, raw)
}
}
steps := detail["evidence"].(map[string]any)["steps"].([]any)
cookie := steps[len(steps)-1].(map[string]any)["response_headers"].(map[string]any)["Set-Cookie"].([]any)
if len(cookie) != 1 || cookie[0] != "sid=<redacted>" {
t.Errorf("Set-Cookie = %v, want the name only", cookie)
}
for _, secret := range []string{"cookie-secret", "header-secret"} {
if strings.Contains(raw, secret) {
t.Errorf("evidence contains %s", secret)
}
}
// The page shows the body as text, never as markup.
_, _, page := owner.page("/monitors/" + id + "/evidence/" + first["id"].(string))
if strings.Contains(page, `<script>alert("pwned")`) || !strings.Contains(page, "&lt;script&gt;") {
t.Error("the evidence page renders the response body as HTML")
}
if !strings.Contains(page, "502 Bad Gateway") || !strings.Contains(page, "/app") {
t.Error("the evidence page lacks the redirect chain")
}
if _, _, mp := owner.page("/monitors/" + id); !strings.Contains(mp, "/evidence/") {
t.Error("the monitor page does not link the evidence")
}
// A lasting outage with the same error is kept once.
time.Sleep(12 * time.Second)
if n := e.count(t, `SELECT count(*) FROM check_evidence WHERE monitor_id = $1 AND NOT ok`, id); n != 1 {
t.Errorf("failure evidence rows = %d, want 1", n)
}
healthy.Store(true)
waitFor(t, 25*time.Second, "evidence of a success", func() bool {
return e.count(t, `SELECT count(*) FROM check_evidence WHERE monitor_id = $1 AND ok`, id) == 1
})
// Readers get neither the list nor the page.
viewer := e.joinVia(t, owner.invite("v@"+uniq("ev")+".test", owner.roleID("viewer")))
if st, _ := viewer.do("GET", "/api/v1/org/monitors/"+id+"/evidence", nil); st != http.StatusForbidden {
t.Errorf("viewer lists evidence = %d, want 403", st)
}
if got := viewer.visit("/monitors/" + id + "/evidence/" + first["id"].(string)); got.Path != "/" {
t.Errorf("viewer opening evidence ended at %s, want /", got.Path)
}
if _, _, vp := viewer.page("/monitors/" + id); strings.Contains(vp, "/evidence/") {
t.Error("the monitor page links evidence for a viewer")
}
}