Skip to content

Commit 4c42800

Browse files
authored
fix(cloudfront): add validations on ResponseHeadersCorsBehavior.accessControlAllowMethods (#32769)
### Issue # (if applicable) N/A ### Reason for this change There is no description about the allowed values on `ResponseHeadersPolicy`'s `corsBehavior.accessControlAllowMethods`. The wildcard (any methods allowed) is `ALL` instead of `*` (wildcard for headers). This PR adds the description and validations on it. ### Description of changes Added the description of the allowed values on `ResponseHeadersCorsBehavior.accessControlAllowMethods`. Added validations: - exactly `['ALL']` which means any http methods are allowed - whether includes only allowed method names ### Describe any new or updated permissions being added Nothing ### Description of how you validated changes Added unit tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 035d17d commit 4c42800

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/response-headers-policy.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Construct } from 'constructs';
22
import { CfnResponseHeadersPolicy } from './cloudfront.generated';
3-
import { Duration, Names, Resource, Token } from '../../core';
3+
import { Duration, Names, Resource, Token, withResolved } from '../../core';
44

55
/**
66
* Represents a response headers policy.
@@ -130,6 +130,15 @@ export class ResponseHeadersPolicy extends Resource implements IResponseHeadersP
130130
}
131131

132132
private _renderCorsConfig(behavior: ResponseHeadersCorsBehavior): CfnResponseHeadersPolicy.CorsConfigProperty {
133+
withResolved(behavior.accessControlAllowMethods, (methods) => {
134+
const allowedMethods = ['GET', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'ALL'];
135+
if (methods.includes('ALL') && methods.length !== 1) {
136+
throw new Error("accessControlAllowMethods - 'ALL' cannot be combined with specific HTTP methods.");
137+
} else if (!methods.every((method) => Token.isUnresolved(method) || allowedMethods.includes(method))) {
138+
throw new Error(`accessControlAllowMethods contains unexpected method name; allowed values: ${allowedMethods.join(', ')}`);
139+
}
140+
});
141+
133142
return {
134143
accessControlAllowCredentials: behavior.accessControlAllowCredentials,
135144
accessControlAllowHeaders: { items: behavior.accessControlAllowHeaders },
@@ -211,6 +220,9 @@ export interface ResponseHeadersCorsBehavior {
211220

212221
/**
213222
* A list of HTTP methods that CloudFront includes as values for the Access-Control-Allow-Methods HTTP response header.
223+
*
224+
* Allowed methods: `'GET'`, `'DELETE'`, `'HEAD'`, `'OPTIONS'`, `'PATCH'`, `'POST'`, and `'PUT'`.
225+
* You can specify `['ALL']` to allow all methods.
214226
*/
215227
readonly accessControlAllowMethods: string[];
216228

packages/aws-cdk-lib/aws-cloudfront/test/response-headers-policy.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,30 @@ describe('ResponseHeadersPolicy', () => {
180180
},
181181
});
182182
});
183+
184+
describe('corsBehavior', () => {
185+
test('throws if accessControlAllowMethods is mixed with `ALL` and other values', () => {
186+
expect(() => new ResponseHeadersPolicy(stack, 'ResponseHeadersPolicy', {
187+
corsBehavior: {
188+
accessControlAllowCredentials: false,
189+
accessControlAllowHeaders: ['*'],
190+
accessControlAllowMethods: ['ALL', 'GET'],
191+
accessControlAllowOrigins: ['*'],
192+
originOverride: true,
193+
},
194+
})).toThrow("accessControlAllowMethods - 'ALL' cannot be combined with specific HTTP methods.");
195+
});
196+
197+
test('throws if accessControlAllowMethods contains unallowed value', () => {
198+
expect(() => new ResponseHeadersPolicy(stack, 'ResponseHeadersPolicy', {
199+
corsBehavior: {
200+
accessControlAllowCredentials: false,
201+
accessControlAllowHeaders: ['*'],
202+
accessControlAllowMethods: ['PROPFIND'],
203+
accessControlAllowOrigins: ['*'],
204+
originOverride: true,
205+
},
206+
})).toThrow(/accessControlAllowMethods contains unexpected method name/);
207+
});
208+
});
183209
});

0 commit comments

Comments
 (0)