fix(lambda-edge): classify ZIP-based document subtypes as binary#4900
Open
rokasta12 wants to merge 1 commit into
Open
fix(lambda-edge): classify ZIP-based document subtypes as binary#4900rokasta12 wants to merge 1 commit into
rokasta12 wants to merge 1 commit into
Conversation
The previous regex `application/(.*json|.*xml).*` matched any subtype containing the substrings "json" or "xml" anywhere, including `application/vnd.openxmlformats-officedocument.*` (.docx/.xlsx/.pptx), `application/epub+zip`, and `application/vnd.oasis.opendocument.*`. These are ZIP archives. Treating them as text caused the body to be returned un-base64-encoded, so CloudFront delivered corrupted files to clients. Replaces the regex with the boundary-aware pattern already used by the aws-lambda adapter: it accepts `text/(plain|html|css|javascript|csv)` at the start, or `/json`, `/xml`, `+json`, `+xml` followed by `;` or end-of-string. This keeps `application/atom+xml`, `application/ld+json`, and `image/svg+xml` as text while correctly rejecting OOXML/EPUB/ODT.
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.
Problem
isContentTypeBinaryinsrc/adapter/lambda-edge/handler.tsused the regexapplication/(.*json|.*xml).*, which matches any subtype that contains the substringjsonorxmlanywhere — including ZIP archives whose subtypes happen to embed those letters:application/vnd.openxmlformats-officedocument.wordprocessingml.document(.docx)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet(.xlsx)application/vnd.openxmlformats-officedocument.presentationml.presentation(.pptx)application/epub+zip(.epub)application/vnd.oasis.opendocument.text(.odt)When
isContentTypeBinaryreturnsfalse, the response body is returned to CloudFront un-base64-encoded, so any user serving these document types from a Lambda@Edge handler delivers a corrupted file.Fix
Replace the regex with the boundary-aware pattern already used by the aws-lambda adapter (
src/adapter/aws-lambda/handler.ts:675):This accepts:
text/(plain|html|css|javascript|csv)at the start/json,/xml,+json,+xmlfollowed by;or end-of-stringSo
application/atom+xml,application/ld+json, andimage/svg+xmlcontinue to be treated as text, while OOXML/EPUB/ODT subtypes are correctly classified as binary.Test
Added a regression test covering the OOXML triple,
application/epub+zip, andapplication/vnd.oasis.opendocument.textalongside the existing assertions. The new test fails onmainand passes with the regex fix.bun run test— passes)bun run format:fix && bun run lint:fix