aws / aws/aws-cdk

aws-cdk/aws-iam: should check for invalid array in PolicyStatement conditions

Open
#25,826 1 comment 2 reactions 0 assignees View on GitHub
@aws-cdk/aws-iam effort/small feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

For some reason, typescript allows an array to be set on `PolicyStatement.conditions`, which is supposed to be an object of type `{[key: string]: any}`. This raises no compile errors either in Typescript or CDK and generates an invalid PolicyDocument in the CFN template.

### Expected Behavior

The broken code should not synth.

### Current Behavior

When accidentally passing an array to `PolicyStatement.conditions`, there are no type errors raised (so IDE shows no problem in syntax highlighting) and some invalid CFN is synthed.

### Reproduction Steps

I had this code:

```typescript
this.bucket.addToResourcePolicy(new PolicyStatement({
effect: Effect.ALLOW,
principals: [
new ServicePrincipal('s3.amazonaws.com'),
],
actions: [
's3:PutObject',
],
resources: [
this.bucket.arnForObjects('*'),
],
conditions: [ // this array should not be here
{
StringEquals: {
'aws:SourceAccount': '123456789',
's3:x-amz-acl': 'bucket-owner-full-control',
},
},
],
}));
});
```

Which synthed fine and resulted in a CFN template that looks like this:

```json
{
"PolicyDocument": {
"Statement": [
{
"Action": "s3:PutObject",
"Condition": {
"0": {
"StringEquals": {
"aws:SourceAccount": "123456789",
"s3:x-amz-acl": "bucket-owner-full-control",
}
}
},
}
]
}
}
```

When applying this template, CFN reports this error:

```
Invalid Condition type : 0 (Service: Amazon S3; Status Code: 400; Error Code: MalformedPolicy;)
```

### Possible Solution

I'm really not sure why Typescript doesn't report this as a proper type error, but it might be worth adding something defensive to check that the PolicyStatement condition is actually an object and not an array, and failing the build if the developer puts the wrong thing in (like I did).

### Additional Information/Context

_No response_

### CDK CLI Version

2.79.1

### Framework Version

_No response_

### Node.js Version

19.1.0

### OS

Linux

### Language

Typescript

### Language Version

4.9.5

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the TypeScript PolicyStatement entry point and trace how conditions are synthesized into the CloudFormation PolicyDocument. Add a regression test using an array for conditions, verify it is rejected before synthesis, and run the relevant IAM tests to confirm valid condition objects still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
authorization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.