Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove content-encoding header from already decompressed responses #54

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Remove content-encoding header from already decompressed responses
When the `compress` flag is set to true, the fetch implementation sends an Accept-Encoding header and decompresses the response.

However, the decompressed body is put into a `Response` that still has a `Content-Encoding` that no longer matches the actual encoding of the body.

This causes issues if this `Response` object is used downstream (i.e. sent back to a browser), and there is another attempt to decode the body based on the content-encoding header.
  • Loading branch information
benbrandt committed Sep 28, 2023
commit bbe84b3b3cf036db26d2a28c0627afcf2af1f878
3 changes: 3 additions & 0 deletions packages/fetch/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ async function fetch(url, options_ = {}) {

// For gzip
if (codings === 'gzip' || codings === 'x-gzip') {
responseOptions.headers.delete("Content-Encoding");
body = pump(body, zlib.createGunzip(zlibOptions), reject);
response = new Response(fromAsyncIterable(body), responseOptions);
resolve(response);
Expand All @@ -285,6 +286,7 @@ async function fetch(url, options_ = {}) {

// For deflate
if (codings === 'deflate' || codings === 'x-deflate') {
responseOptions.headers.delete("Content-Encoding");
// Handle the infamous raw deflate response from old servers
// a hack for old IIS and Apache servers
const raw = pump(response_, new PassThrough(), reject);
Expand All @@ -304,6 +306,7 @@ async function fetch(url, options_ = {}) {

// For br
if (codings === 'br') {
responseOptions.headers.delete("Content-Encoding");
body = pump(body, zlib.createBrotliDecompress(), reject);
response = new Response(fromAsyncIterable(body), responseOptions);
resolve(response);
Expand Down
8 changes: 6 additions & 2 deletions packages/fetch/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ describe("node-fetch", () => {
const url = `${base}gzip`;
return fetch(url).then((res) => {
expect(res.headers.get("content-type")).to.equal("text/plain");
expect(res.headers.get("content-encoding")).to.be.null;
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.equal("hello world");
Expand All @@ -836,10 +837,9 @@ describe("node-fetch", () => {
});
});

it("should make capitalised Content-Encoding lowercase", () => {
it("should decompress capitalised Content-Encoding", () => {
const url = `${base}gzip-capital`;
return fetch(url).then((res) => {
expect(res.headers.get("content-encoding")).to.equal("gzip");
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.equal("hello world");
Expand All @@ -851,6 +851,7 @@ describe("node-fetch", () => {
const url = `${base}deflate`;
return fetch(url).then((res) => {
expect(res.headers.get("content-type")).to.equal("text/plain");
expect(res.headers.get("content-encoding")).to.be.null;
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.equal("hello world");
Expand All @@ -877,6 +878,7 @@ describe("node-fetch", () => {
const url = `${base}brotli`;
return fetch(url).then((res) => {
expect(res.headers.get("content-type")).to.equal("text/plain");
expect(res.headers.get("content-encoding")).to.be.null;
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.equal("hello world");
Expand Down Expand Up @@ -906,6 +908,7 @@ describe("node-fetch", () => {
const url = `${base}sdch`;
return fetch(url).then((res) => {
expect(res.headers.get("content-type")).to.equal("text/plain");
expect(res.headers.get("content-encoding")).to.equal("sdch");
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.equal("fake sdch string");
Expand Down Expand Up @@ -957,6 +960,7 @@ describe("node-fetch", () => {
};
return fetch(url, options).then((res) => {
expect(res.headers.get("content-type")).to.equal("text/plain");
expect(res.headers.get("content-encoding")).to.equal("gzip");
return res.text().then((result) => {
expect(result).to.be.a("string");
expect(result).to.not.equal("hello world");
Expand Down