Skip to content

Commit

Permalink
Added "Allow-Credentials" in static $cors and addCorsHeaders() (#227)
Browse files Browse the repository at this point in the history
* Added "Allow-Credentials" in static $cors and addCorsHeaders() to allow modification in yaml

* Changed the default Allow-Credentials value to empty

* Updated test to include Allow-Credentials and ensure Access-Control-Allow-Credentials is only set when it is included in config

* Updated README.md to include Allow-Credentials information
  • Loading branch information
syanaputra authored and chillu committed Mar 15, 2019
1 parent 4bce29d commit 9c46d49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -2131,6 +2131,26 @@ Once you have enabled CORS you can then control four new headers in the HTTP Res
Max-Age: 600
```

5. **Access-Control-Allow-Credentials.**

When a request's credentials mode (Request.credentials) is "include", browsers
will only expose the response to frontend JavaScript code if the
Access-Control-Allow-Credentials value is true.

The Access-Control-Allow-Credentials header works in conjunction with the
XMLHttpRequest.withCredentials property or with the credentials option in the
Request() constructor of the Fetch API. For a CORS request with credentials,
in order for browsers to expose the response to frontend JavaScript code, both
the server (using the Access-Control-Allow-Credentials header) and the client
(by setting the credentials mode for the XHR, Fetch, or Ajax request) must
indicate that they’re opting in to including credentials.

This is set to empty by default but can be changed in YAML as in this example:

```yaml
Allow-Credentials: 'true'
```

### Sample Custom CORS Config

```yaml
Expand All @@ -2141,6 +2161,7 @@ SilverStripe\GraphQL\Controller:
Allow-Origin: 'silverstripe.org'
Allow-Headers: 'Authorization, Content-Type'
Allow-Methods: 'GET, POST, OPTIONS'
Allow-Credentials: 'true'
Max-Age: 600 # 600 seconds = 10 minutes.
```
## Persisting queries
Expand Down
5 changes: 5 additions & 0 deletions src/Controller.php
Expand Up @@ -40,6 +40,7 @@ class Controller extends BaseController implements Flushable
'Allow-Origin' => [], // List of all allowed origins; Deny by default
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400, // 86,400 seconds = 1 day.
];

Expand Down Expand Up @@ -224,6 +225,10 @@ public function addCorsHeaders(HTTPRequest $request, HTTPResponse $response)
$response->addHeader('Access-Control-Allow-Methods', $corsConfig['Allow-Methods']);
$response->addHeader('Access-Control-Max-Age', $corsConfig['Max-Age']);

if (isset($corsConfig['Allow-Credentials'])) {
$response->addHeader('Access-Control-Allow-Credentials', $corsConfig['Allow-Credentials']);
}

return $response;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ControllerTest.php
Expand Up @@ -180,6 +180,7 @@ public function testAddCorsHeadersOriginDisallowed()
'Allow-Origin' => null,
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand All @@ -200,6 +201,7 @@ public function testAddCorsHeadersOriginAllowed()
'Allow-Origin' => 'http://localhost',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand All @@ -226,6 +228,7 @@ public function testAddCorsHeadersRefererAllowed()
'Allow-Origin' => 'http://localhost',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand All @@ -252,6 +255,7 @@ public function testAddCorsHeadersRefererPortAllowed()
'Allow-Origin' => 'http://localhost:8181',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand Down Expand Up @@ -283,6 +287,7 @@ public function testAddCorsHeadersRefererPortDisallowed()
'Allow-Origin' => 'http://localhost:9090',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand All @@ -300,6 +305,7 @@ public function testAddCorsHeadersOriginAllowedWildcard()
'Allow-Origin' => '*',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, PUT, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 600
]);

Expand All @@ -323,6 +329,7 @@ public function testAddCorsHeadersOriginMissing()
'Allow-Origin' => 'localhost',
'Allow-Headers' => 'Authorization, Content-Type',
'Allow-Methods' => 'GET, POST, OPTIONS',
'Allow-Credentials' => '',
'Max-Age' => 86400
]);

Expand Down

0 comments on commit 9c46d49

Please sign in to comment.