Skip to content

Return 413 when the ASP.NET Core response exceeds the Lambda payload limit - #2620

Open
pkryusuf wants to merge 2 commits into
aws:masterfrom
pkryusuf:fix-1815-response-too-large
Open

pkryusuf wants to merge 2 commits into
aws:masterfrom
pkryusuf:fix-1815-response-too-large

Conversation

@pkryusuf

Copy link
Copy Markdown

Issue #, if available: #1815

Description of changes:

Following the approach suggested in the issue: when the marshalled response body is larger than the 6 MB Lambda payload limit, APIGatewayProxyFunction and APIGatewayHttpApiV2ProxyFunction now return a 413 (and log an error) instead of the invocation failing with an opaque error. The check runs in MarshallResponse before PostMarshallResponseFeature, so users can still customize the 413 response.

  • Utilities.IsResponseBodyTooLarge: base64 bodies are measured by string length, text bodies by UTF-8 byte count.
  • Change file added for Amazon.Lambda.AspNetCoreServer (patch).
  • Tests: unit tests for the limit boundary (text, base64, multi-byte UTF-8, null) and end-to-end tests for REST and HTTP API v2 using a new 7 MB LargeResponseController in TestWebApp. All 233 tests in Amazon.Lambda.AspNetCoreServer.Test pass on net8.0 and net10.0.

Notes for review:

  • The check covers the body only, not headers or JSON escaping, so a response just under the limit can still fail the old way. Exact checking would mean serializing every response.
  • ALB (1 MB limit) and response streaming are unchanged; happy to add ALB if wanted.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@pkryusuf
pkryusuf requested review from a team as code owners November 21, 2026 18:11
@boblodgett

Copy link
Copy Markdown
Contributor

Repro from #1815 is fixed.

However, as you mentioned, the check measures the raw body rather than the payload Lambda measures, so it can still go over so #1815 isn't completely fixed. Seems like we'd need the exact encoded body count plus a fixed headroom constant.

@pkryusuf

Copy link
Copy Markdown
Author

Thanks for testing it! I pushed a change that does that:

  • The body is now measured as it is written into the response JSON: escaped with JavaScriptEncoder.UnsafeRelaxedJsonEscaping, like the Lambda serializers, and counted in UTF-8 bytes. Short bodies skip the escaping pass (a character is at most 6 bytes escaped), so the extra work only happens for large responses.
  • A fixed ResponseEnvelopeHeadroom of 16 KB is kept for the status code, headers and property names, so the body limit is 6 MB minus 16 KB.
  • New tests cover quotes, backslashes, newlines and \u0001, and one serializes the largest allowed body (mixed with escaped characters) through DefaultLambdaJsonSerializer and checks the whole response stays under 6 MB.

Headers are not measured, so a response with more than about 16 KB of headers could still go over; I can count them exactly too if you prefer that over a constant.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants