From 68716b977bf723b2697f40300805d3346bea13e9 Mon Sep 17 00:00:00 2001 From: Kharec Date: Sun, 23 Nov 2025 15:01:54 +0100 Subject: [PATCH] fix: verify XSS sanitization in handler response instead of repository stub --- internal/handlers/security_test.go | 38 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/internal/handlers/security_test.go b/internal/handlers/security_test.go index a9e37d3..fce7492 100644 --- a/internal/handlers/security_test.go +++ b/internal/handlers/security_test.go @@ -24,10 +24,6 @@ func TestPostHandler_XSSProtection_Comprehensive(t *testing.T) { t.Run("XSS_"+payload[:minLen(20, len(payload))], func(t *testing.T) { repo := &testutils.PostRepositoryStub{ CreateFn: func(post *database.Post) error { - sanitizedTitle := security.SanitizeInput(payload) - if post.Title != sanitizedTitle { - t.Errorf("Expected sanitized title, got %q", post.Title) - } return nil }, } @@ -48,7 +44,39 @@ func TestPostHandler_XSSProtection_Comprehensive(t *testing.T) { handler.CreatePost(recorder, request) - testutils.AssertHTTPStatus(t, recorder, http.StatusCreated) + if recorder.Code != http.StatusCreated { + t.Errorf("Expected status %d, got %d. Body: %s", http.StatusCreated, recorder.Code, recorder.Body.String()) + return + } + + var response CommonResponse + if err := json.NewDecoder(recorder.Body).Decode(&response); err != nil { + t.Fatalf("Failed to decode response: %v", err) + } + + if !response.Success { + t.Errorf("Expected successful response, got error: %s", response.Error) + return + } + + dataMap, ok := response.Data.(map[string]any) + if !ok { + t.Fatalf("Expected data to be a map, got %T", response.Data) + } + + title, ok := dataMap["title"].(string) + if !ok { + t.Fatalf("Expected title to be a string, got %T", dataMap["title"]) + } + + expectedSanitized := security.SanitizeInput(payload) + if title != expectedSanitized { + t.Errorf("Expected sanitized title %q, got %q", expectedSanitized, title) + } + + if title == payload { + t.Errorf("Title was not sanitized - original payload %q matches response %q", payload, title) + } }) } }