Skip to content

Limit picod request body size for prevent DoS attack#326

Open
RainbowMango wants to merge 1 commit into
volcano-sh:mainfrom
RainbowMango:pr_limit_picod_request_body
Open

Limit picod request body size for prevent DoS attack#326
RainbowMango wants to merge 1 commit into
volcano-sh:mainfrom
RainbowMango:pr_limit_picod_request_body

Conversation

@RainbowMango
Copy link
Copy Markdown

What type of PR is this?

/kind cleanup
/kind security

What this PR does / why we need it:

This pull request refactors how the maximum request body size is enforced in the application. The body size limit logic has been moved from the authentication middleware to a new global middleware, ensuring that all incoming requests are protected from large payloads, not just those passing through authentication.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

This PR is split from #250 to address the comments of #250 (comment).

Does this PR introduce a user-facing change?:

`picod`: All incoming requests are now protected from large payloads. 

Signed-off-by: RainbowMango <qdurenhongcai@gmail.com>
Copilot AI review requested due to automatic review settings May 12, 2026 11:09
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

@RainbowMango: The label(s) kind/security cannot be applied, because the repository doesn't have them.

Details

In response to this:

What type of PR is this?

/kind cleanup
/kind security

What this PR does / why we need it:

This pull request refactors how the maximum request body size is enforced in the application. The body size limit logic has been moved from the authentication middleware to a new global middleware, ensuring that all incoming requests are protected from large payloads, not just those passing through authentication.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

This PR is split from #250 to address the comments of #250 (comment).

Does this PR introduce a user-facing change?:

`picod`: All incoming requests are now protected from large payloads. 

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign acsoto for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@volcano-sh-bot
Copy link
Copy Markdown
Contributor

Welcome @RainbowMango! It looks like this is your first PR to volcano-sh/agentcube 🎉

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates PicoD’s Gin server setup to enforce a maximum HTTP request body size globally (not only for authenticated routes), reducing DoS risk from oversized payloads.

Changes:

  • Add a global Gin middleware that wraps c.Request.Body with http.MaxBytesReader(..., MaxBodySize) and set engine.MaxMultipartMemory = MaxBodySize.
  • Remove the request body size enforcement from AuthMiddleware, keeping auth focused on JWT verification.

Reviewed changes

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

File Description
pkg/picod/server.go Adds global request body size limiting middleware and aligns multipart parsing memory cap with MaxBodySize.
pkg/picod/auth.go Removes body-size enforcement from authentication middleware.

Comment thread pkg/picod/server.go
Comment on lines +75 to +80
// Limit request body size to 32 MB to prevent DoS attacks
engine.Use(func(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)
c.Next()
})
engine.MaxMultipartMemory = MaxBodySize
Comment thread pkg/picod/server.go
// Global middleware
engine.Use(gin.Logger()) // Request logging
engine.Use(gin.Recovery()) // Crash recovery
// Limit request body size to 32 MB to prevent DoS attacks
Comment thread pkg/picod/server.go
Comment on lines +77 to +80
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)
c.Next()
})
engine.MaxMultipartMemory = MaxBodySize
Comment thread pkg/picod/auth.go

// Enforce maximum body size to prevent memory exhaustion
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request moves the request body size limit enforcement from the authentication middleware to a global middleware in the server setup. Feedback includes a request to remove redundant blank lines in auth.go, a suggestion to generalize a comment in server.go to avoid hardcoded values, and a recommendation to lower MaxMultipartMemory to prevent high memory consumption during multipart uploads.

Comment thread pkg/picod/auth.go

// Enforce maximum body size to prevent memory exhaustion
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)

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.

medium

There are multiple consecutive blank lines here. Please remove the extra blank line to maintain consistent code style.

Comment thread pkg/picod/server.go
// Global middleware
engine.Use(gin.Logger()) // Request logging
engine.Use(gin.Recovery()) // Crash recovery
// Limit request body size to 32 MB to prevent DoS attacks
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.

medium

The comment explicitly mentions '32 MB', which might become outdated if the MaxBodySize constant is changed in the future. It is better to refer to the constant name or use a more generic description.

Suggested change
// Limit request body size to 32 MB to prevent DoS attacks
// Limit request body size to prevent DoS attacks

Comment thread pkg/picod/server.go
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)
c.Next()
})
engine.MaxMultipartMemory = MaxBodySize
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.

medium

Setting engine.MaxMultipartMemory to the same value as the hard body size limit (MaxBodySize) effectively disables Gin's ability to spill large multipart uploads to disk. If the goal is to prevent memory exhaustion, it is often better to keep MaxMultipartMemory at a lower value (e.g., 8 MiB) while using http.MaxBytesReader to enforce the absolute maximum request size. This allows processing files up to 32 MiB without consuming 32 MiB of RAM per concurrent request.

Comment thread pkg/picod/server.go
engine.Use(gin.Recovery()) // Crash recovery
// Limit request body size to 32 MB to prevent DoS attacks
engine.Use(func(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, MaxBodySize)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TestAuthMiddleware_MaxBodySize needs to be updated as well.

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.

4 participants