From c4c67df3c3ab4536498438d0797448f73e26c711 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 28 Nov 2024 18:44:10 +0100 Subject: [PATCH] [platform] refining sub-path detection in the Defender configuration script (IJPL-164957) (cherry picked from commit c11857dc34299b734a93acfbf035c2dcd5b36944) IJ-CR-149682 GitOrigin-RevId: e0263de6336c8078cd897d9c5b5365e618953540 --- bin/win/defender-exclusions.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bin/win/defender-exclusions.ps1 b/bin/win/defender-exclusions.ps1 index 2c1dd6a641f8..91b7b2e8bf58 100644 --- a/bin/win/defender-exclusions.ps1 +++ b/bin/win/defender-exclusions.ps1 @@ -34,10 +34,18 @@ try { return $result } + # returns `$true` when the given path is a sub-path of the given parent + function Test-StartsWith ([string] $path, [string] $parent) { + $pathNorm = $path.Trim('\') + '\' + $parentNorm = $parent.Trim('\') + '\' + return $pathNorm.Equals($parentNorm, [StringComparison]::OrdinalIgnoreCase) -or ` + $pathNorm.StartsWith($parentNorm, [StringComparison]::OrdinalIgnoreCase) + } + # returns `$true` when a path is already covered by the exclusion list function Test-Excluded ([string] $path, [string[]] $exclusions) { foreach ($exclusion in $exclusions) { - if (((Get-Item -Path $exclusion) -is [System.IO.DirectoryInfo]) -and ([cultureinfo]::InvariantCulture.CompareInfo.IsPrefix($path, $exclusion, @("IgnoreCase"))) -and ($exclusion.TrimEnd('\').split('\').Count -gt $path.TrimEnd('\').Split('\').Count)) { + if (Test-StartsWith $path $exclusion) { return $true } }