core: CfnResource.isCfnResource is too permissive
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
I created a construct class with a cfnResourceType property. I use that type to search for a CfnResource under the parent of that construct. But my non-CfnResource class is recognized as a CfnResource by the CDK because it has a cfnResourceType property! Wrong!
### Expected Behavior
```
public static isCfnResource(x: any): x is CfnResource {
return CfnElement.isCfnElement(x) && x.cfnResourceType !== undefined;
}
```
That would be a lot better because isCfnElement has an RTTI symbol property.
### Current Behavior
```
public static isCfnResource(x: any): x is CfnResource {
return x !== null && typeof(x) === 'object' && x.cfnResourceType !== undefined;
}
```
### Reproduction Steps
```
class BucketHelperClass extends Construct {
readonly cfnResourceType = CfnBucket.CFN_RESOURCE_TYPE_NAME
}
let helper = new BucketHelperClass(new App(), "Helper")
if (CfnResource.isCfnResource(helper)) {
throw new Error('No it is not a CfnResource!');
}
```
### Possible Solution
Either this:
```
public static isCfnResource(x: any): x is CfnResource {
return CfnElement.isCfnElement(x) && x.cfnResourceType !== undefined;
}
```
Or maybe:
```
const CFN_RESOURCE_SYMBOL = Symbol.for('@aws-cdk/core.CfnResource');
```
### Additional Information/Context
When I try to synthesize a stack with a fake CfnResource, I get the following error:
```
[CDK] TypeError: source.addDependency is not a function
```
### CDK CLI Version
2.120.0 (build 58b90c4)
### Framework Version
_No response_
### Node.js Version
10.2.5
### OS
MacOS
### Language
TypeScript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Locate the CfnResource.isCfnResource implementation and inspect how CfnElement.isCfnElement identifies constructs. Reproduce the issue with the BucketHelperClass example, then verify that a construct exposing cfnResourceType is not treated as a CfnResource while genuine CfnResources still are.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100