|
| 1 | +function Test-CIPPConditionFilter { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Returns a sanitized PowerShell condition string for an audit log / delta query condition. |
| 5 | + .DESCRIPTION |
| 6 | + Validates operator and property name against allowlists, sanitizes input values, |
| 7 | + then returns a safe condition string suitable for [ScriptBlock]::Create(). |
| 8 | +
|
| 9 | + This replaces the old Invoke-Expression pattern which was vulnerable to code injection |
| 10 | + through unsanitized user-controlled fields. |
| 11 | + .PARAMETER Condition |
| 12 | + A single condition object with Property.label, Operator.value, and Input.value. |
| 13 | + .OUTPUTS |
| 14 | + [string] A sanitized PowerShell condition string, or $null if validation fails. |
| 15 | + .FUNCTIONALITY |
| 16 | + Internal |
| 17 | + #> |
| 18 | + [CmdletBinding()] |
| 19 | + [OutputType([string])] |
| 20 | + param( |
| 21 | + [Parameter(Mandatory = $true)] |
| 22 | + $Condition |
| 23 | + ) |
| 24 | + |
| 25 | + # Operator allowlist - only these PowerShell comparison operators are permitted |
| 26 | + $AllowedOperators = @( |
| 27 | + 'eq', 'ne', 'like', 'notlike', 'match', 'notmatch', |
| 28 | + 'gt', 'lt', 'ge', 'le', 'in', 'notin', |
| 29 | + 'contains', 'notcontains' |
| 30 | + ) |
| 31 | + |
| 32 | + # Property name validation - only alphanumeric, underscores, and dots allowed |
| 33 | + $SafePropertyRegex = [regex]'^[a-zA-Z0-9_.]+$' |
| 34 | + |
| 35 | + # Value sanitization - block characters that enable code injection |
| 36 | + $UnsafeValueRegex = [regex]'[;|`\$\{\}]' |
| 37 | + |
| 38 | + $propertyName = $Condition.Property.label |
| 39 | + $operatorValue = $Condition.Operator.value.ToLower() |
| 40 | + $inputValue = $Condition.Input.value |
| 41 | + |
| 42 | + # Validate operator against allowlist |
| 43 | + if ($operatorValue -notin $AllowedOperators) { |
| 44 | + Write-Warning "Blocked invalid operator '$($Condition.Operator.value)' in condition for property '$propertyName'" |
| 45 | + return $null |
| 46 | + } |
| 47 | + |
| 48 | + # Validate property name to prevent injection via property paths |
| 49 | + if (-not $SafePropertyRegex.IsMatch($propertyName)) { |
| 50 | + Write-Warning "Blocked invalid property name '$propertyName' in condition" |
| 51 | + return $null |
| 52 | + } |
| 53 | + |
| 54 | + # Build sanitized condition string |
| 55 | + if ($inputValue -is [array]) { |
| 56 | + # Sanitize each array element |
| 57 | + $sanitizedItems = foreach ($item in $inputValue) { |
| 58 | + $itemStr = [string]$item |
| 59 | + if ($UnsafeValueRegex.IsMatch($itemStr)) { |
| 60 | + Write-Warning "Blocked unsafe value in array for property '$propertyName': '$itemStr'" |
| 61 | + return $null |
| 62 | + } |
| 63 | + $itemStr -replace "'", "''" |
| 64 | + } |
| 65 | + if ($null -eq $sanitizedItems) { return $null } |
| 66 | + $arrayAsString = $sanitizedItems | ForEach-Object { "'$_'" } |
| 67 | + $value = "@($($arrayAsString -join ', '))" |
| 68 | + } else { |
| 69 | + $valueStr = [string]$inputValue |
| 70 | + if ($UnsafeValueRegex.IsMatch($valueStr)) { |
| 71 | + Write-Warning "Blocked unsafe value for property '$propertyName': '$valueStr'" |
| 72 | + return $null |
| 73 | + } |
| 74 | + $value = "'$($valueStr -replace "'", "''")'" |
| 75 | + } |
| 76 | + |
| 77 | + return "`$(`$_.$propertyName) -$operatorValue $value" |
| 78 | +} |
0 commit comments