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

AspNetCoreServer incorrectly sets IHttpRequestBodyDetectionFeature.CanHaveBody that leads to breaking change after net 7 upgrade #1854

Open
1 task
MadSciencist opened this issue Oct 31, 2024 · 0 comments
Assignees
Labels
bug This issue is a bug. module/aspnetcore-support needs-reproduction This issue needs reproduction. p2 This is a standard priority issue

Comments

@MadSciencist
Copy link

Describe the bug

Hi,

It appears that the behavior of the APIGatewayProxyFunction is inconsistent with the behavior of Kestrel when handling requests with an empty body directed to endpoints with a parameter marked [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)].

In the InvokeFeatures class, the CanHaveBody property is set based on requestFeature.Body != null. This is problematic because the IHttpRequestFeature.Body property is initialized with a new MemoryStream() value, therefor CanHaveBody is never false.

As a result, this change introduces a breaking behavior when upgrading from .NET 6.0 to .NET 7.0 in scenarios where requests with an empty body are sent from AWS Lambda. ASP.NET Core attempts to deserialize the request (due to the CanHaveBody property), and because the request body is an empty stream, it causes the API to throw an error.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Endpoints with parameters marked with [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] attribute can handle request both with and without body.

Current Behavior

AspNetCore tries to deserialize the body and throws The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

Reproduction Steps

Create PUT endpoint with optional body:

    public IActionResult Test([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body request = default)
    {
        return Accepted();
    }

    public class Body
    {
        public string Prop { get; set; }
    }

and send requests with and without body.

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Lambda.AspNetCoreServer 9.0.1

Targeted .NET Platform

NET 8.0

Operating System and version

Windows 10

@MadSciencist MadSciencist added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 31, 2024
@bhoradc bhoradc added needs-reproduction This issue needs reproduction. module/aspnetcore-support p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Oct 31, 2024
@bhoradc bhoradc self-assigned this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/aspnetcore-support needs-reproduction This issue needs reproduction. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants