Skip to content

Commit acbee45

Browse files
committed
Switch to headers_requiring_opt_in approach
1 parent 05b264a commit acbee45

File tree

3 files changed

+81
-30
lines changed

3 files changed

+81
-30
lines changed

packages/playground/php-cors-proxy/cors-proxy-functions.php

+22-19
Original file line numberDiff line numberDiff line change
@@ -300,38 +300,41 @@ private static function ipv6InRange($ip, $start, $end)
300300
}
301301

302302

303-
function filter_headers_strings($php_headers, $allowed_headers, $remove_headers) {
304-
$allowed_headers = $allowed_headers ?? [];
303+
function filter_headers_strings($php_headers, $headers_requiring_opt_in, $remove_headers) {
304+
$headers_requiring_opt_in = $headers_requiring_opt_in ?? [];
305305
$remove_headers = $remove_headers ?? [];
306306

307307
$allowed_request_headers_header = strtolower('X-Cors-Proxy-Allowed-Request-Headers');
308308

309-
// Add any additional allowed headers from X-Cors-Proxy-Allowed-Request-Headers
310-
if (isset($php_headers[$allowed_request_headers_header])) {
311-
$allowed_request_headers = $php_headers[$allowed_request_headers_header];
309+
$lowercased_php_headers = array_change_key_case($php_headers, CASE_LOWER);
312310

313-
$additional_headers = array_map(
311+
// Get explicitly allowed headers from X-Cors-Proxy-Allowed-Request-Headers
312+
$explicitly_allowed_headers = [];
313+
if (isset($lowercased_php_headers[$allowed_request_headers_header])) {
314+
$explicitly_allowed_headers = array_map(
314315
'trim',
315-
explode(',', $allowed_request_headers)
316+
explode(',', $lowercased_php_headers[$allowed_request_headers_header])
316317
);
317-
$allowed_headers = array_merge($allowed_headers, $additional_headers);
318318
}
319+
$explicitly_allowed_headers = array_map('strtolower', $explicitly_allowed_headers);
319320

320-
$allowed_headers = array_map('strtolower', $allowed_headers);
321-
322-
// Only keep headers that are in the allowed list
323-
$php_headers = array_filter($php_headers, function($header) use ($allowed_headers) {
324-
$header_name = strtolower(explode(':', $header)[0]);
325-
return in_array($header_name, $allowed_headers);
326-
});
327-
321+
$headers_requiring_opt_in = array_map('strtolower', $headers_requiring_opt_in);
328322
$remove_headers = array_map('strtolower', $remove_headers);
329323

330-
// Remove strictly disallowed headers
324+
// Filter headers
331325
return array_filter(
332326
$php_headers,
333-
function($key) use ($remove_headers) {
334-
return !in_array(strtolower($key), $remove_headers);
327+
function ($key) use ($headers_requiring_opt_in, $remove_headers, $explicitly_allowed_headers) {
328+
$lower_key = strtolower($key);
329+
// Remove if in remove_headers list
330+
if (in_array($lower_key, $remove_headers)) {
331+
return false;
332+
}
333+
// Remove if requires opt-in but not explicitly allowed
334+
if (in_array($lower_key, $headers_requiring_opt_in) && !in_array($lower_key, $explicitly_allowed_headers)) {
335+
return false;
336+
}
337+
return true;
335338
},
336339
ARRAY_FILTER_USE_KEY
337340
);

packages/playground/php-cors-proxy/cors-proxy.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,9 @@
101101
"$host:443:$resolvedIp"
102102
]);
103103

104-
$default_allowed_headers = [
105-
'Accept',
106-
'Accept-Language',
107-
'Content-Language',
108-
'Content-Type',
109-
'DNT',
110-
'Origin',
111-
'Range',
112-
'User-Agent'
104+
// Pass all incoming headers except cookies and authorization
105+
$headers_requiring_opt_in = [
106+
'Authorization'
113107
];
114108
$strictly_disallowed_headers = [
115109
'Cookie',
@@ -118,7 +112,7 @@
118112
$curlHeaders = kv_headers_to_curl_format(
119113
filter_headers_strings(
120114
getallheaders(),
121-
$default_allowed_headers,
115+
$headers_requiring_opt_in,
122116
$strictly_disallowed_headers
123117
)
124118
);

packages/playground/php-cors-proxy/tests/ProxyFunctionsTests.php

+55-1
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,58 @@ public function testUrlValidateAndResolveWithTargetSelf()
147147
'http://cors.playground.wordpress.net/cors-proxy.php?http://cors.playground.wordpress.net'
148148
);
149149
}
150-
}
150+
151+
public function testFilterHeadersStrings()
152+
{
153+
$original_headers = [
154+
'Accept' => 'application/json',
155+
'Content-Type' => 'application/json',
156+
'Cookie' => 'test=1',
157+
'Host' => 'example.com',
158+
];
159+
160+
$headers_requiring_opt_in = [
161+
'Authorization',
162+
];
163+
164+
$strictly_disallowed_headers = [
165+
'Cookie',
166+
'Host',
167+
];
168+
169+
$this->assertEquals([
170+
'Accept' => 'application/json',
171+
'Content-Type' => 'application/json',
172+
], filter_headers_strings($original_headers, $headers_requiring_opt_in, $strictly_disallowed_headers));
173+
}
174+
175+
public function testFilterHeaderStringsWithAdditionalAllowedHeaders()
176+
{
177+
$original_headers = [
178+
'Accept' => 'application/json',
179+
'Content-Type' => 'application/json',
180+
'Cookie' => 'test=1',
181+
'Host' => 'example.com',
182+
'Authorization' => 'Bearer 1234567890',
183+
'X-Authorization' => 'Bearer 1234567890',
184+
'X-Cors-Proxy-Allowed-Request-Headers' => 'Authorization',
185+
];
186+
187+
$headers_requiring_opt_in = [
188+
'Authorization',
189+
];
190+
191+
$strictly_disallowed_headers = [
192+
'Cookie',
193+
'Host',
194+
];
195+
196+
$this->assertEquals([
197+
'Accept' => 'application/json',
198+
'Content-Type' => 'application/json',
199+
'Authorization' => 'Bearer 1234567890',
200+
'X-Authorization' => 'Bearer 1234567890',
201+
'X-Cors-Proxy-Allowed-Request-Headers' => 'Authorization',
202+
], filter_headers_strings($original_headers, $headers_requiring_opt_in, $strictly_disallowed_headers));
203+
}
204+
}

0 commit comments

Comments
 (0)