Skip to content

Commit 67bc0b6

Browse files
committed
fix(Security): Added security control for body in /v1/api/purge for just allow JSON
1 parent a68d62e commit 67bc0b6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

internal/api/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ var (
2121
func PurgeHandler(ctx v1alpha1.Context) func(c *fiber.Ctx) error {
2222
return func(c *fiber.Ctx) error {
2323

24+
// Verify the Content-Type header
25+
if c.Get("Content-Type") != "application/json" {
26+
ctx.Logger.Error("Invalid content type")
27+
return c.Status(fiber.StatusBadRequest).JSON(map[string]string{
28+
"error": "Invalid content type",
29+
})
30+
}
31+
32+
// Verify body to be really a JSON
33+
if !json.Valid(c.Body()) {
34+
ctx.Logger.Error("Invalid JSON body")
35+
return c.Status(fiber.StatusBadRequest).JSON(map[string]string{
36+
"error": "Invalid JSON body",
37+
})
38+
}
39+
2440
// Parse the JSON body from the request and validate the body
2541
if err := c.BodyParser(&req); err != nil {
2642
ctx.Logger.Errorf("Failed to parse request: %v\n", err)

0 commit comments

Comments
 (0)