Skip to content

Kestrel settings are not applied if configure KestrelServerOptions in appsettings.json file #37544

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

Closed
mm-ryo opened this issue Oct 14, 2021 · 8 comments
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel

Comments

@mm-ryo
Copy link

mm-ryo commented Oct 14, 2021

I am unable to configure KestrelServerOptions in appsettings.json file.
I tested the below settings, it doesn't work. it still takes default value (30,000,000 bytes => 28.6MB).
-- MaxRequestBodySize
-- MaxRequestHeaderCount

{
  "Kestrel": {
    "Limits": {
      "MaxRequestBodySize": 3000000,
      "MaxRequestHeaderCount": 10
    },
    "EndPoints": {
      "Http": {
        "Url": "http://*:10110"
      }
    }
  }
}

if I do it in C# code, it works.

services.Configure<FormOptions>(opt =>
{
    opt.MultipartBodyLengthLimit = 3000000;
});

services.Configure<KestrelServerOptions>(opt =>
{
    opt.Limits.MaxRequestBodySize = 3000000;
});

Further technical details

  • ASP.NET Core version: 5.0
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: vs2019 v16.11.4
  • Include the output of dotnet --info:
dotnet --info Output
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21463.6
 Commit:    e627d556a1

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-rc.1.21463.6\

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

.NET SDKs installed:
  1.1.14 [C:\Program Files\dotnet\sdk]
  2.1.526 [C:\Program Files\dotnet\sdk]
  2.1.818 [C:\Program Files\dotnet\sdk]
  2.2.402 [C:\Program Files\dotnet\sdk]
  3.1.118 [C:\Program Files\dotnet\sdk]
  5.0.100 [C:\Program Files\dotnet\sdk]
  5.0.102 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]
  5.0.301 [C:\Program Files\dotnet\sdk]
  5.0.303 [C:\Program Files\dotnet\sdk]
  5.0.401 [C:\Program Files\dotnet\sdk]
  6.0.100-rc.1.21463.6 [C:\Program Files\dotnet\sdk]
@Tratcher
Copy link
Member

MultipartBodyLengthLimit is a different limit than you've set in config. Which exception do you get during a request?

@mm-ryo
Copy link
Author

mm-ryo commented Oct 15, 2021

MultipartBodyLengthLimit is a different limit than you've set in config. Which exception do you get during a request?

if the upload file size exceeds the limitation it throws BadHttpRequestException.
I am also unable to catch this exception in my ExceptionMiddleware.
BadHttpRequestException: Request body too large.

@Tratcher
Copy link
Member

This is a known limitation of Kestrel's config integration, it only supports Endpoints, not other options. To read options you need to add this:

        services.Configure<KestrelServerOptions>(
            Configuration.GetSection("Kestrel"));

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-5.0

@Tratcher
Copy link
Member

Duplicate of #4765

@Tratcher Tratcher marked this as a duplicate of #4765 Oct 15, 2021
@mm-ryo
Copy link
Author

mm-ryo commented Oct 19, 2021

Hi @Tratcher,
Where can I handle BadHttpRequestException?
we already have an exception middleware to handle exceptions that throw out from requests.
but it never steps into it if throws BadHttpRequestException.

@Tratcher
Copy link
Member

Then where do you see the BadHttpRequestException?

@mm-ryo
Copy link
Author

mm-ryo commented Oct 20, 2021

Then where do you see the BadHttpRequestException?

I saw it in my "ValidateModelFilter : IActionFilter" and in Browser i saw this (see below img)

image

@Tratcher
Copy link
Member

Model binding has an opportunity to catch and handle request body exceptions.

A connection reset is expected when the request body size limits are exceeded, disconnecting the client is the only mitigation for a potential DOS attack.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 19, 2021
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions feature-kestrel
Projects
None yet
Development

No branches or pull requests

5 participants