With Spec.Capture, HTTP, JSON and Prometheus checks record every request and response of the redirect chain, the time spent per phase, the remote address, the server certificate and the first 32 KiB of the body. Configured headers, credentials and cookie values are redacted. The certificate is recorded before verification, which is now done in VerifyConnection the way crypto/tls does it, because crypto/tls drops the certificate exactly when verification fails. Refs #79 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
123 lines
4.5 KiB
Go
123 lines
4.5 KiB
Go
package check
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestEvidence(t *testing.T) {
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/old":
|
|
http.Redirect(w, r, "/new", http.StatusMovedPermanently)
|
|
case "/new":
|
|
http.SetCookie(w, &http.Cookie{Name: "session", Value: "very-secret"})
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
_, _ = w.Write([]byte("<h1>Maintenance</h1>" + strings.Repeat("x", 40<<10)))
|
|
case "/binary":
|
|
_, _ = w.Write([]byte{0x89, 'P', 'N', 'G', 0, 0, 1})
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
run := func(path string, cfg map[string]any) Outcome {
|
|
t.Helper()
|
|
raw, _ := json.Marshal(cfg)
|
|
c, err := Validate(HTTP, srv.URL+path, raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return Run(context.Background(), Spec{Type: HTTP, Target: srv.URL + path, Timeout: 3 * time.Second, Config: c, AllowPrivate: true, Capture: true})
|
|
}
|
|
|
|
o := run("/old", map[string]any{"verify_tls": false, "headers": map[string]string{"X-Api-Key": "k3y"}})
|
|
ev := o.Evidence
|
|
if o.OK || ev == nil {
|
|
t.Fatalf("Run() = %+v, want a failure with evidence", o)
|
|
}
|
|
if len(ev.Steps) != 2 || ev.Steps[0].Status != http.StatusMovedPermanently || !strings.HasSuffix(ev.Steps[1].URL, "/new") || ev.Steps[1].Status != http.StatusServiceUnavailable {
|
|
t.Errorf("steps = %+v, want the redirect and the final response", ev.Steps)
|
|
}
|
|
if got := ev.Steps[0].RequestHeaders["X-Api-Key"]; len(got) != 1 || got[0] != redacted {
|
|
t.Errorf("configured header = %v, want redacted", got)
|
|
}
|
|
if got := ev.Steps[1].ResponseHeaders["Set-Cookie"]; len(got) != 1 || got[0] != "session="+redacted {
|
|
t.Errorf("Set-Cookie = %v, want the name only", got)
|
|
}
|
|
if !strings.HasPrefix(ev.Body, "<h1>Maintenance</h1>") || len(ev.Body) != maxEvidenceBody || !ev.BodyTruncated || ev.ContentType != "text/html" {
|
|
t.Errorf("body = %d bytes, truncated %t, type %q", len(ev.Body), ev.BodyTruncated, ev.ContentType)
|
|
}
|
|
if ev.TLS == nil || ev.TLS.Version == "" || ev.TLS.NotAfter.IsZero() || ev.RemoteAddr == "" {
|
|
t.Errorf("tls = %+v, remote = %q", ev.TLS, ev.RemoteAddr)
|
|
}
|
|
if ev.Timing.Total <= 0 || ev.Timing.Total < ev.Timing.Wait {
|
|
t.Errorf("timing = %+v", ev.Timing)
|
|
}
|
|
|
|
// Verification failures still show the certificate that caused them.
|
|
o = run("/new", map[string]any{"verify_tls": true})
|
|
if o.Evidence == nil || o.Evidence.TLS == nil || o.Evidence.FailedAt != "tls" {
|
|
t.Errorf("verification failure evidence = %+v", o.Evidence)
|
|
}
|
|
|
|
if o = run("/binary", map[string]any{"verify_tls": false}); !o.Evidence.BodyBinary || o.Evidence.Body != "" {
|
|
t.Errorf("binary body = %+v", o.Evidence)
|
|
}
|
|
|
|
// Refused connections stop at connect.
|
|
dead := httptest.NewServer(http.NotFoundHandler())
|
|
url := dead.URL
|
|
dead.Close()
|
|
raw, _ := Validate(HTTP, url, nil)
|
|
o = Run(context.Background(), Spec{Type: HTTP, Target: url, Timeout: time.Second, Config: raw, AllowPrivate: true, Capture: true})
|
|
if o.Evidence == nil || o.Evidence.FailedAt != "connect" || len(o.Evidence.Steps) != 1 {
|
|
t.Errorf("refused connection evidence = %+v", o.Evidence)
|
|
}
|
|
|
|
// Without Capture nothing is recorded.
|
|
raw, _ = Validate(HTTP, srv.URL+"/new", json.RawMessage(`{"verify_tls":false}`))
|
|
if o = Run(context.Background(), Spec{Type: HTTP, Target: srv.URL + "/new", Timeout: time.Second, Config: raw, AllowPrivate: true}); o.Evidence != nil {
|
|
t.Error("evidence recorded without Capture")
|
|
}
|
|
}
|
|
|
|
// recordingTLS replaces crypto/tls verification, so it must reject what
|
|
// crypto/tls rejects.
|
|
func TestRecordingTLSVerifies(t *testing.T) {
|
|
srv := httptest.NewTLSServer(http.NotFoundHandler())
|
|
defer srv.Close()
|
|
roots := x509.NewCertPool()
|
|
roots.AddCert(srv.Certificate())
|
|
handshake := func(serverName string, trusted, verify bool) error {
|
|
cfg := recordingTLS(newRecorder(), verify)
|
|
if trusted {
|
|
cfg.RootCAs = roots
|
|
}
|
|
cfg.ServerName = serverName
|
|
conn, err := tls.Dial("tcp", srv.Listener.Addr().String(), cfg)
|
|
if err == nil {
|
|
_ = conn.Close()
|
|
}
|
|
return err
|
|
}
|
|
if err := handshake("example.com", true, true); err != nil {
|
|
t.Errorf("valid name and trusted root rejected: %v", err)
|
|
}
|
|
if err := handshake("evil.test", true, true); err == nil {
|
|
t.Error("wrong host name accepted")
|
|
}
|
|
if err := handshake("example.com", false, true); err == nil {
|
|
t.Error("untrusted certificate accepted")
|
|
}
|
|
if err := handshake("evil.test", false, false); err != nil {
|
|
t.Errorf("opted-out verification still rejects: %v", err)
|
|
}
|
|
}
|