Security Group Unrestricted Ingress
security-group-unrestricted-ingress
What this rule checks
Detects security groups with unrestricted ingress (0.0.0.0/0).
How to fix it
- 1Restrict ingress to specific IP ranges
- 2Use security group references for internal traffic
- 3Implement least privilege network access
import * as ec2 from 'aws-cdk-lib/aws-ec2';
const vpc = new ec2.Vpc(this, 'Vpc', {
maxAzs: 2,
natGateways: 0,
subnetConfiguration: [
{ name: 'isolated', subnetType: ec2.SubnetType.PRIVATE_ISOLATED, cidrMask: 24 },
],
});
const sg = new ec2.SecurityGroup(this, 'Sg', { vpc });
// SSH open to the whole internet.
sg.addIngressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(22), 'ssh');import * as ec2 from 'aws-cdk-lib/aws-ec2';
const vpc = new ec2.Vpc(this, 'Vpc', {
maxAzs: 2,
natGateways: 0,
subnetConfiguration: [
{ name: 'isolated', subnetType: ec2.SubnetType.PRIVATE_ISOLATED, cidrMask: 24 },
],
});
// allowAllOutbound:false keeps egress off 0.0.0.0/0; a single narrow
// ingress rule (private CIDR, HTTPS) is the only opening.
const sg = new ec2.SecurityGroup(this, 'Sg', {
vpc,
allowAllOutbound: false,
});
sg.addIngressRule(ec2.Peer.ipv4('10.0.0.0/16'), ec2.Port.tcp(443), 'internal https');CDK Insights pinpoints the exact file and line in your CDK source for every finding, so you can jump straight to the fix.
Affected resource types
AWS::EC2::SecurityGroupIntentional? Suppress this finding
Sometimes a flag is deliberate โ a genuinely public endpoint, say. You can dismiss security-group-unrestricted-ingress and the reason is kept in the report, not silently hidden.
In .cdk-insights.json:
{
"ignoreRules": [
{ "id": "security-group-unrestricted-ingress", "reason": "Why this is intentional" }
]
}Or inline in your CDK code:
Validations.of(scope).acknowledge({
id: 'cdk-insights::security-group-unrestricted-ingress',
reason: 'Why this is intentional',
});Use the rule ID security-group-unrestricted-ingress shown above โ not the CDK-* ID from SARIF / GitHub code scanning. To dismiss every finding on one construct instead, use ignorePaths. Suppression docs โ
Catch this in your stack
$ npx cdk-insights scanCDK Insights runs this and 118+ other rules locally against your synthesised CDK app โ free, no account, your code never leaves your machine.