CORSProxy
A small PHP CORS proxy intended for browser-side code that needs to reach servers without CORS headers.
composer require wp-php-toolkit/corsproxy
A Playground-style browser tool reads https://api.github.com/repos/WordPress/php-toolkit, a plugin ZIP from downloads.wordpress.org, or a raw fixture from GitHub. The browser blocks the response when the upstream server does not send the required CORS headers, even though PHP can fetch the same public URL server-side.
The CORSProxy component is that server-side bridge. It accepts a target URL, fetches it from PHP, and returns a browser-readable response. Because an open proxy is a security and abuse risk, real deployments should add host allowlists, rate limits, header controls, and private-network protections appropriate to their environment.
Run the proxy locally
Run on your machine: the proxy needs to listen on a port. Start PHP's built-in server and request any HTTPS URL through it.
PLAYGROUND_CORS_PROXY_DISABLE_RATE_LIMIT=1 \
php -S 127.0.0.1:5263 vendor/wp-php-toolkit/corsproxy/cors-proxy.php
# In another terminal:
curl -s "http://127.0.0.1:5263/cors-proxy.php/https://api.github.com/repos/WordPress/php-toolkit" | head
Destination validation
Before forwarding a request, the proxy resolves the target hostname to IPv4 addresses and rejects the request if any of them is private, loopback, link-local, reserved, or not a valid IPv4 address. It then pins cURL to the validated address for the URL's effective port: the explicit port, or 80 for HTTP and 443 for HTTPS. cURL never resolves the hostname a second time, so a DNS rebinding answer cannot swap in an internal address between the check and the connection. Custom ports remain supported; empty DNS answers and port zero are rejected.
cURL is restricted to IPv4, so IPv6-only targets are not supported. Environment HTTP/SOCKS proxy settings such as http_proxy are ignored, because a forward proxy would resolve the hostname itself and bypass the validation. The URL's hostname stays the HTTP Host and TLS identity. Redirects are relayed back through the proxy rather than followed, so each hop is validated again. Keep outbound network rules that deny private and metadata destinations as an independent layer of protection.
Production rate limiting
Drop a cors-proxy-config.php next to cors-proxy.php. If that file defines a playground_cors_proxy_maybe_rate_limit() function, the proxy calls it before forwarding any request — your one chance to reject early. Without that function the proxy relies only on its coarse built-in safeguards, so production deployments should provide their own rate limiting. Keep that directory out of your web root so the file can't be downloaded; see "Deploy behind nginx" below.
This example uses a per-IP token bucket stored on disk. Replace with Redis or memcached for multi-host deployments.
Allowlist upstream hosts
Out of the box the proxy will fetch any public URL. Most real deployments want a fixed list of upstreams — GitHub, Packagist, wp.org. Both the rate-limit logic and the allowlist live in the same hook, since cors-proxy.php only calls playground_cors_proxy_maybe_rate_limit() once. The example below shows just the allowlist concern; in practice you stack both in one function inside cors-proxy-config.php.
Browser-side fetch through the proxy
Once deployed, the client side is just fetch() with the proxy URL. Drop this into any HTML page.
const PROXY = "https://cors.example.com/cors-proxy.php";
async function viaProxy(url, init = {}) {
const res = await fetch(`${PROXY}/${url}`, {
...init,
headers: {
...(init.headers || {}),
"X-Cors-Proxy-Allowed-Request-Headers": "Authorization",
},
});
if (!res.ok) throw new Error(`Proxy returned ${res.status}`);
return res;
}
const repo = await viaProxy("https://api.github.com/repos/WordPress/php-toolkit").then(r => r.json());
console.log(repo.full_name, repo.stargazers_count);
Deploy behind nginx
The proxy is a single PHP script — any SAPI works. nginx + php-fpm is a common production setup. PATH_INFO is what the proxy reads to learn the target URL.
Put cors-proxy.php, cors-proxy-functions.php, and cors-proxy-config.php in a directory that is not any server's web root, such as /srv/cors-proxy. php-fpm reads the scripts from disk, but nginx never serves files from that directory, so it can't return the config file — which may hold credentials — or the proxy's source as plain text. cors-proxy.php loads cors-proxy-config.php from its own directory, so the config has to sit next to it there.
server {
listen 443 ssl http2;
server_name cors.example.com;
# Keep the "//" in target URLs such as https://example.com.
merge_slashes off;
# Only /cors-proxy.php and /cors-proxy.php/<target URL> reach PHP,
# and they always run this one script.
location ~ ^/cors-proxy\.php(/.*)?$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_split_path_info ^(/cors-proxy\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME /srv/cors-proxy/cors-proxy.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
# Nothing else is served: no static files, no other scripts.
location / {
return 404;
}
}
To keep the config out of the proxy's directory entirely, load it with PHP's auto_prepend_file setting in a php-fpm pool that runs only the proxy, for example php_admin_value[auto_prepend_file] = /etc/cors-proxy/config.php. The setting applies to every script in the pool. Use one location or the other, not both: if two files define playground_cors_proxy_maybe_rate_limit(), PHP stops with a fatal error.
Give the proxy its own origin that carries no cookies, such as a dedicated hostname like cors.example.com. The proxy relays responses from other sites under its own origin, so that origin shouldn't share cookies or other browser state with your applications. Don't serve it from an application's host, and don't set cookies with a Domain attribute that covers its hostname. A separate registrable domain is the simplest way to rule that out.