Skip to content

test: add fuzz test for file handler error paths - #5539

Open
taeyoung0823 wants to merge 8 commits into
litmuschaos:masterfrom
taeyoung0823:test/add-file-handler-fuzz-tests
Open

test: add fuzz test for file handler error paths#5539
taeyoung0823 wants to merge 8 commits into
litmuschaos:masterfrom
taeyoung0823:test/add-file-handler-fuzz-tests

Conversation

@taeyoung0823

Copy link
Copy Markdown

Summary

Fixes #5538

This PR adds fuzz test coverage for the file handler manifest endpoint and ensures the handler returns immediately after writing error responses.

Changes

  • Added early returns after error responses in FileHandler
  • Added fuzz tests for file handler key and Referer inputs
  • Covered malformed keys, empty keys, path-like inputs, and invalid Referer values
  • Ensured the handler does not panic on unexpected inputs

Testing

go test ./pkg/handlers/...
go test ./pkg/handlers/... -run=FuzzFileHandler -fuzz=FuzzFileHandler -fuzztime=30s

@PriteshKiri

Copy link
Copy Markdown
Contributor

Hey @taeyoung0823
Could you please check the CI failures?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to harden the /file/:key manifest endpoint by ensuring FileHandler stops execution immediately after writing error responses, and adds a fuzz test to exercise error paths and prevent panics on unexpected inputs.

Changes:

  • Added return statements after multiple error responses in FileHandler.
  • Added a new fuzz test targeting FileHandler with varied key and Referer inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
chaoscenter/graphql/server/pkg/handlers/file_handler.go Adds early returns after error writes to prevent continued execution after an error response.
chaoscenter/graphql/server/pkg/handlers/file_handler_fuzz_test.go Introduces fuzzing for the file handler to catch panics and unexpected behavior on malformed inputs.
Comments suppressed due to low confidence (1)

chaoscenter/graphql/server/pkg/handlers/file_handler.go:48

  • /file/:key is registered without the authorization middleware that injects request-header into the request context (see server.go), so relying on c.Value("request-header") is unlikely to work for real requests and will force the handler into the 500 path. In a gin handler you can read headers directly from c.Request.Header / c.GetHeader(...).
		reqHeader, ok := c.Value("request-header").(http.Header)
		if !ok {
			logrus.Error("unable to parse referer header")
			utils.WriteHeaders(&c.Writer, 500)
			c.Writer.Write([]byte("unable to parse referer header"))
			return
		}

		referrer := reqHeader.Get("Referer")
		if referrer == "" {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

},
}

ctx.Set("request-header", req.Header)
Comment on lines +32 to +33
gin.SetMode(gin.TestMode)

@PriteshKiri

Copy link
Copy Markdown
Contributor

Hey @taeyoung0823
Could you please check the Co-Pilot review comments?

@taeyoung0823

Copy link
Copy Markdown
Author

Thanks for the review @PriteshKiri

I addressed the Copilot review comments by updating FileHandler to read the Referer header directly from the Gin request using c.GetHeader("Referer").

I also updated the fuzz test by removing the request-header context setup, moving gin.SetMode(gin.TestMode) outside the fuzz loop, and skipping JWT-shaped keys that can trigger auth configuration / Mongo access during fuzzing.

The changes have been pushed. Could you please approve the pending workflows when you get a chance?

@taeyoung0823
taeyoung0823 force-pushed the test/add-file-handler-fuzz-tests branch 2 times, most recently from 3928e3f to 3c2dbbc Compare June 18, 2026 00:44
@PriteshKiri
PriteshKiri requested a review from Copilot June 30, 2026 09:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

@@ -0,0 +1,66 @@
**package handlers**
Comment on lines +34 to +56
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)

req := httptest.NewRequest(http.MethodGet, "/file/"+url.PathEscape(key), nil)
req.Header.Set("Referer", referer)
ctx.Request = req

ctx.Params = gin.Params{
{
Key: "key",
Value: key,
},
}

mockOp := new(mocks.MongoOperator)

defer func() {
if r := recover(); r != nil {
t.Fatalf("FileHandler panicked with key=%q referer=%q: %v", key, referer, r)
}
}()

FileHandler(mockOp)(ctx)
Comment on lines +4 to +8
@@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"strings"
"html"
@PriteshKiri

Copy link
Copy Markdown
Contributor

@taeyoung0823 could you please resolve the conflicts?

Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
@taeyoung0823
taeyoung0823 force-pushed the test/add-file-handler-fuzz-tests branch from 72e0f66 to 8366840 Compare July 6, 2026 22:25
Comment thread chaoscenter/graphql/server/pkg/handlers/file_handler.go Fixed
Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
@taeyoung0823
taeyoung0823 force-pushed the test/add-file-handler-fuzz-tests branch from f8b889e to f4c96b2 Compare July 7, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment on lines +48 to +56
mockOp := new(mocks.MongoOperator)

defer func() {
if r := recover(); r != nil {
t.Fatalf("FileHandler panicked with key=%q referer=%q: %v", key, referer, r)
}
}()

FileHandler(mockOp)(ctx)
Comment on lines +38 to +39
req.Header.Set("Referer", referer)
ctx.Request = req
@PriteshKiri

Copy link
Copy Markdown
Contributor

Hey @taeyoung0823
Could you please review the comments by Co-Pilot?

Also, some CI checks are failing. Could you please address those?

@PriteshKiri

Copy link
Copy Markdown
Contributor

@taeyoung0823 any updates?

Validate and escape Referer-derived endpoints before embedding them in generated manifests, and return the manifest with safe YAML download headers. Keep fuzz coverage deterministic by isolating Referer parsing from Mongo-backed JWT validation.

Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
@taeyoung0823

Copy link
Copy Markdown
Author

@PriteshKiri Please take a look when you have a chance.

@taeyoung0823

Copy link
Copy Markdown
Author

@PriteshKiri Could you please review this?

@PriteshKiri

Copy link
Copy Markdown
Contributor

Hey @taeyoung0823
Could you please look into the CI failures?

Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
@taeyoung0823

Copy link
Copy Markdown
Author

@PriteshKiri Please take a look when you have a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add fuzz tests and early returns for file handler error paths

5 participants