Repository navigation
Explicitly request no-cache to discourage WP Cloud from edge caching CORS proxy results - #1930
Merged
Merged
Conversation
brandonpayton
commented
Oct 23, 2024
| $httpcode_sent = false; | ||
| curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $header) use($targetUrl, &$httpcode_sent, $ch) { | ||
| if(!$httpcode_sent) { | ||
| if (!$httpcode_sent) { |
Member
Author
There was a problem hiding this comment.
Just adding a space for consistency.
| } else { | ||
| $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| @http_response_code($httpCode); | ||
| if (!$httpcode_sent) { |
Member
Author
There was a problem hiding this comment.
Since we may have already sent the HTTP response code and set $httpcode_sent, let's avoid setting the response code again.
brandonpayton
added a commit
that referenced
this pull request
Sep 27, 2026
## Motivation for the change, related issues Range requests are a standard part of HTTP, and the CORS proxy should support them for any client. Resumable downloads, media seeking, and reading parts of large files all depend on them. This PR was prompted by a Kandelo need. Kandelo reads ZIP archive indexes through the CORS proxy without downloading whole archives. That takes three or four small range reads: the end-of-central-directory record via a suffix range, then the central directory, then individual members. The proxy already forwarded `Range` and relayed `206`, but browsers could not use it: - Preflight did not allow `Range`. Browsers only safelist simple `bytes=N-M` ranges, so suffix ranges like `bytes=-22` were blocked. - `Content-Range`, `Accept-Ranges`, and `ETag` are not CORS-safelisted response headers, so scripts could not read them. Running requests against the production deployment also showed that the WP Cloud front end **strips `Range` and `If-Range`** before the request reaches PHP. It also rewrites `Accept-Encoding` to `gzip, br`. Custom headers pass through untouched, and responses (`206`, `416`, `Content-Range`, `ETag`) come back intact. **Merge order:** this PR, then #4364, then #4365. #4364 is stacked on this PR, and #4365 is stacked on #4364. ## Implementation details - Add `Range` to `Access-Control-Allow-Headers`. - Add `Content-Range`, `Accept-Ranges`, and `ETag` to `Access-Control-Expose-Headers`. `Last-Modified` is already safelisted. - When the outgoing request has a range, send `Accept-Encoding: identity` to the target so byte offsets refer to the unencoded body. Browsers already do this for `Range` requests per the Fetch spec, but the front end overwrites the value. - **Workaround:** clients may send `X-Cors-Proxy-Range`, which the proxy forwards to the target as `Range`. The internal header is never forwarded. The code carries a `WORKAROUND:` comment, and the README has a note, both listing what to remove once `Range` reaches PHP on WP Cloud. I confirmed that production's front end lets this header through. - Size cap: `MAX_RESPONSE_SIZE` is still checked against the response's `Content-Length`, which on a `206` is the slice length. A small read of a multi-GiB file therefore works, and a test guards against "fixing" this by parsing the total out of `Content-Range`. An inline comment now documents an existing limitation: the cap is only enforced against a declared `Content-Length`, so responses without one, such as chunked responses, stream through at any size. Range requests make such responses easier to reach, but enforcing a hard limit is out of scope here. - No range parsing or multi-range support was added. `If-Range` is not allowed. - `Range` takes priority over `X-Cors-Proxy-Range`. Clients may send both with the same value, so they keep working once the workaround is removed; the proxy then forwards `Range` unchanged. If the two disagree, the proxy responds with `400 Bad Request` rather than silently ignore one. - An empty `X-Cors-Proxy-Range` is ignored. Otherwise curl would treat the resulting empty `Range` header as a removal and drop the client's real `Range`. - If the target fails after the proxy has started sending its response, the proxy now ends the response instead of appending a "Bad Gateway" message to the body. Previously a truncated `206` could carry that text as if it were file bytes. If the target fails before the proxy has sent anything, for example right after its headers, the proxy still returns `502`. The proxy does not relay `Content-Length`, so clients can detect a short `206` by comparing the body length with `Content-Range`. - When the proxy replaces the target's response with its own `413` or `502`, it removes every header relayed from the target, e.g. `Content-Range`, `Location`, or `Transfer-Encoding`, since they describe a response that never arrived. It then restores a snapshot of the headers queued before the target was contacted: the CORS headers, `Cache-Control: no-cache`, and any set by the deployment in `cors-proxy-config.php` or an `auto_prepend_file`. `Cache-Control` is now set before that snapshot instead of when the target's status arrives. **Edge caching:** ranged responses must never be stored by the WP Cloud edge cache. Every range of a file shares one proxy URL, so a cached slice could be served for another range, and many distinct slices would add cache noise. - Edge caching has been disabled for all proxy responses since #1930 (for #1920). This PR leaves that unchanged. - WP Cloud's edge cache does not store responses with `Cache-Control: no-cache`. The proxy sends that header on every response it relays from a target and never relays the target's own `Cache-Control`. - The edge also bypasses its cache for any request with an `Origin` header. Since browsers send `Origin` on every cross-origin request, re-enabling edge caching for browser traffic would first need a platform change. - I verified this against production: responses, including `206`s, are not stored because of the proxy's `Cache-Control` header. - The e2e tests now assert this header on `206` and `416` responses. I confirmed they fail when the header is removed. **Test harness fixes:** - The e2e script could not start its servers where `php` is not on the fallback system path, such as Homebrew PHP on macOS: `proc_open` replaced the whole environment, dropping `PATH`. It now uses `PHP_BINARY` and inherits the parent environment. - `test.sh`, which the `test` target runs, now runs the e2e script after PHPUnit. Previously nothing ran it. - Removed the no-op `curl_close()` calls, which emit deprecation warnings on PHP 8.5. **Note for reviewers:** because `test.sh` now runs the e2e script, CI's `nx affected --target=test` will run it when this package changes. It needs PHP's `curl` and `sockets` extensions, which should be available on the Ubuntu runners. ## Testing Instructions (or ideally a Blueprint) ```bash cd packages/playground/php-cors-proxy bash test.sh ``` Expect PHPUnit to pass, followed by `All 44 assertions passed.` from the e2e script. The new e2e tests cover: - preflight allowing `Range` and `X-Cors-Proxy-Range`; - a suffix range returning `206` with the right bytes, `Content-Range`, and exposed headers; - an unsatisfiable range relaying `416` with `Content-Range: bytes */<total>`; - `206` and `416` responses carrying `Cache-Control: no-cache` so the edge cache does not store them; - a small range of a 4 GiB file passing the size cap, and an oversized slice rejected with `413`; - the target receiving `Range` and `Accept-Encoding: identity`; - `X-Cors-Proxy-Range` being forwarded as `Range` and not forwarded itself; - `Range` and `X-Cors-Proxy-Range` together: an empty `X-Cors-Proxy-Range` doesn't drop `Range`, matching values forward one `Range`, and disagreeing values get `400`; - a target failing mid-body without an error message being appended, and a target failing before its body getting `502`; - the `413` and `502` responses carrying none of the target's headers while keeping the proxy's own and a header set by the deployment. The PHPUnit tests use `@dataProvider` annotations, which PHPUnit 13 no longer supports. I ran them with PHPUnit 9. After deployment: ```bash P="https://wordpress-playground-cors-proxy.net/?" F="https://dn721609.ca.archive.org/0/items/carpetshark/CarpetShark.nes" curl -si -H "X-Cors-Proxy-Range: bytes=-22" "$P$F" | head -20 # expect: HTTP 206 + content-range: bytes 524282-524303/524304 ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for forwarding byte-range requests and relaying partial or unsatisfiable range responses with relevant headers. * Added a fallback range header for environments that remove the standard header. Conflicting range values return a 400 error. * Ranged requests use identity encoding, and relayed responses disable caching. * **Bug Fixes** * Proxy-generated errors preserve proxy and deployment headers without including upstream headers. * Upstream transfer failures return a 502 error if no response has begun. If a response has started, the proxy no longer appends an error body or chunked-response terminator. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Motivation for the change, related issues
This PR attempts to disable WP Cloud edge caching of CORS proxy responses.
Closes #1920
Implementation details
This PR updates the CORS proxy to always include a
Cache-Control: no-cacheheader with normal proxy responses.Testing Instructions (or ideally a Blueprint)
cors-proxy-config.phpthat defines a truthyPLAYGROUND_CORS_PROXY_DISABLE_RATE_LIMITconstant. This disables rate-limiting checks which are irrelevant to this change.php -S localhost:7777Cache-Control: no-cacheheader