Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 48 additions & 0 deletions packages/playground/php-cors-proxy/cors-proxy-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,54 @@ function kv_headers_to_curl_format($headers) {
return $curl_headers;
}

/**
* Answers whether a response header instructs the web server or CDN in
* front of PHP rather than the client.
*
* For example, nginx performs an internal redirect when PHP sends
* X-Accel-Redirect. The proxy must not relay these from a target, or the
* target could control the proxy's own server.
*/
function is_server_control_response_header($name) {
$name = strtolower($name);
$prefixes = [
// nginx: X-Accel-Redirect, X-Accel-Expires, X-Accel-Buffering, etc.
'x-accel-',
// LiteSpeed: X-LiteSpeed-Location, X-LiteSpeed-Cache-Control, etc.
'x-litespeed-',
];
foreach ($prefixes as $prefix) {
if (str_starts_with($name, $prefix)) {
return true;
}
}
// CDN-targeted cache control. RFC 9213 defines CDN-Cache-Control and
// names CDN-specific variants the same way, e.g.
// Cloudflare-CDN-Cache-Control. A CDN obeys these over Cache-Control, so
// relaying one would override the proxy's Cache-Control: no-cache.
if (
$name === 'cdn-cache-control' ||
str_ends_with($name, '-cdn-cache-control')
) {
return true;
}
return in_array($name, [
// Apache mod_xsendfile. X-Sendfile-Temporary also deletes the file
// after sending it when the server allows that.
'x-sendfile',
'x-sendfile-temporary',
// lighttpd.
'x-sendfile2',
'x-lighttpd-send-file',
// Other cache directives aimed at CDNs rather than browsers.
'surrogate-control',
'edge-control',
// CGI and FastCGI servers take the response status from this header,
// overriding the status PHP would send.
'status',
], true);
}

function rewrite_relative_redirect(
$request_url,
$redirect_location,
Expand Down
4 changes: 4 additions & 0 deletions packages/playground/php-cors-proxy/cors-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ function(
return $len;
}

if (is_server_control_response_header($name)) {
return $len;
}

if ($name === 'transfer-encoding' && stripos($value, 'chunked') !== false) {
$is_chunked_response = true;
header($header, false);
Expand Down
30 changes: 30 additions & 0 deletions packages/playground/php-cors-proxy/tests/ProxyFunctionsTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,36 @@ static public function providerGetTargetUrl() {
],
];
}

/**
* @dataProvider providerServerControlResponseHeaders
*/
public function testIsServerControlResponseHeader($name, $expected)
{
$this->assertSame($expected, is_server_control_response_header($name));
}

static public function providerServerControlResponseHeaders() {
return [
'X-Accel- prefix' => ['X-Accel-Redirect', true],
'X-LiteSpeed- prefix' => ['X-LiteSpeed-Location', true],
'Prefix matching ignores case' => ['x-accel-expires', true],
'Exact name' => ['X-Sendfile2', true],
'CDN-targeted cache control, RFC 9213 naming' => [
'Cloudflare-CDN-Cache-Control',
true,
],
'CGI status instruction' => ['Status', true],
'Similar name without the prefix hyphen' => ['X-Accelerated-By', false],
'Similar name with a suffix after an exact name' => ['X-Sendfile-Foo', false],
'Similar name that only contains the CDN suffix' => [
'CDN-Cache-Control-Extension',
false,
],
'Ordinary header' => ['Content-Type', false],
];
}

public function testGetCurrentScriptUri()
{
$this->assertEquals('http://localhost/cors-proxy/', get_current_script_uri('http://example.com', 'http://localhost/cors-proxy/http://example.com'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,27 @@ function assert_not_contains($needle, $haystack, $message) {
);
assert_only_proxy_headers($response, '502');

// ──────────────────────────────────────────────
// Test 14: Server-control response headers are not relayed
// ──────────────────────────────────────────────
echo "\nTest 14: Server-control response headers are not relayed\n";
// The web server or CDN in front of the proxy would act on these, e.g.
// nginx performs an internal redirect for X-Accel-Redirect.
$response = proxy_request(
$proxy_port,
"http://127.0.0.1:$upstream_port/server-control-headers"
);
foreach (['x-accel-redirect', 'x-sendfile', 'surrogate-control'] as $name) {
assert_true(
get_header_list($response['headers_raw'], $name) === [],
"Response should not relay the target's $name header"
);
}
assert_true(
get_header_list($response['headers_raw'], 'content-type') === ['application/octet-stream'],
"Response should still relay the target's Content-Type"
);

// ──────────────────────────────────────────────
// Clean up
// ──────────────────────────────────────────────
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
flush();
break;

case '/server-control-headers':
// Headers that instruct the web server or CDN in front of PHP.
header('Content-Type: application/octet-stream');
header('X-Accel-Redirect: /internal/secret');
header('X-Sendfile: /etc/passwd');
header('Surrogate-Control: max-age=3600');
echo 'body';
break;

case '/headers':
header('Content-Type: application/json');
echo json_encode(array_change_key_case(getallheaders(), CASE_LOWER));
Expand Down
Loading