Skip to content

Explicitly request no-cache to discourage WP Cloud from edge caching CORS proxy results - #1930

Merged
brandonpayton merged 3 commits into
trunkfrom
disable-wp-cloud-edge-caching
Oct 23, 2024
Merged

brandonpayton merged 3 commits into
trunkfrom
disable-wp-cloud-edge-caching

Conversation

@brandonpayton

Copy link
Copy Markdown
Member

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-cache header with normal proxy responses.

Testing Instructions (or ideally a Blueprint)

  • Add a temporary cors-proxy-config.php that defines a truthy PLAYGROUND_CORS_PROXY_DISABLE_RATE_LIMIT constant. This disables rate-limiting checks which are irrelevant to this change.
  • Run local PHP web server using php -S localhost:7777
  • Test that CORS proxy continues to work
  • In a browser, test that http://localhost:7777/cors-proxy.php?https://wordpress.org responds with a Cache-Control: no-cache header

@brandonpayton brandonpayton self-assigned this Oct 23, 2024
@brandonpayton brandonpayton added [Type] Bug An existing feature does not function as intended [Package][@wp-playground] CORS Proxy labels Oct 23, 2024
@brandonpayton
brandonpayton requested a review from a team October 23, 2024 04:34
@brandonpayton brandonpayton changed the title Explicitly request no-cache to avoid WP Cloud edge caching CORS proxy results Explicitly request no-cache to discourage WP Cloud from edge caching CORS proxy results 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) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding a space for consistency.

} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
@http_response_code($httpCode);
if (!$httpcode_sent) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we may have already sent the HTTP response code and set $httpcode_sent, let's avoid setting the response code again.

@brandonpayton
brandonpayton merged commit 5e5ba3e into trunk Oct 23, 2024
@brandonpayton
brandonpayton deleted the disable-wp-cloud-edge-caching branch October 23, 2024 16:44
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package][@wp-playground] CORS Proxy [Type] Bug An existing feature does not function as intended

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

Avoid WP Cloud edge caching for CORS proxy

1 participant