Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to limit HTTP request size? #3059

Closed
shaofeng66 opened this issue Oct 27, 2023 · 6 comments
Closed

[Question] How to limit HTTP request size? #3059

shaofeng66 opened this issue Oct 27, 2023 · 6 comments
Labels
question Further information is requested

Comments

@shaofeng66
Copy link

seems not possible to archive this by middleware b/c middlewares are processed after receiving full request?

@shaofeng66 shaofeng66 added the question Further information is requested label Oct 27, 2023
@shenqidebaozi
Copy link
Sponsor Member

Is the title and content the same question? When middleware is processing, the request body has already been parsed

@shenqidebaozi
Copy link
Sponsor Member

Perhaps there is a deviation in my understanding of the problem, please provide specific supplements

@czyt
Copy link
Contributor

czyt commented Jan 9, 2024

I think you can setlimt on gRPC request.if gRPC is limited,the http is also limitted.

import "google.golang.org/grpc"

...

s := grpc.NewServer(
  grpc.MaxRecvMsgSize(maxReceiveMessageSize),
  grpc.MaxSendMsgSize(maxSendMessageSize),
)

see dapr/go-sdk#189

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Apr 9, 2024
Copy link

dosubot bot commented Apr 9, 2024

Hi, @shaofeng66,

I'm helping the Kratos team manage their backlog and am marking this issue as stale. The issue pertains to limiting the size of HTTP requests using middleware, with some discussion around the processing of requests and potential solutions such as setting limits on gRPC requests.

Could you please confirm if this issue is still relevant to the latest version of the Kratos repository? If it is, please let the Kratos team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your understanding and cooperation.

@shaofeng66
Copy link
Author

I achieved by below filter in our server:

import (
	"net/http"
)

const MaxRequestSize = 2 * 1024 * 1024

func RequestSizeLimitFilter(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		r.Body = http.MaxBytesReader(w, r.Body, MaxRequestSize)
		next.ServeHTTP(w, r)
	})
}

@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Apr 10, 2024
Copy link

dosubot bot commented Apr 10, 2024

Thank you, @shaofeng66, for closing this issue! Your contribution is much appreciated.

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants