cloudflare / cloudflare/workers-sdk

Miniflare corrupts response when returning a compressed subrequest

Open
#10,375 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4.5k
Forks
1.5k
Avg merge
3d 8h
Merged PRs (30d)
186

Description

### What versions & operating system are you using?

System:
OS: macOS 15.6
CPU: (10) arm64 Apple M1 Pro
Memory: 2.35 GB / 32.00 GB
Shell: 5.2.21 - /opt/homebrew/bin/bash
Binaries:
Node: 20.18.0 - ~/.local/share/mise/installs/node/20.18.0/bin/node
Yarn: 1.22.22 - ~/.local/share/mise/installs/npm-yarn/1.22.22/bin/yarn
npm: 10.8.2 - ~/.local/share/mise/installs/node/20.18.0/bin/npm
pnpm: 9.15.9 - ~/.local/share/mise/installs/pnpm/9.15.9/bin/pnpm
npmPackages:
@cloudflare/vite-plugin: ^1.11.3 => 1.11.3
wrangler: ^4.29.0 => 4.29.0

### Please provide a link to a minimal reproduction

_No response_

### Describe the Bug

A compressed subrequest response that WAS compressed, is delivered uncompressed and WITHOUT content-encoding header, while the original content-length header is preserved. This results in browsers cutting off the response when the content-length is consumed, resulting in response corruption, and downstream errors (e.g. parsing errors for js).

[Full description here](https://github.com/cloudflare/workers-sdk/issues/8004#issuecomment-2981396283). I'm filing this as a separate issue because that's just a comment on a different issue.

I can reproduce this by setting up posthog using cloudflare as a reverse proxy, as described here: https://posthog.com/docs/advanced/proxy/cloudflare. When you do that, after calling `posthog.init`, posthog will make a request for https://us-proxy-direct.i.posthog.com/array/CHANGE_THIS_TO_POSTHOG_KEY/config.js which is returned with `content-encoding: gzip`, which miniflare decompresses but fails to change the content-length, resulting in this error in the browser console (because the truncated JS file can't be parsed): `Uncaught SyntaxError: Invalid or unexpected token (at config.js:4:460)`

Other people ran into this and reported it on the posthog repo: https://github.com/PostHog/posthog-js/issues/1906

### Please provide any relevant error logs

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.