Skip to content

Commit 0f06223

Browse files
Elad Ben-Israelmergify[bot]
authored andcommitted
feat(apigateway): cors preflight support (#4211)
* feat(apigateway): cors support (wip) adds support for CORS preflight (OPTIONS) requests on resources. currently supports: origin, methods, headers, credentials, status code implemented during a live twitch stream on sep 24, 2019 * export types from cors.ts * allow 'ANY' to be used in `allowMethods` * add a bunch of unit tests * document api * support multiple origins * add maxAge + disableCache * document addCorsPreflight * exposeHeaders * doc cleanups * reorder methods * add integ test expectation * defaultCorsPreflightOptions * README * add missing features link * update integ test expectation * fix compilation error * respond with Vary if specific origin is specified * update expectation
1 parent 8f4a38d commit 0f06223

File tree

11 files changed

+1664
-7
lines changed

11 files changed

+1664
-7
lines changed

packages/@aws-cdk/aws-apigateway/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,61 @@ new route53.ARecord(this, 'CustomDomainAliasRecord', {
466466
});
467467
```
468468

469+
### Cross Origin Resource Sharing (CORS)
470+
471+
[Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is a mechanism
472+
that uses additional HTTP headers to tell browsers to give a web application
473+
running at one origin, access to selected resources from a different origin. A
474+
web application executes a cross-origin HTTP request when it requests a resource
475+
that has a different origin (domain, protocol, or port) from its own.
476+
477+
You can add the CORS [preflight](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests) OPTIONS HTTP method to any API resource via the `addCorsPreflight` method.
478+
479+
The following example will add an OPTIONS method to the `myResource` API resource, which
480+
only allows GET and PUT HTTP requests from the origin https://amazon.com.
481+
482+
```ts
483+
myResource.addCorsPreflight({
484+
allowOrigins: [ 'https://amazon.com' ],
485+
allowMethods: [ 'GET', 'PUT' ]
486+
});
487+
```
488+
489+
See the
490+
[`CorsOptions`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-apigateway.CorsOptions.html)
491+
API reference for a detailed list of supported configuration options.
492+
493+
You can specify default options for all resources within an API or a sub-tree using
494+
`defaultCorsPreflightOptions`:
495+
496+
497+
```ts
498+
new apigateway.RestApi(this, 'api', {
499+
defaultCorsPreflightOptions: {
500+
allowOrigins: [ 'https://amazon.com' ]
501+
}
502+
});
503+
```
504+
505+
This means that the construct will add a CORS preflight OPTIONS method to
506+
**all** HTTP resources in this API gateway.
507+
508+
Similarly, you can specify this at the resource level:
509+
510+
```ts
511+
const subtree = resource.addResource('subtree', {
512+
defaultCorsPreflightOptions: {
513+
allowOrigins: [ 'https://amazon.com' ]
514+
}
515+
});
516+
```
517+
518+
This means that all resources under `subtree` (inclusive) will have a preflight
519+
OPTIONS added to them.
520+
521+
See [#906](https://github.com/aws/aws-cdk/issues/906) for a list of CORS
522+
features which are not yet supported.
523+
469524
----
470525

471526
This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { Duration } from '@aws-cdk/core';
2+
import { ALL_METHODS } from './util';
3+
4+
export interface CorsOptions {
5+
/**
6+
* Specifies the response status code returned from the OPTIONS method.
7+
*
8+
* @default 204
9+
*/
10+
readonly statusCode?: number;
11+
12+
/**
13+
* The Access-Control-Allow-Origin response header indicates whether the
14+
* response can be shared with requesting code from the given origin.
15+
*
16+
* Specifies the list of origins that are allowed to make requests to this resource.
17+
*
18+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
19+
*/
20+
readonly allowOrigins: string[];
21+
22+
/**
23+
* The Access-Control-Allow-Headers response header is used in response to a
24+
* preflight request which includes the Access-Control-Request-Headers to
25+
* indicate which HTTP headers can be used during the actual request.
26+
*
27+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
28+
* @default Cors.DEFAULT_HEADERS
29+
*/
30+
readonly allowHeaders?: string[];
31+
32+
/**
33+
* The Access-Control-Allow-Methods response header specifies the method or
34+
* methods allowed when accessing the resource in response to a preflight request.
35+
*
36+
* If `ANY` is specified, it will be expanded to `Cors.ALL_METHODS`.
37+
*
38+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
39+
* @default Cors.ALL_METHODS
40+
*/
41+
readonly allowMethods?: string[];
42+
43+
/**
44+
* The Access-Control-Allow-Credentials response header tells browsers whether
45+
* to expose the response to frontend JavaScript code when the request's
46+
* credentials mode (Request.credentials) is "include".
47+
*
48+
* When a request's credentials mode (Request.credentials) is "include",
49+
* browsers will only expose the response to frontend JavaScript code if the
50+
* Access-Control-Allow-Credentials value is true.
51+
*
52+
* Credentials are cookies, authorization headers or TLS client certificates.
53+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
54+
* @default false
55+
*/
56+
readonly allowCredentials?: boolean;
57+
58+
/**
59+
* The Access-Control-Max-Age response header indicates how long the results of
60+
* a preflight request (that is the information contained in the
61+
* Access-Control-Allow-Methods and Access-Control-Allow-Headers headers)
62+
* can be cached.
63+
*
64+
* To disable caching altogther use `disableCache: true`.
65+
*
66+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
67+
* @default - browser-specific (see reference)
68+
*/
69+
readonly maxAge?: Duration;
70+
71+
/**
72+
* Sets Access-Control-Max-Age to -1, which means that caching is disabled.
73+
* This option cannot be used with `maxAge`.
74+
*
75+
* @default - cache is enabled
76+
*/
77+
readonly disableCache?: boolean;
78+
79+
/**
80+
* The Access-Control-Expose-Headers response header indicates which headers
81+
* can be exposed as part of the response by listing their names.
82+
*
83+
* If you want clients to be able to access other headers, you have to list
84+
* them using the Access-Control-Expose-Headers header.
85+
*
86+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
87+
*
88+
* @default - only the 6 CORS-safelisted response headers are exposed:
89+
* Cache-Control, Content-Language, Content-Type, Expires, Last-Modified,
90+
* Pragma
91+
*/
92+
readonly exposeHeaders?: string[];
93+
}
94+
95+
export class Cors {
96+
/**
97+
* All HTTP methods.
98+
*/
99+
public static readonly ALL_METHODS = ALL_METHODS;
100+
101+
/**
102+
* The set of default headers allowed for CORS and useful for API Gateway.
103+
*/
104+
public static readonly DEFAULT_HEADERS = [ 'Content-Type', 'X-Amz-Date', 'Authorization', 'X-Api-Key', 'X-Amz-Security-Token', 'X-Amz-User-Agent' ];
105+
}

packages/@aws-cdk/aws-apigateway/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './authorizer';
1616
export * from './json-schema';
1717
export * from './domain-name';
1818
export * from './base-path-mapping';
19+
export * from './cors';
1920

2021
// AWS::ApiGateway CloudFormation Resources:
2122
export * from './apigateway.generated';

0 commit comments

Comments
 (0)