package middleware import ( "bytes" "log" "net/http" "net/http/httptest" "strings" "testing" ) func TestDAVLogging(t *testing.T) { var output bytes.Buffer previous := log.Writer() log.SetOutput(&output) t.Cleanup(func() { log.SetOutput(previous) }) for _, tc := range []struct { name, auth, outcome string status int }{ {"challenge", "", "missing", 401}, {"accepted", "basic", "basic_accepted", 204}, {"rejected", "basic", "basic_rejected", 401}, {"malformed", "malformed_basic", "invalid_authorization", 401}, {"bearer", "bearer", "session_accepted", 207}, {"multiple", "multiple", "multiple_authorization", 401}, } { t.Run(tc.name, func(t *testing.T) { output.Reset() r := httptest.NewRequest("OPTIONS", "https://example.test/_/api/v1/caldav/private-calendar/private-event.ics?token=private-query", strings.NewReader("private-body")) switch tc.auth { case "basic", "multiple": r.SetBasicAuth("private-username", "private-password") if tc.auth == "multiple" { r.Header.Add("Authorization", "Bearer private-token") } case "malformed_basic": r.Header.Set("Authorization", "Basic private-malformed") case "bearer": r.Header.Set("Authorization", "Bearer private-token") } r.Header.Set("Cookie", "private-cookie") r.Header.Set("User-Agent", "private-user-agent") w := httptest.NewRecorder() Logging(RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { SetDAVAuthOutcome(r, tc.outcome) SetDAVAuthOutcome(r, "private-injected-outcome") // Not allowlisted. if tc.status == 401 { w.Header().Set("WWW-Authenticate", `Basic realm="test"`) } if tc.status == 204 { w.Header().Set("X-PagesWiki-CalDAV-Profile", "limited-compatibility") } w.WriteHeader(tc.status) }))).ServeHTTP(w, r) line := output.String() auth := tc.auth if auth == "" { auth = "none" } for _, wanted := range []string{"caldav request", "target=calendar_resource", "auth=" + auth, "outcome=" + tc.outcome, w.Header().Get("X-Request-ID")} { if !strings.Contains(line, wanted) { t.Fatalf("missing %q in %q", wanted, line) } } if strings.Contains(line, "private-") || strings.Contains(line, "/_/api/") || (r.Header.Get("Authorization") != "" && strings.Contains(line, r.Header.Get("Authorization"))) { t.Fatalf("sensitive metadata in log: %q", line) } if w.Code != tc.status { t.Fatalf("status changed: %d", w.Code) } }) } } func TestDAVLogTarget(t *testing.T) { for path, want := range map[string]string{ "/_/api/v1/caldav": "home", "/_/api/v1/caldav/": "home", "/_/api/v1/caldav/tasks/": "task_home", "/_/api/v1/caldav/tasks/default": "task_collection", "/_/api/v1/caldav/tasks/default/private.ics": "task_resource", "/_/api/v1/caldav/private/": "calendar_collection", "/.well-known/caldav": "well_known", "/_/api/v1/caldav-credentials": "", "/health": "", } { if got := davLogTarget(path); got != want { t.Errorf("%s: %q != %q", path, got, want) } } } func TestDAVLogWriterStatus(t *testing.T) { w := &davLogWriter{ResponseWriter: httptest.NewRecorder()} w.WriteHeader(103) if w.status != 0 { t.Fatal("informational status recorded as final") } w.Write([]byte("synthetic")) w.WriteHeader(500) if w.status != 200 { t.Fatal("implicit response or duplicate header changed status", w.status) } if w.Unwrap() == nil { t.Fatal("response controller cannot unwrap") } }