Documentation
Policies Reference
CORS
Description
The CORS Policy Enables Cross-origin resource sharing (CORS) in Express Gateway. CORS defines a way in which a browser and server can interact and determine whether or not it is safe to allow a cross-origin request.
Usage
To enable the CORS policy, add cors in gateway.config.yml in the policies section.
policies:
- cors
# other policies
Example: excerpt
policies:
- cors:
-
action:
origin: http://www.example.com
credentials: true
Example: full
http:
port: 9089
apiEndpoints:
test_default:
serviceEndpoints:
example: # will be referenced in proxy policy
url: 'http://example.com'
pipelines:
pipeline1:
apiEndpoints: test_default
policies:
-
cors:
-
action:
origin: 'http://www.example.com'
methods: 'HEAD,PUT,PATCH,POST,DELETE'
allowedHeaders: 'X-TEST'
-
proxy:
-
action:
serviceEndpoint: example
Note on security
In case the pipeline you’re putting the cors policy has a security check such as OAuth, Key or Basic you should:
- Put the
corspolicy always on the top of the pipeline before the security check; otherwise theOPTIONScall will likley fail with a401, while the browser expects a204to proceed with the real call - Make sure all the subsequent policies have a condition to make sure they do not try to handle the
OPTIONSmethod
Options Reference
origin:- configures the
Access-Control-Allow-OriginCORS header - Boolean: set origin to
trueto reflect the request origin as defined by req.header(‘Origin’), or set tofalseto disable CORS - String: set origin to a specific origin. example:
http://foobar.comwill allow only requests from “http://foobar.com” - Array: set origin to an array of valid origins. Each origin can be a String or RegExp. example:
[http://foobar1.com", !!js/regexp /\.foobar2.com$]will accept any request from “http://foobar1.com” or from any subdomain of “foobar2.com”
- configures the
methods:- configures the
Access-Control-Allow-MethodsCORS header. - expects a comma-delimited string (ex:
'GET,PUT,POST') or an array (ex:['GET', 'PUT', 'POST']).
- configures the
allowedHeaders:- configures the
Access-Control-Allow-HeadersCORS header. - expects a comma-delimited string (ex:
'Content-Type,Authorization') or an array (ex:['Content-Type', 'Authorization']). - if not specified, defaults to reflecting the headers specified in the request’s
Access-Control-Request-Headersheader.
- configures the
exposedHeaders:- configures the
Access-Control-Expose-HeadersCORS header. - expects a comma-delimited string (ex:
'Content-Range,X-Content-Range') or an array (ex:['Content-Range', 'X-Content-Range']). - if not specified, no custom headers are exposed.
- configures the
credentials:- configures the
Access-Control-Allow-CredentialsCORS header. - set to
trueto pass the header, otherwise it is omitted.
- configures the
maxAge:- configures the
Access-Control-Max-AgeCORS header. - set to an integer to pass the header, otherwise it is omitted.
- configures the
preflightContinue:- pass the CORS preflight response to the next handler.
optionsSuccessStatus:- provides a status code to use for successful
OPTIONSrequests, since some legacy browsers (IE11, various SmartTVs) choke on 204.
- provides a status code to use for successful
The default configuration is the equivalent of:
{
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204
}