test: add fuzz test for file handler error paths - #5539
Conversation
9b5a9e9 to
7ca3bad
Compare
|
Hey @taeyoung0823 |
There was a problem hiding this comment.
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
returnstatements after multiple error responses inFileHandler. - Added a new fuzz test targeting
FileHandlerwith variedkeyandRefererinputs.
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/:keyis registered without the authorization middleware that injectsrequest-headerinto the request context (seeserver.go), so relying onc.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 fromc.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) |
| gin.SetMode(gin.TestMode) | ||
|
|
|
Hey @taeyoung0823 |
|
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? |
3928e3f to
3c2dbbc
Compare
| @@ -0,0 +1,66 @@ | |||
| **package handlers** | |||
| 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) |
| @@ -5,6 +5,7 @@ import ( | |||
| "net/http" | |||
| "net/url" | |||
| "strings" | |||
| "html" | |||
|
@taeyoung0823 could you please resolve the conflicts? |
Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
72e0f66 to
8366840
Compare
Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
f8b889e to
f4c96b2
Compare
| 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) |
| req.Header.Set("Referer", referer) | ||
| ctx.Request = req |
|
Hey @taeyoung0823 Also, some CI checks are failing. Could you please address those? |
|
@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>
|
@PriteshKiri Please take a look when you have a chance. |
|
@PriteshKiri Could you please review this? |
|
Hey @taeyoung0823 |
Signed-off-by: taeyoung0823 <kimxodud0823@naver.com>
|
@PriteshKiri Please take a look when you have a chance. |
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
FileHandlerTesting