# CORS Configuration

The `cors` plugin enables cross-origin resource sharing, allowing servers to specify permitted origins and instructing browsers to load resources from those origins, enhancing API accessibility.<!-- --> <!-- -->Configuration covers plugin parameters and plugin metadata.

## Parameters[​](#parameters "Direct link to Parameters")

See plugin [common configurations](https://docs.api7.ai/apisix/reference/plugin-common-configurations.md) for configuration options available to all plugins.

* allow\_origins

  string

  default: `*`

  ***

  Comma-separated string of origins to allow CORS.

  If `allow_credential` is set to `true`, you can forcefully allow CORS on all origins by configuring the field to `**` but sensitive data, such as authentication tokens or cookies, can get exposed to any malicious website.

  You can also configure allow origins on a global scale using the [plugin metadata](https://docs.api7.ai/apisix/key-concepts/plugin-metadata.md), which configures the allow origins for all `cors` plugin instances. See the [example](https://docs.api7.ai/hub/cors.md#configure-origins-in-plugin-metadata) for more details.

* allow\_methods

  string

  default: `*`

  ***

  Comma-separated string of HTTP request methods to allow CORS.

  If `allow_credential` is set to `true`, you can forcefully allow CORS on all methods by configuring the field to `**`, but a malicious actor can use HTTP methods, such as `PUT` or `DELETE`, to make unexpected modifications to shared resource and pose a security threat.

* allow\_headers

  string

  default: `*`

  ***

  Comma-separated string of HTTP headers allowed in requests.

  If `allow_credential` is set to `true`, you can forcefully allow CORS on all request headers by configuring the field to `**`, but it can potentially allow malicious headers to be sent to the server.

* expose\_headers

  string

  ***

  Comma-separated string of HTTP headers that should be made available in response to a cross-origin request.

* max\_age

  integer

  default: `5`

  ***

  Maximum time in seconds for which the results of a [preflight request](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request) can be cached. If the time is within this limit, the browser will check the cached result. To disable caching, set `max_age` to `-1`.

  Note that the maximum value allowed is browser-dependent. See [`Access-Control-Max-Age`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age#Directives) for more details.

* allow\_credential

  boolean

  ***

  If true, allow requests to include credentials, such as cookies. According to CORS specification, when `allow_credential` is set to true, you cannot use `*` for other CORS attributes.

  To allow all origins, set the field to `**`. This can potentially allow sensitive user data, such as authentication tokens or cookies, to be exposed to malicious actors.

* allow\_origins\_by\_regex

  array\[string]

  ***

  Regular expressions used to match origins that allow CORS. When configured, these expressions replace literal matching through `allow_origins`.

  If `allow_origins_by_metadata` is also configured, named metadata origins are evaluated first. The regular expressions are used as a fallback only when `allow_origins` is explicitly set to a value other than `*`.

  For example, `['.*\.test.com$']` can match all subdomains of `test.com`.

* allow\_origins\_by\_metadata

  array\[string]

  ***

  Origins to enable CORS referenced from `allow_origins` set in the plugin metadata. For example, if `allow_origins: {'EXAMPLE': 'https://example.com'}` is set in the plugin metadata, then `['EXAMPLE']` can be used to allow CORS on the origin `https://example.com`.

  Named metadata origins are evaluated before local origin settings. If none match, local literal or regular-expression matching is available only when `allow_origins` is explicitly set to a value other than `*`.

* timing\_allow\_origins

  string

  ***

  Comma-separated string of origins to allow to access the resource timing information. See [`Timing-Allow-Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin) for more details.

* timing\_allow\_origins\_by\_regex

  array\[string]

  ***

  RegEx to match with origin for enabling access to the resource timing information. When configured, only domains matching the RegEx will be allowed and any configuration in `timing_allow_origins` will be ignored.

  For example, `['.*\.test.com']` can match all subdomain of `test.com`.

## Plugin Metadata[​](#plugin-metadata "Direct link to Plugin Metadata")

* allow\_origins

  object

  ***

  A map of named origins to allow for CORS, where each key is an identifier referenced by `allow_origins_by_metadata` and each value is the corresponding origin string.

  For example, `{'EXAMPLE': 'https://example.com'}` defines the key `EXAMPLE` for the origin `https://example.com`.

  If `allow_credential` is set to `true`, you can forcefully allow CORS on all origins by setting a map value to `**`, but sensitive data, such as authentication tokens or cookies, can get exposed to any malicious website.
