Skip to content

Commit 2ce7bbe

Browse files
Merge pull request #200500 from dlepow/corspol
[APIM] CORS policy updates
2 parents 48a6a6c + 184a08b commit 2ce7bbe

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

articles/api-management/api-management-cross-domain-policies.md

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
---
2-
title: Azure API Management cross domain policies | Microsoft Docs
3-
description: Reference for the cross domain policies available for use in Azure API Management. Provides policy usage, settings, and examples.
2+
title: Azure API Management cross-domain policies | Microsoft Docs
3+
description: Reference for the policies in Azure API Management to enable cross-domain calls from various clients. Provides policy usage, settings, and examples.
44
services: api-management
55
author: dlepow
66

77
ms.service: api-management
88
ms.topic: reference
9-
ms.date: 03/07/2022
9+
ms.date: 07/05/2022
1010
ms.author: danlep
1111
---
12-
# API Management cross domain policies
13-
This article provides a reference for API Management policies used to enable cross domain calls from different clients.
12+
# API Management cross-domain policies
13+
This article provides a reference for API Management policies used to enable cross-domain calls from different clients.
1414

1515
[!INCLUDE [api-management-policy-intro-links](../../includes/api-management-policy-intro-links.md)]
1616

17-
## <a name="CrossDomainPolicies"></a> Cross domain policies
17+
## <a name="CrossDomainPolicies"></a> Cross-domain policies
1818

1919
- [Allow cross-domain calls](api-management-cross-domain-policies.md#AllowCrossDomainCalls) - Makes the API accessible from Adobe Flash and Microsoft Silverlight browser-based clients.
2020
- [CORS](api-management-cross-domain-policies.md#CORS) - Adds cross-origin resource sharing (CORS) support to an operation or an API to allow cross-domain calls from browser-based clients.
@@ -63,17 +63,37 @@ This policy can be used in the following policy [sections](./api-management-howt
6363
## <a name="CORS"></a> CORS
6464
The `cors` policy adds cross-origin resource sharing (CORS) support to an operation or an API to allow cross-domain calls from browser-based clients.
6565

66-
> [!NOTE]
67-
> If request matches an operation with an OPTIONS method defined in the API, pre-flight request processing logic associated with CORS policies will not be executed. Therefore, such operations can be used to implement custom pre-flight processing logic.
66+
[!INCLUDE [api-management-policy-form-alert](../../includes/api-management-policy-form-alert.md)]
6867

69-
> [!IMPORTANT]
70-
> If you configure the CORS policy at the product scope, and your API uses subscription key authentication, the policy will only work when requests include a subscription key as a query parameter.
68+
### About CORS
7169

72-
CORS allows a browser and a server to interact and determine whether or not to allow specific cross-origin requests (i.e. XMLHttpRequests calls made from JavaScript on a web page to other domains). This allows for more flexibility than only allowing same-origin requests, but is more secure than allowing all cross-origin requests.
70+
[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is an HTTP header-based standard that allows a browser and a server to interact and determine whether or not to allow specific cross-origin requests (`XMLHttpRequest` calls made from JavaScript on a web page to other domains). This allows for more flexibility than only allowing same-origin requests, but is more secure than allowing all cross-origin requests.
7371

74-
You need to apply the CORS policy to enable the interactive console in the developer portal. Refer to the [developer portal documentation](./developer-portal-faq.md#cors) for details.
72+
CORS specifies two types of [cross-origin requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#specifications):
7573

76-
[!INCLUDE [api-management-policy-form-alert](../../includes/api-management-policy-form-alert.md)]
74+
1. **Preflighted (or "preflight") requests** - The browser first sends an HTTP request using the `OPTIONS` method to the server, to determine if the actual request is permitted to send. If the server response includes the `Access-Control-Allow-Origin` header that allows access, the browser follows with the actual request.
75+
76+
1. **Simple requests** - These requests include one or more extra `Origin` headers but don't trigger a CORS preflight. Only requests using the `GET` and `HEAD` methods and a limited set of request headers are allowed.
77+
78+
### `cors` policy scenarios
79+
80+
Configure the `cors` policy in API Management for the following scenarios:
81+
82+
* Enable the interactive test console in the developer portal. Refer to the [developer portal documentation](./developer-portal-faq.md#cors) for details.
83+
> [!NOTE]
84+
> When you enable CORS for the interactive console, by default API Management configures the `cors` policy at the global scope.
85+
86+
* Enable API Management to reply to preflight requests or to pass through simple CORS requests when the backends don't provide their own CORS support.
87+
88+
> [!NOTE]
89+
> If a request matches an operation with an `OPTIONS` method defined in the API, preflight request processing logic associated with the `cors` policy will not be executed. Therefore, such operations can be used to implement custom preflight processing logic - for example, to apply the `cors` policy only under certain conditions.
90+
91+
### Common configuration issues
92+
93+
* **Subscription key in header** - If you configure the `cors` policy at the *product* scope, and your API uses subscription key authentication, the policy won't work when the subscription key is passed in a header. As a workaround, modify requests to include a subscription key as a query parameter.
94+
* **API with header versioning** - If you configure the `cors` policy at the *API* scope, and your API uses a header-versioning scheme, the policy won't work because the version is passed in a header. You may need to configure an alternative versioning method such as a path or query parameter.
95+
* **Policy order** - You may experience unexpected behavior if the `cors` policy is not the first policy in the inbound section. Select **Calculate effective policy** in the policy editor to check the [policy evaluation order](set-edit-policies.md#use-base-element-to-set-policy-evaluation-order) at each scope. Generally, only the first `cors` policy is applied.
96+
* **Empty 200 OK response** - In some policy configurations, certain cross-origin requests complete with an empty `200 OK` response. This response is expected when `terminate-unmatched-request` is set to its default value of `true` and an incoming request has an `Origin` header that doesn’t match an allowed origin configured in the `cors` policy.
7797

7898
### Policy statement
7999

@@ -95,7 +115,7 @@ You need to apply the CORS policy to enable the interactive console in the devel
95115
```
96116

97117
### Example
98-
This example demonstrates how to support [pre-flight requests](https://developer.mozilla.org/docs/Web/HTTP/CORS#preflighted_requests), such as those with custom headers or methods other than GET and POST. To support custom headers and additional HTTP verbs, use the `allowed-methods` and `allowed-headers` sections as shown in the following example.
118+
This example demonstrates how to support [preflight requests](https://developer.mozilla.org/docs/Web/HTTP/CORS#preflighted_requests), such as those with custom headers or methods other than `GET` and `POST`. To support custom headers and other HTTP verbs, use the `allowed-methods` and `allowed-headers` sections as shown in the following example.
99119

100120
```xml
101121
<cors allow-credentials="true">
@@ -134,11 +154,11 @@ This example demonstrates how to support [pre-flight requests](https://developer
134154
|cors|Root element.|Yes|N/A|
135155
|allowed-origins|Contains `origin` elements that describe the allowed origins for cross-domain requests. `allowed-origins` can contain either a single `origin` element that specifies `*` to allow any origin, or one or more `origin` elements that contain a URI.|Yes|N/A|
136156
|origin|The value can be either `*` to allow all origins, or a URI that specifies a single origin. The URI must include a scheme, host, and port.|Yes|If the port is omitted in a URI, port 80 is used for HTTP and port 443 is used for HTTPS.|
137-
|allowed-methods|This element is required if methods other than GET or POST are allowed. Contains `method` elements that specify the supported HTTP verbs. The value `*` indicates all methods.|No|If this section is not present, GET and POST are supported.|
157+
|allowed-methods|This element is required if methods other than `GET` or `POST` are allowed. Contains `method` elements that specify the supported HTTP verbs. The value `*` indicates all methods.|No|If this section isn't present, `GET` and `POST` are supported.|
138158
|method|Specifies an HTTP verb.|At least one `method` element is required if the `allowed-methods` section is present.|N/A|
139-
|allowed-headers|This element contains `header` elements specifying names of the headers that can be included in the request.|No|N/A|
159+
|allowed-headers|This element contains `header` elements specifying names of the headers that can be included in the request.|Yes|N/A|
140160
|expose-headers|This element contains `header` elements specifying names of the headers that will be accessible by the client.|No|N/A|
141-
|header|Specifies a header name.|At least one `header` element is required in `allowed-headers` or `expose-headers` if the section is present.|N/A|
161+
|header|Specifies a header name.|At least one `header` element is required in `allowed-headers` or in `expose-headers` if that section is present.|N/A|
142162

143163
> [!CAUTION]
144164
> Use the `*` wildcard with care in policy settings. This configuration may be overly permissive and may make an API more vulnerable to certain [API security threats](mitigate-owasp-api-threats.md#security-misconfiguration).
@@ -148,15 +168,19 @@ This example demonstrates how to support [pre-flight requests](https://developer
148168
|Name|Description|Required|Default|
149169
|----------|-----------------|--------------|-------------|
150170
|allow-credentials|The `Access-Control-Allow-Credentials` header in the preflight response will be set to the value of this attribute and affect the client's ability to submit credentials in cross-domain requests.|No|false|
151-
|terminate-unmatched-request|This attribute controls the processing of cross-origin requests that don't match the CORS policy settings. When OPTIONS request is processed as a pre-flight request and doesn't match CORS policy settings: If the attribute is set to `true`, immediately terminate the request with an empty 200 OK response; If the attribute is set to `false`, check inbound for other in-scope CORS policies that are direct children of the inbound element and apply them. If no CORS policies are found, terminate the request with an empty 200 OK response. When GET or HEAD request includes the Origin header (and therefore is processed as a cross-origin request) and doesn't match CORS policy settings: If the attribute is set to `true`, immediately terminate the request with an empty 200 OK response; If the attribute is set to `false`, allow the request to proceed normally and don't add CORS headers to the response.|No|true|
152-
|preflight-result-max-age|The `Access-Control-Max-Age` header in the preflight response will be set to the value of this attribute and affect the user agent's ability to cache pre-flight response.|No|0|
171+
|preflight-result-max-age|The `Access-Control-Max-Age` header in the preflight response will be set to the value of this attribute and affect the user agent's ability to cache the preflight response.|No|0|
172+
|terminate-unmatched-request|Controls the processing of cross-origin requests that don't match the policy settings. When `OPTIONS` request is processed as a preflight request and `Origin` header doesn't match policy settings: If the attribute is set to `true`, immediately terminate the request with an empty `200 OK` response; If the attribute is set to `false`, check inbound for other in-scope `cors` policies that are direct children of the inbound element and apply them. If no `cors` policies are found, terminate the request with an empty `200 OK` response. <br/><br/>When `GET` or `HEAD` request includes the `Origin` header (and therefore is processed as a simple cross-origin request), and doesn't match policy settings: If the attribute is set to `true`, immediately terminate the request with an empty `200 OK` response; If the attribute is set to `false`, allow the request to proceed normally and don't add CORS headers to the response.|No|true|
153173

154174
### Usage
155175
This policy can be used in the following policy [sections](./api-management-howto-policies.md#sections) and [scopes](./api-management-howto-policies.md#scopes).
156176

157177
- **Policy sections:** inbound
158178
- **Policy scopes:** all scopes
159179

180+
#### Usage notes
181+
* You may configure the `cors` policy at more than one scope (for example, at the product scope and the global scope). Ensure that the `base` element is configured at the operation, API, and product scopes to inherit needed policies at the parent scopes.
182+
* Only the `cors` policy is evaluated on the `OPTIONS` request during preflight. Remaining configured policies are evaluated on the approved request.
183+
160184
## <a name="JSONP"></a> JSONP
161185
The `jsonp` policy adds JSON with padding (JSONP) support to an operation or an API to allow cross-domain calls from JavaScript browser-based clients. JSONP is a method used in JavaScript programs to request data from a server in a different domain. JSONP bypasses the limitation enforced by most web browsers where access to web pages must be in the same domain.
162186

articles/api-management/api-management-policies.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ More information about policies:
2121
> [Limit call rate by subscription](api-management-access-restriction-policies.md#LimitCallRate) and [Set usage quota by subscription](api-management-access-restriction-policies.md#SetUsageQuota) have a dependency on the subscription key. A subscription key isn't required when using other policies.
2222
2323

24-
## [Access restriction policies](api-management-access-restriction-policies.md)
24+
## Access restriction policies
2525
- [Check HTTP header](api-management-access-restriction-policies.md#CheckHTTPHeader) - Enforces existence and/or value of an HTTP Header.
2626
- [Get authorization context](api-management-access-restriction-policies.md#GetAuthorizationContext) - Gets the authorization context of a specified [authorization](authorizations-overview.md) configured in the API Management instance.
2727
- [Limit call rate by subscription](api-management-access-restriction-policies.md#LimitCallRate) - Prevents API usage spikes by limiting call rate, on a per subscription basis.
@@ -32,7 +32,7 @@ More information about policies:
3232
- [Validate JWT](api-management-access-restriction-policies.md#ValidateJWT) - Enforces existence and validity of a JWT extracted from either a specified HTTP Header or a specified query parameter.
3333
- [Validate client certificate](api-management-access-restriction-policies.md#validate-client-certificate) - Enforces that a certificate presented by a client to an API Management instance matches specified validation rules and claims.
3434

35-
## [Advanced policies](api-management-advanced-policies.md)
35+
## Advanced policies
3636
- [Control flow](api-management-advanced-policies.md#choose) - Conditionally applies policy statements based on the evaluation of Boolean expressions.
3737
- [Forward request](api-management-advanced-policies.md#ForwardRequest) - Forwards the request to the backend service.
3838
- [Limit concurrency](api-management-advanced-policies.md#LimitConcurrency) - Prevents enclosed policies from executing by more than the specified number of requests at a time.
@@ -50,33 +50,33 @@ More information about policies:
5050
- [Trace](api-management-advanced-policies.md#Trace) - Adds custom traces into the [API Inspector](./api-management-howto-api-inspector.md) output, Application Insights telemetries, and Resource Logs.
5151
- [Wait](api-management-advanced-policies.md#Wait) - Waits for enclosed [Send request](api-management-advanced-policies.md#SendRequest), [Get value from cache](api-management-caching-policies.md#GetFromCacheByKey), or [Control flow](api-management-advanced-policies.md#choose) policies to complete before proceeding.
5252

53-
## [Authentication policies](api-management-authentication-policies.md)
53+
## Authentication policies
5454
- [Authenticate with Basic](api-management-authentication-policies.md#Basic) - Authenticate with a backend service using Basic authentication.
5555
- [Authenticate with client certificate](api-management-authentication-policies.md#ClientCertificate) - Authenticate with a backend service using client certificates.
5656
- [Authenticate with managed identity](api-management-authentication-policies.md#ManagedIdentity) - Authenticate with a backend service using a [managed identity](../active-directory/managed-identities-azure-resources/overview.md).
5757

58-
## [Caching policies](api-management-caching-policies.md)
58+
## Caching policies
5959
- [Get from cache](api-management-caching-policies.md#GetFromCache) - Perform cache lookup and return a valid cached response when available.
6060
- [Store to cache](api-management-caching-policies.md#StoreToCache) - Caches response according to the specified cache control configuration.
6161
- [Get value from cache](api-management-caching-policies.md#GetFromCacheByKey) - Retrieve a cached item by key.
6262
- [Store value in cache](api-management-caching-policies.md#StoreToCacheByKey) - Store an item in the cache by key.
6363
- [Remove value from cache](api-management-caching-policies.md#RemoveCacheByKey) - Remove an item in the cache by key.
6464

65-
## [Cross domain policies](api-management-cross-domain-policies.md)
65+
## Cross-domain policies
6666
- [Allow cross-domain calls](api-management-cross-domain-policies.md#AllowCrossDomainCalls) - Makes the API accessible from Adobe Flash and Microsoft Silverlight browser-based clients.
6767
- [CORS](api-management-cross-domain-policies.md#CORS) - Adds cross-origin resource sharing (CORS) support to an operation or an API to allow cross-domain calls from browser-based clients.
6868
- [JSONP](api-management-cross-domain-policies.md#JSONP) - Adds JSON with padding (JSONP) support to an operation or an API to allow cross-domain calls from JavaScript browser-based clients.
6969

70-
## [Dapr integration policies](api-management-dapr-policies.md)
70+
## Dapr integration policies
7171
- [Send request to a service](api-management-dapr-policies.md#invoke) - uses Dapr runtime to locate and reliably communicate with a Dapr microservice.
7272
- [Send message to Pub/Sub topic](api-management-dapr-policies.md#pubsub) - uses Dapr runtime to publish a message to a Publish/Subscribe topic.
7373
- [Trigger output binding](api-management-dapr-policies.md#bind) - uses Dapr runtime to invoke an external system via output binding.
7474

75-
## [GraphQL API policies](graphql-policies.md)
75+
## GraphQL API policies
7676
- [Validate GraphQL request](graphql-policies.md#validate-graphql-request) - Validates and authorizes a request to a GraphQL API.
7777
- [Set GraphQL resolver](graphql-policies.md#set-graphql-resolver) - Retrieves or sets data for a GraphQL field in an object type specified in a GraphQL schema.
7878

79-
## [Transformation policies](api-management-transformation-policies.md)
79+
## Transformation policies
8080
- [Convert JSON to XML](api-management-transformation-policies.md#ConvertJSONtoXML) - Converts request or response body from JSON to XML.
8181
- [Convert XML to JSON](api-management-transformation-policies.md#ConvertXMLtoJSON) - Converts request or response body from XML to JSON.
8282
- [Find and replace string in body](api-management-transformation-policies.md#Findandreplacestringinbody) - Finds a request or response substring and replaces it with a different substring.
@@ -88,7 +88,7 @@ More information about policies:
8888
- [Rewrite URL](api-management-transformation-policies.md#RewriteURL) - Converts a request URL from its public form to the form expected by the web service.
8989
- [Transform XML using an XSLT](api-management-transformation-policies.md#XSLTransform) - Applies an XSL transformation to XML in the request or response body.
9090

91-
## [Validation policies](validation-policies.md)
91+
## Validation policies
9292
- [Validate content](validation-policies.md#validate-content) - Validates the size or JSON schema of a request or response body against the API schema.
9393
- [Validate parameters](validation-policies.md#validate-parameters) - Validates the request header, query, or path parameters against the API schema.
9494
- [Validate headers](validation-policies.md#validate-headers) - Validates the response headers against the API schema.

0 commit comments

Comments
 (0)