CORS
Last updated
Was this helpful?
Last updated
Was this helpful?
Grey Matter supports the configuration of cross-origin resource sharing on a sidecar. CORS can be configured to allow an application to access resources at a different origin (domain, protocol, or port) than its own.
For more information on CORS, see this .
Simple requests are classified as requests that don't require a . This distinction is made between requests that might be dangerous (i.e. modifies server resources) and those that are most likely benign. A request is considered simple when all of the following criteria is true:
The method is GET
, HEAD
, or POST
Only are present
The Content Type
header is set to one of application/x-www-form-urlencoded
or multipart/form-data
or text/plain
A more comprehensive list and explanation can be found .
As an example, say an app running at http://localhost:8080
is trying to call a backend service with a Grey Matter sidecar at http://localhost:10808
. Without a CORS configuration, this request would fail, because the app localhost:8080
is trying to access resources from a server at a different origin, localhost:10808
. To solve this, the following CORS config is set on its domain:
With this configuration, if a simple request comes in to the sidecar from the app, it will have an Origin
header value of http://localhost:8080
, and this request will succeed. The server will attach a header Access-Control-Allow-Origin: http://localhost:8080
to the response, which signals to the browser that this request is allowed.
In the above configuration, CORS will allow requests with Origin
header value http://localhost:8080
only and indicate that the content-type header can be set according to the request.
For an existing domain, run
and add the desired cors_config
object.
allowed_origins
Available matchers include:
exact
prefix
suffix
regex
Example:
A wildcard value *
is allowed except when using the regex
matcher.
allow_credentials
exposed_headers
max_age
allowed_methods
allowed_headers
By default, the proxy will use the upstream service's CORS policy on the gateway and on the upstream service. The gateway policy is ignored.
Because CORS is a browser construct, curl can always make a request to the server, with or without CORS. However, it can be used to mimic a browser and verify how the proxy will react to CORS requests:
are initiated by the browser using the OPTIONS
HTTP method before sending a request in order to determine if the real request is safe to send. The response to this kind of request contains information about what is allowed from a request, and the server determines whether or not to send the actual request based on this information.
This response information is in the form of three HTTP headers, access-control-request-method
, access-control-request-headers
, and the origin
header. These correspond to of the cors_config
- thus these configurations can be specified to determine how the Grey Matter sidecar will respond to preflight requests. If a preflight request comes in to the Grey Matter Sidecar that does not meet the specification for one of these configured fields, the sidecar will send back a response to not initiate the request.
Based on the same , say the app running at http://localhost:8080
wants to send requests to the backend service at http://localhost:10808
with a content-type of application/json;charset=UTF-8
. This particular content-type is outside of those allowed by CORS for simple requests, and thus would result in sending a preflight request to determine if the request can be sent. In order for the CORS configuration to indicate that the request can be sent, it would need to allow the content-type
header by configuring the field:
To set up CORS, set the cors_config
field on a domain object with the desired configuration, see the below.
This field specifies an array of string patterns that match allowed origins. The proxy will use these matchers to set the header. This header will be set on any cross-origin response that matches one of the allowed_origins
.
Specifies the content for the header that the proxy will set on any cross-origin request that matches one of the allowed_origins
. This header specifies whether or not the upstream service allows credentials.
Specifies the content for the header that the proxy will set on any cross-origin request that matches one of the allowed_origins
. This header specifies an array of headers that are allowed on the response.
Specifies the content for the header that the proxy will set on the preflight response. This header is an integer value specifying how long a preflight request can be cached by the browser.
Specifies the content for the header that the proxy will set on the preflight response. This header specifies an array of methods allowed by the upstream service.
Specifies the content for the header that the proxy will set on the preflight response. This header specifies an array of headers allowed by the upstream service.