Untrusted AMI images Medium

The code requests Amazon Machine Images (AMIs) by name, without filtering them by owner or AMI identifiers. The response might contain untrusted public images from other accounts. Launching an AMI from an untrusted source might inadvertently run malicious code.

Detector ID
python/untrusted-ami-images@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1def image_filter_non_compliant():
2    import boto3
3    ec2 = boto3.resource('ec2')
4    image_name = 'The name of the AMI (provided during image creation)'
5    # Noncompliant: requests Amazon Machine Images (AMIs) with
6    # only name filter ignoring owner or AMI identifiers.
7    filters = [{'Name': 'name', 'Values': [image_name]}]
8    images = ec2.images.filter(Filters=filters)

Compliant example

1def image_filter_compliant():
2    import boto3
3    ec2 = boto3.resource('ec2')
4    image_name = 'The name of the AMI (provided during image creation)'
5    owner_id = 'The AWS account ID of the owner'
6    # Compliant: requests Amazon Machine Images (AMIs) with
7    # both name and owner-id filters.
8    filters = [
9        {'Name': 'name', 'Values': [image_name]},
10        {'Name': 'owner-id', 'Values': [owner_id]}
11    ]
12    images = ec2.images.filter(Filters=filters)