fix(lambda-edge): base64 encode compressed response bodies#5003
Open
LeSingh1 wants to merge 1 commit into
Open
fix(lambda-edge): base64 encode compressed response bodies#5003LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
The lambda-edge adapter decided whether to base64 encode the response body solely from the content-type. A compressed response, such as one produced by the compress middleware, can carry a textual content-type like text/html while the body is gzip bytes. In that case res.text() was called on binary data and the body was corrupted, so CloudFront returned an empty or broken response. Check the content-encoding header as a fallback when the content-type is not binary, matching the behavior already used by the aws-lambda adapter. Fixes honojs#4254
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4254
When the lambda-edge adapter builds a CloudFront result, it decides whether to base64 encode the body using only the content-type. A compressed response can keep a textual content-type such as text/html while the actual body is gzip bytes. The compress middleware does exactly this. In that case the adapter calls res.text() on binary data, which corrupts the body, and CloudFront returns an empty or broken response.
This checks the content-encoding header as a fallback when the content-type is not detected as binary. If the response is gzip, deflate, compress, or br encoded, the body is base64 encoded with bodyEncoding set to base64. The aws-lambda adapter already does the same thing in its createResult, so this brings lambda-edge in line with it.
I added a small isContentEncodingBinary helper that mirrors the one in the aws-lambda adapter, with unit tests, plus a handle test that runs a gzip-encoded text/html response through the adapter and verifies the body comes back base64 encoded and decompresses to the original payload.
Note: a previous attempt at this issue (#4874) was closed for including unrelated changes. This PR only touches the lambda-edge handler and its test.