Skip to content

Commit 5866bc7

Browse files
committed
fix(aws-lambda): strip parameters before matching content type
1 parent 39ab122 commit 5866bc7

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

‎src/adapters/_aws/utils.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,17 @@ function waitForDrain(writer: NodeJS.WritableStream): Promise<void> {
340340
});
341341
}
342342

343+
// The parameters are split off before matching: testing the raw header value
344+
// let the charset alternative match inside a parameter, so
345+
// `application/octet-stream; charset=utf-8` (or a `multipart/form-data`
346+
// boundary that merely contains `utf8`) was decoded with `toString("utf8")` and
347+
// every invalid byte became U+FFFD — unrecoverable corruption. Structured
348+
// syntax suffixes are matched too, since `/xml` and `/json` never match the
349+
// `+xml`/`+json` forms, which left `image/svg+xml`, `application/xhtml+xml` and
350+
// `application/ld+json` base64-inflated by ~33% for no reason.
343351
function isTextType(contentType = "") {
344-
return /^text\/|\/(javascript|json|xml)|utf-?8/i.test(contentType);
352+
const mimeType = contentType.split(";")[0].trim();
353+
return /^text\/|\/(javascript|json|xml)|\+(json|xml)$/i.test(mimeType);
345354
}
346355

347356
function toBuffer(data: ReadableStream): Promise<Buffer> {

‎test/aws.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,74 @@ describe("[AWS Lambda] Request Utils", () => {
11411141
}
11421142
});
11431143

1144+
test("should not treat a charset/boundary parameter as the MIME type", async () => {
1145+
// `; charset=utf-8` and a boundary containing `utf8` used to match the
1146+
// content type as text, so the payload was UTF-8 decoded and every
1147+
// invalid byte was replaced with U+FFFD — unrecoverable.
1148+
const binaryData = Buffer.from([0xff, 0xfe, 0x00, 0x80, 0x01]);
1149+
1150+
for (const contentType of [
1151+
"application/octet-stream; charset=utf-8",
1152+
"multipart/form-data; boundary=--utf8x",
1153+
]) {
1154+
const response = new Response(binaryData, {
1155+
status: 200,
1156+
headers: { "Content-Type": contentType },
1157+
});
1158+
1159+
const awsBody = await awsResponseBody(response);
1160+
1161+
expect(awsBody.isBase64Encoded).toBe(true);
1162+
expect(Buffer.from(awsBody.body, "base64")).toEqual(binaryData);
1163+
}
1164+
});
1165+
1166+
test("should treat +json/+xml structured syntax suffixes as text", async () => {
1167+
const content = '<svg xmlns="http://www.w3.org/2000/svg" />';
1168+
1169+
for (const contentType of [
1170+
"image/svg+xml",
1171+
"application/xhtml+xml",
1172+
"application/ld+json",
1173+
"application/manifest+json; charset=utf-8",
1174+
]) {
1175+
const response = new Response(content, {
1176+
status: 200,
1177+
headers: { "Content-Type": contentType },
1178+
});
1179+
1180+
const awsBody = await awsResponseBody(response);
1181+
1182+
expect(awsBody.body).toBe(content);
1183+
expect(awsBody.isBase64Encoded).toBeUndefined();
1184+
}
1185+
});
1186+
1187+
test("should keep binary types with a `+` suffix binary", async () => {
1188+
const binaryData = Buffer.from([0x00, 0x61, 0x73, 0x6d]);
1189+
const response = new Response(binaryData, {
1190+
status: 200,
1191+
headers: { "Content-Type": "application/wasm" },
1192+
});
1193+
1194+
const awsBody = await awsResponseBody(response);
1195+
1196+
expect(awsBody.isBase64Encoded).toBe(true);
1197+
expect(Buffer.from(awsBody.body, "base64")).toEqual(binaryData);
1198+
});
1199+
1200+
test("should still detect text types carrying parameters", async () => {
1201+
const response = new Response("Hello, World!", {
1202+
status: 200,
1203+
headers: { "Content-Type": "text/plain; charset=utf-8" },
1204+
});
1205+
1206+
const awsBody = await awsResponseBody(response);
1207+
1208+
expect(awsBody.body).toBe("Hello, World!");
1209+
expect(awsBody.isBase64Encoded).toBeUndefined();
1210+
});
1211+
11441212
test("should handle missing content-type header", async () => {
11451213
const response = new Response("content", { status: 200 });
11461214

0 commit comments

Comments
 (0)