Skip to content

Commit 1ec3b03

Browse files
f-tochriskacerguis
authored andcommitted
response method update for CORB protection
CORB protection was added to Chromium based browsers to add further cross-origin resource loads protection. Details about the protection here : https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md Without this update, some AJAX requests from those browsers fall in the CORB and fail. The update consists of sending the right Content-Type header for callback encapsuled JSON output : application/javascript instead of application/json
1 parent 169ccd9 commit 1ec3b03

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/RestController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,18 @@ public function response($data = null, $http_code = null, $continue = false)
628628
elseif ($data !== null) {
629629
// If the format method exists, call and return the output in that format
630630
if (method_exists(Format::class, 'to_'.$this->response->format)) {
631-
// Set the format header
632-
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
631+
// CORB protection
632+
// First, get the output content.
633633
$output = Format::factory($data)->{'to_'.$this->response->format}();
634-
634+
635+
// Set the format header
636+
// Then, check if the client asked for a callback, and if the output contains this callback :
637+
if (isset($this->_get_args['callback']) && $this->response->format == 'json' && preg_match('/^' . $this->_get_args['callback'] . '/', $output)) {
638+
$this->output->set_content_type($this->_supported_formats['jsonp'], strtolower($this->config->item('charset')));
639+
} else {
640+
$this->output->set_content_type($this->_supported_formats[$this->response->format], strtolower($this->config->item('charset')));
641+
}
642+
635643
// An array must be parsed as a string, so as not to cause an array to string error
636644
// Json is the most appropriate form for such a data type
637645
if ($this->response->format === 'array') {

0 commit comments

Comments
 (0)