chore(iam): add resource validation to PolicyStatement for resource-based policies#35300
Open
chore(iam): add resource validation to PolicyStatement for resource-based policies#35300
Conversation
…ed policies - Add resource validation to PolicyStatement.validateForResourcePolicy() method - Create new validateForTrustPolicy() method for IAM Role trust policies - Update Role validation to use trust policy validation instead of resource policy validation - Add comprehensive unit tests covering all validation scenarios - Add S3 bucket policy integration test demonstrating end-to-end validation - Automatically benefits 8+ AWS services (S3, SNS, KMS, SQS, ECR, Secrets Manager, Kinesis) Closes aws#35296 This change provides synthesis-time validation for missing resources in resource-based policies, replacing deployment-time CloudFormation errors with immediate, actionable CDK synthesis errors. No breaking changes - only catches currently invalid code that fails at deployment.
duranbe
approved these changes
Sep 27, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue # (if applicable)
Closes #35296.
Reason for this change
CDK currently allows creating resource-based policies (such as S3 bucket policies, SNS topic policies, KMS key policies) with PolicyStatements that lack the required
resourcesproperty. This causes deployment-time CloudFormation failures instead of providing immediate synthesis-time validation errors.This creates a poor developer experience where:
The root cause is that
PolicyStatement.validateForResourcePolicy()validates principals but not resources, despite both being required for resource-based policies according to AWS IAM documentation.Description of changes
This change enhances the existing validation infrastructure to catch missing resources at synthesis time while handling the nuanced differences between different types of resource-based policies:
Enhanced PolicyStatement.validateForResourcePolicy():
ResourcePolicyValidationOptionsparameter with JSII-compatible interface for services with implicit resource contextsNew ResourcePolicyValidationOptions interface:
skipResourceValidation?: booleanproperty to handle services like ECR where resources are implicitNew PolicyStatement.validateForTrustPolicy():
Updated PolicyDocument.validateForTrustPolicy():
Updated Role validation:
ECR Repository policy handling:
{ skipResourceValidation: true }since ECR repository policies have implicit resources (the repository itself)Cross-service impact:
This change automatically benefits 8+ AWS services that use resource-based policies:
skipResourceValidation: trueAPI Changes:
New Interface:
Updated Method Signatures:
Describe any new or updated permissions being added
N/A - This change only adds synthesis-time validation logic and does not modify IAM permissions, resource access patterns, or CloudFormation template generation.
Description of how you validated changes
Unit tests:
Added 7 comprehensive unit tests in
packages/aws-cdk-lib/aws-iam/test/policy-document.test.ts:Integration tests:
Added integration test in
packages/aws-cdk-lib/aws-s3/test/bucket-policy.test.ts:Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license