forked from longtao.wu/droboticflow
139 lines
4.8 KiB
Go
139 lines
4.8 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"github.com/go-chi/chi/v5"
|
|
"go.mongodb.org/mongo-driver/event"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDiagnosticTraceRedactsAndPreservesHTTP(t *testing.T) {
|
|
var logs bytes.Buffer
|
|
old := log.Writer()
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(old)
|
|
handler := diagnosticTrace(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if _, ok := w.(http.Flusher); !ok {
|
|
t.Error("streaming support lost")
|
|
}
|
|
w.Header().Set("Location", "https://storage.invalid/video?authorization=secret")
|
|
w.WriteHeader(307)
|
|
}))
|
|
request := httptest.NewRequest("GET", "/api/private-customer-id?password=secret", nil)
|
|
request.Header.Set("X-Diagnostic-Request", "1")
|
|
request.Header.Set("X-Request-ID", "attacker-controlled")
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, request)
|
|
id := response.Header().Get("X-Request-ID")
|
|
if !regexp.MustCompile(`^[a-f0-9]{32}$`).MatchString(id) {
|
|
t.Fatalf("unexpected id %q", id)
|
|
}
|
|
if response.Code != 307 || response.Header().Get("Location") == "" {
|
|
t.Fatal("HTTP response changed")
|
|
}
|
|
if !strings.Contains(logs.String(), id) || !strings.Contains(logs.String(), "status=307") {
|
|
t.Fatal("missing trace")
|
|
}
|
|
for _, secret := range []string{"secret", "private-customer", "storage.invalid", "attacker"} {
|
|
if strings.Contains(logs.String(), secret) {
|
|
t.Fatal("sensitive data logged")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDiagnosticTraceIncludesLoginFailures(t *testing.T) {
|
|
var logs bytes.Buffer
|
|
old := log.Writer()
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(old)
|
|
handler := diagnosticTrace(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(401) }))
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest("POST", "/api/auth/login", strings.NewReader(`{"password":"secret"}`)))
|
|
if !strings.Contains(logs.String(), "kind=login status=401") || strings.Contains(logs.String(), "secret") {
|
|
t.Fatal("invalid login trace")
|
|
}
|
|
first := response.Header().Get("X-Request-ID")
|
|
response = httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest("GET", "/api/auth/me", nil))
|
|
if first == response.Header().Get("X-Request-ID") {
|
|
t.Fatal("reused ID")
|
|
}
|
|
if strings.Count(logs.String(), "request_trace") != 1 {
|
|
t.Fatal("ordinary requests should not generate diagnostic logs")
|
|
}
|
|
}
|
|
|
|
func TestDiagnosticTaskSummaryIsScopedAndReadOnly(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
path := "/api/human-items/" + f.item.ID + "?view=diagnostics"
|
|
r := f.request(http.MethodGet, path, f.annotator, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("authorized: %d %s", r.Code, r.Body.String())
|
|
}
|
|
var value map[string]map[string]any
|
|
if err := json.Unmarshal(r.Body.Bytes(), &value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(value) != 1 || len(value["item"]) != 2 || value["item"]["id"] != f.item.ID {
|
|
t.Fatal("summary contains extra customer data")
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("diagnostics mutated task")
|
|
}
|
|
other := f.annotator
|
|
other.ID = "unrelated-user"
|
|
denied := f.request(http.MethodGet, path, other, nil)
|
|
if denied.Code != 403 {
|
|
t.Fatalf("unrelated user: %d", denied.Code)
|
|
}
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
anonymous := httptest.NewRecorder()
|
|
NewHandler(f.store, nil).ServeHTTP(anonymous, httptest.NewRequest("GET", path, nil))
|
|
if anonymous.Code != 401 {
|
|
t.Fatalf("anonymous: %d", anonymous.Code)
|
|
}
|
|
}
|
|
|
|
func TestDiagnosticTraceCorrelatesReadPhasesWithoutQueryOrResourceIDs(t *testing.T) {
|
|
var logs bytes.Buffer
|
|
old := log.Writer()
|
|
log.SetOutput(&logs)
|
|
defer log.SetOutput(old)
|
|
router := chi.NewRouter()
|
|
router.Use(diagnosticTrace)
|
|
router.Get("/api/tasks/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
if store.ReadRequestID(r.Context()) == "" {
|
|
t.Fatal("missing request context")
|
|
}
|
|
finish := store.ObserveReadPhase(r.Context(), "candidate_query")
|
|
finish()
|
|
store.ReadCommandMonitor().Failed(r.Context(), &event.CommandFailedEvent{Failure: "i/o timeout private-body"})
|
|
w.WriteHeader(500)
|
|
})
|
|
req := httptest.NewRequest("GET", "/api/tasks/private-id?search=secret-annotation", nil)
|
|
req.Header.Set("X-Request-ID", "caller-id")
|
|
resp := httptest.NewRecorder()
|
|
router.ServeHTTP(resp, req)
|
|
text := logs.String()
|
|
for _, value := range []string{"private-id", "secret-annotation", "private-body", "caller-id"} {
|
|
if strings.Contains(text, value) {
|
|
t.Fatal("sensitive request information leaked")
|
|
}
|
|
}
|
|
for _, value := range []string{"/api/tasks/{id}", "candidate_query", "mongo_failures", "client_or_network_deadline", resp.Header().Get("X-Request-ID")} {
|
|
if !strings.Contains(text, value) {
|
|
t.Fatalf("missing diagnostic: %s", value)
|
|
}
|
|
}
|
|
}
|