id: gcloud-sql-cross-db-ownership-chaining-enabled

info:
  name: Cross DB Ownership Chaining Enabled in SQL Server Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "cross db ownership chaining" database flag is disabled for your Google Cloud SQL Server database instances. This flag, when enabled, can potentially introduce security risks by allowing cross-database access without explicit permissions.
  impact: |
    Enabling the "cross db ownership chaining" flag can lead to potential security vulnerabilities by allowing unauthorized access across databases.
  remediation: |
    Disable the "cross db ownership chaining" flag in your SQL Server database instance configuration to prevent unauthorized cross-database access.
  reference:
    - https://cloud.google.com/sql/docs/sqlserver/flags
  tags: cloud,devops,gcp,gcloud,google-cloud-sql,sqlserver,gcp-cloud-config

flow: |
  code(1)
  for(let projectId of iterate(template.projectIds)){
    set("projectId", projectId)
    code(2)
    for(let sqlInstance of iterate(template.sqlInstances)){
      set("sqlInstance", sqlInstance)
      code(3)
    }
  }

self-contained: true

code:
  - engine:
      - sh
      - bash
    source: |
      gcloud projects list --format="json(projectId)"

    extractors:
      - type: json
        name: projectIds
        internal: true
        json:
          - '.[].projectId'

  - engine:
      - sh
      - bash
    source: |
      gcloud sql instances list --project $projectId --filter=DATABASE_VERSION:SQLSERVER* --format="json(name)"

    extractors:
      - type: json
        name: sqlInstances
        internal: true
        json:
          - '.[].name'

  - engine:
      - sh
      - bash
    source: |
      gcloud sql instances describe $sqlInstance --project $projectId --format=json | jq -r '.settings.databaseFlags[]? | select(.name=="cross db ownership chaining") | .value // "null"'

    matchers:
      - type: word
        words:
          - 'on'

    extractors:
      - type: dsl
        dsl:
          - '"The cross db ownership chaining flag is enabled for SQL Server instance " + sqlInstance + " in project " + projectId'
# digest: 490a00463044022016461f9616320de94701ddd7c7f394e0a40de0e877e1f3da6ef060cf41ddeb9b02206e1d50c4a65743dc1af124f7d3c97a403007cb0ab5dd8c8478ee8f6ab4b782a7:922c64590222798bb761d5b6d8e72950