Bylaw

Gallery › App Service › Azure built-in policies

Function apps should not have CORS configured to allow every resource to access your apps

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 2.1.0

Cross-Origin Resource Sharing (CORS) should not allow all domains to access your Function app. Allow only required domains to interact with your Function app.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONRestrictCORSAccess_FuntionApp_Audit.json · 65 lines
{
  "properties": {
    "displayName": "Function apps should not have CORS configured to allow every resource to access your apps",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Cross-Origin Resource Sharing (CORS) should not allow all domains to access your Function app. Allow only required domains to interact with your Function app.",
    "metadata": {
      "version": "2.1.0",
      "category": "App Service"
    },
    "version": "2.1.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Web/sites"
          },
          {
            "field": "kind",
            "contains": "functionapp"
          },
          {
            "field": "kind",
            "notContains": "workflowapp"
          },
          {
            "field": "kind",
            "notContains": "azurecontainerapps"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Web/sites/config",
          "existenceCondition": {
            "field": "Microsoft.Web/sites/config/web.cors.allowedOrigins[*]",
            "notEquals": "*"
          }
        }
      }
    },
    "versions": [
      "2.1.0",
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0820b7b9-23aa-4725-a1ce-ae4558f718e5",
  "name": "0820b7b9-23aa-4725-a1ce-ae4558f718e5"
}
Bylaw0820b7b9-23aa-4725-a1ce-ae4558f718e5.bylaw.ps1 · 40 lines
# Converted from RestrictCORSAccess_FuntionApp_Audit.json by ConvertTo-Bylaw.
Policy '0820b7b9-23aa-4725-a1ce-ae4558f718e5' {

    Properties @{
        DisplayName   = 'Function apps should not have CORS configured to allow every resource to access your apps'
        Description   = 'Cross-Origin Resource Sharing (CORS) should not allow all domains to access your Function app. Allow only required domains to interact with your Function app.'
        Version       = '2.1.0'
        Category      = 'App Service'
        PolicyVersion = '2.1.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Web/sites' {

        When {
            Field kind | Must -ContainText 'functionapp'
            Field kind | Must -Not -ContainText 'workflowapp'
        }

        Require 'kind contains azurecontainerapps' {
            Field kind | Must -ContainText 'azurecontainerapps'
        }
    }

    AuditIfNotExists 'Microsoft.Web/sites/config' -Effect (Param Effect) {
        Require 'web.cors.allowedOrigins is as required' {
            Each 'web.cors.allowedOrigins[*]' {
                Element | Must -Not -Be '*'
            }
        }
    }
}

Source: built-in-policies/policyDefinitions/App Service/RestrictCORSAccess_FuntionApp_Audit.json in Azure/azure-policy, under the MIT licence.