Skip to content

Adding allowlist content types #2180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
34 changes: 32 additions & 2 deletions packages/playground/php-cors-proxy/cors-proxy.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,18 @@
$server_host = $_SERVER['HTTP_HOST'] ?? '';
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';

// Define allowlist of Content-Types
$allowed_content_types = [
'application/json',
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's also non-standard text/json and text/javascript. Also, there are image content types, font content types, I wonder what else

'application/xml',
'application/rss+xml',
'application/atom+xml',
'text/html',
'text/plain',
'text/xml',
// Add more as needed
];

if (should_respond_with_cors_headers($server_host, $origin)) {
header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Credentials: true');
@@ -149,7 +161,7 @@ function should_send_as_chunked_response() {
filter_headers_by_name(
getallheaders(),
$strictly_disallowed_headers,
$headers_requiring_opt_in,
$headers_requiring_opt_in
)
);
curl_setopt(
@@ -181,7 +193,7 @@ function(
) use (
$targetUrl,
$relay_http_code_and_initial_headers_if_not_already_sent,
&$is_chunked_response
&$is_chunked_response, $allowed_content_types
) {
@$relay_http_code_and_initial_headers_if_not_already_sent();

@@ -205,6 +217,24 @@ function(
return $len;
}

// Check if Content-Type is allowed
if ($name === 'content-type') {
$mimeType = strtolower(trim(explode(';', $value)[0])); // Normalize and strip charset
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there any way ; would be there for different reasons than the charset?


$isGitContent = str_starts_with($mimeType, 'application/x-git-');

// If not in allowlist and not a special case, block it
if (
!in_array($mimeType, $allowed_content_types, true) &&
$mimeType !== 'application/octet-stream' &&
!$isGitContent
) {
http_response_code(415); // Unsupported Media Type
send_response_chunk("Unsupported Content-Type: $mimeType");
exit;
}
}

if ($name === 'transfer-encoding' && stripos($value, 'chunked') !== false) {
$is_chunked_response = true;
header($header, false);
Loading