Skip to content

Commit

Permalink
exposed headers
Browse files Browse the repository at this point in the history
  • Loading branch information
daycry committed Jun 28, 2023
1 parent 3d2d44b commit f65263c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Config/RestFul.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class RestFul extends BaseConfig
* Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any
* source domain
*/
public bool $allowAnyCorsDomain = true;
public bool $allowAnyCorsDomain = false;

/**
* --------------------------------------------------------------------------
Expand Down Expand Up @@ -261,7 +261,7 @@ class RestFul extends BaseConfig
| http://docs.sencha.com/extjs/6.5.2/classic/Ext.data.proxy.Rest.html#cfg-withCredentials
|
*/
public array $forcedCorsHeaders = [ 'Access-Control-Allow-Credentials' => 'true' ];
public bool $supportsCredentials = false;

/**
* --------------------------------------------------------------------------
Expand Down
14 changes: 8 additions & 6 deletions src/Validators/Cors.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public static function check(ResponseInterface &$response)
$response->setHeader('Access-Control-Allow-Headers', $allowedCorsHeaders);
$response->setHeader('Access-Control-Allow-Methods', $allowedCorsMethods);

$forcedheaders = service('settings')->get('RestFul.forcedCorsHeaders');
// If there are headers that should be forced in the CORS check, add them now
if (is_array($forcedheaders)) {
foreach ($forcedheaders as $header => $value) {
$response->setHeader($header, $value);
}
$response->setHeader('Access-Control-Expose-Headers', implode(', ', service('settings')->get('RestFul.exposedCorsHeaders')));

if (service('settings')->get('RestFul.corsMaxAge') !== null) {
$response = $response->setHeader('Access-Control-Max-Age', (string) service('settings')->get('RestFul.corsMaxAge'));
}

if (service('settings')->get('RestFul.supportsCredentials')) {
$response = $response->setHeader('Access-Control-Allow-Credentials', 'true');
}

}
Expand Down
2 changes: 1 addition & 1 deletion tests/Validators/CorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testCorsAllowCustomDomainError(): void
$result = $this->call('get', 'example');

$result->assertHeaderMissing('Access-Control-Allow-Origin');
$result->assertHeader('Access-Control-Allow-Credentials');
$result->assertHeaderMissing('Access-Control-Allow-Credentials');
}

public function testCorsOptionsMethodError(): void
Expand Down

0 comments on commit f65263c

Please sign in to comment.