Skip to content

Commit facf3cb

Browse files
authored
fix: don't decode application/octet-stream as text when charset is utf-8 (#823)
getResponseData() treats any response carrying charset=utf-8 as text, even application/octet-stream — the canonical "arbitrary binary" media type (RFC 2046). Binary content (e.g. a raw binary file fetched with mediaType: { format: "raw" }) is therefore run through response.text() and mangled instead of being returned as an ArrayBuffer. Exclude application/octet-stream from the charset-based text branch so it falls through to arrayBuffer(). Genuinely textual responses (text/* and other charset=utf-8 types such as application/vnd.github.v3.raw) are unaffected. Refs #751
1 parent 554e102 commit facf3cb

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎src/fetch-wrapper.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ async function getResponseData(response: Response): Promise<any> {
172172
}
173173
} else if (
174174
mimetype.type.startsWith("text/") ||
175-
mimetype.parameters.charset?.toLowerCase() === "utf-8"
175+
// `application/octet-stream` is the canonical "arbitrary binary" type
176+
// (RFC 2046) and must never be decoded as text, even when the response
177+
// carries a (misleading) `charset=utf-8` parameter — see #751.
178+
(mimetype.parameters.charset?.toLowerCase() === "utf-8" &&
179+
mimetype.type !== "application/octet-stream")
176180
) {
177181
return response.text().catch(noop);
178182
} else {

‎test/request-native-fetch.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,33 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
406406
);
407407
});
408408

409+
it("Binary response with utf-8 charset is not mangled to text (#751)", async () => {
410+
expect.assertions(3);
411+
412+
const payload =
413+
"1f8b0800000000000003cb4f2ec9cfce2cd14dcbac28292d4ad5cd2f4ad74d4f2dd14d2c4acec82c4bd53580007d060a0050bfb9b9a90203c428741ac2313436343307222320dbc010a8dc5c81c194124b8905a5c525894540a714e5e797e05347481edd734304e41319ff41ae8e2ebeae7ab92964d801d46f66668227fe0d4d51e3dfc8d0c8d808284f75df6201233cfe951590627ba01d330a46c1281805a3806e000024cb59d6000a0000";
414+
415+
const request = await mockRequestHttpServer((req, res) => {
416+
// Binary payload that (incorrectly) carries a charset parameter.
417+
// It must still be returned as an ArrayBuffer, not decoded as text.
418+
res.writeHead(200, {
419+
"content-type": "application/octet-stream; charset=utf-8",
420+
"content-length": "172",
421+
});
422+
res.end(Buffer.from(payload, "hex"));
423+
});
424+
425+
const response = await request(
426+
`GET ${request.baseUrlMockServer}/octokit-fixture-org/get-archive/legacy.tar.gz/master`,
427+
);
428+
429+
expect(response.status).toEqual(200);
430+
expect(response.data).toBeInstanceOf(ArrayBuffer);
431+
expect(zlib.gunzipSync(Buffer.from(payload, "hex")).buffer).toEqual(
432+
response.data,
433+
);
434+
});
435+
409436
it("304 etag", async () => {
410437
expect.assertions(6);
411438

0 commit comments

Comments
 (0)