WebAdministrationDsc icon indicating copy to clipboard operation
WebAdministrationDsc copied to clipboard

Can you set multiple file extensions to allowed for request filtering?

Open obsolete3032 opened this issue 4 years ago • 1 comments

I am trying to add multiple file extensions to the allowed list for request filtering using the xWebConfigPropertyCollection resource but am stuck on exactly how to do it. I can add an individual extension, but can't figure out how to indicate multiples. Below adds one extension with the whole string. If I remove the ", .aspz" then it successfully adds the .aspx extension.

WebsitePath = 'MACHINE/WEBROOT/APPHOST' Filter = 'system.webServer/security/requestFiltering' CollectionName = 'fileExtensions' ItemName = 'add' ItemKeyName = 'fileExtension' ItemKeyValue = '.aspx, .aspz' ItemPropertyName = 'allowed' ItemPropertyValue = 'true' Ensure = 'Present'

obsolete3032 avatar May 25 '21 16:05 obsolete3032

Using the above configuration would result in this configuration <add fileExtension=".aspx, .aspz" />. This will mean that a file should be named like index.aspx, .aspz, which is not what you want to achieve. Instead you could loop over the extensions that you would like to allow and add all of them. That could look something like

Configuration Sample_xWebConfigPropertyCollection_Add
{
    param
    (
        # Target nodes to apply the configuration.
        [Parameter()]
        [String[]]
        $NodeName = 'localhost',

        # File extensions to allow in request filtering configuration
        [Parameter()]
        [String[]]
        $AllowedFileExtensions = @('.aspx', '.aspz')
    )

    # Import the modules that define custom resources
    Import-DscResource -ModuleName xWebAdministration

    Node $NodeName
    {
        foreach($extension in $AllowedFileExtensions)
        {
            xWebConfigPropertyCollection "$($NodeName) - Request Filtering allow fileextension $($extension)"
            {
                WebsitePath       = 'MACHINE/WEBROOT/APPHOST'
                Filter            = 'system.webServer/security/requestFiltering'
                CollectionName    = 'fileExtensions'
                ItemName          = 'add'
                ItemKeyName       = 'fileExtension'
                ItemKeyValue      = $extension
                ItemPropertyName  = 'allowed'
                ItemPropertyValue = 'true'
                Ensure            = 'Present'
            }
        }
    }
}

remcoeissing avatar Nov 05 '21 17:11 remcoeissing