Skip to content

Commit

Permalink
fix(cloudfront): add validations on ResponseHeadersCorsBehavior.acces…
Browse files Browse the repository at this point in the history
…sControlAllowMethods (#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*
  • Loading branch information
Tietew authored Jan 20, 2025
1 parent 035d17d commit 4c42800
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct } from 'constructs';
import { CfnResponseHeadersPolicy } from './cloudfront.generated';
import { Duration, Names, Resource, Token } from '../../core';
import { Duration, Names, Resource, Token, withResolved } from '../../core';

/**
* Represents a response headers policy.
Expand Down Expand Up @@ -130,6 +130,15 @@ export class ResponseHeadersPolicy extends Resource implements IResponseHeadersP
}

private _renderCorsConfig(behavior: ResponseHeadersCorsBehavior): CfnResponseHeadersPolicy.CorsConfigProperty {
withResolved(behavior.accessControlAllowMethods, (methods) => {
const allowedMethods = ['GET', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'ALL'];
if (methods.includes('ALL') && methods.length !== 1) {
throw new Error("accessControlAllowMethods - 'ALL' cannot be combined with specific HTTP methods.");
} else if (!methods.every((method) => Token.isUnresolved(method) || allowedMethods.includes(method))) {
throw new Error(`accessControlAllowMethods contains unexpected method name; allowed values: ${allowedMethods.join(', ')}`);
}
});

return {
accessControlAllowCredentials: behavior.accessControlAllowCredentials,
accessControlAllowHeaders: { items: behavior.accessControlAllowHeaders },
Expand Down Expand Up @@ -211,6 +220,9 @@ export interface ResponseHeadersCorsBehavior {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,30 @@ describe('ResponseHeadersPolicy', () => {
},
});
});

describe('corsBehavior', () => {
test('throws if accessControlAllowMethods is mixed with `ALL` and other values', () => {
expect(() => new ResponseHeadersPolicy(stack, 'ResponseHeadersPolicy', {
corsBehavior: {
accessControlAllowCredentials: false,
accessControlAllowHeaders: ['*'],
accessControlAllowMethods: ['ALL', 'GET'],
accessControlAllowOrigins: ['*'],
originOverride: true,
},
})).toThrow("accessControlAllowMethods - 'ALL' cannot be combined with specific HTTP methods.");
});

test('throws if accessControlAllowMethods contains unallowed value', () => {
expect(() => new ResponseHeadersPolicy(stack, 'ResponseHeadersPolicy', {
corsBehavior: {
accessControlAllowCredentials: false,
accessControlAllowHeaders: ['*'],
accessControlAllowMethods: ['PROPFIND'],
accessControlAllowOrigins: ['*'],
originOverride: true,
},
})).toThrow(/accessControlAllowMethods contains unexpected method name/);
});
});
});

0 comments on commit 4c42800

Please sign in to comment.