terraform.gcp.security.gcp-compute-firewall-unrestricted-ingress-3306.gcp-compute-firewall-unrestricted-ingress-3306

profile photo of semgrepsemgrep
Author
unknown
Download Count*

Ensure Google compute firewall ingress does not allow unrestricted MySQL access

Run Locally

Run in CI

Defintion

rules:
  - id: gcp-compute-firewall-unrestricted-ingress-3306
    patterns:
      - pattern: resource
      - pattern-either:
          - pattern-inside: |
              resource "google_compute_firewall" "..." {
              ...
              allow {
                protocol = "tcp"
                ports    = [3306]
              }
              source_ranges = ["0.0.0.0/0"]
              ...
              }
          - pattern-inside: |
              resource "google_compute_firewall" "..." {
              ...
              allow {
                protocol = "tcp"
                ports    = [..., "3306", ...]
              }
              source_ranges = ["0.0.0.0/0"]
              ...
              }
    message: Ensure Google compute firewall ingress does not allow unrestricted
      MySQL access
    metadata:
      owasp:
        - A05:2017 - Broken Access Control
        - A01:2021 - Broken Access Control
      cwe:
        - "CWE-284: Improper Access Control"
      category: security
      technology:
        - terraform
        - gcp
      references:
        - https://docs.bridgecrew.io/docs/google-cloud-policy-index
      subcategory:
        - audit
      likelihood: LOW
      impact: LOW
      confidence: LOW
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]
      vulnerability_class:
        - Improper Authorization
    languages:
      - hcl
    severity: WARNING

Examples

gcp-compute-firewall-unrestricted-ingress-3306.tf

# ok: gcp-compute-firewall-unrestricted-ingress-3306
resource "google_compute_firewall" "restricted" {
  name    = "example"
  network = "google_compute_network.vpc.name"

  allow {
    protocol = "tcp"
    ports    = ["3306"]
  }

  source_ranges = ["172.1.2.3/32"]
}

# ok: gcp-compute-firewall-unrestricted-ingress-3306
resource "google_compute_firewall" "allow_different_int" {
  name    = "example"
  network = "google_compute_network.vpc.name"

  allow {
    protocol = "tcp"
    ports    = [4624]
  }

  source_ranges = ["172.1.2.3/32"]
}

# ok: gcp-compute-firewall-unrestricted-ingress-3306
resource "google_compute_firewall" "allow_null" {
  name    = "example"
  network = "google_compute_network.vpc.name"

  allow {
    protocol = "tcp"
    ports    = null
  }

  source_ranges = ["172.1.2.3/32"]
  target_tags   = ["mysql"]
}

# fail
# ruleid: gcp-compute-firewall-unrestricted-ingress-3306
resource "google_compute_firewall" "allow_mysql_int" {
  name    = "example"
  network = "google_compute_network.vpc.name"

  allow {
    protocol = "tcp"
    ports    = [3306]
  }

  source_ranges = ["0.0.0.0/0"]
}

# fail
# ruleid: gcp-compute-firewall-unrestricted-ingress-3306
resource "google_compute_firewall" "allow_multiple" {
  name    = "example"
  network = "google_compute_network.vpc.name"

  allow {
    protocol = "tcp"
    ports    = ["4000-65535", "3306"]
  }

  source_ranges = ["0.0.0.0/0"]
}