Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudfront): add validations on ResponseHeadersCorsBehavior.accessControlAllowMethods #32769

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/);
});
});
});
Loading