Skip to content

Commit 4c78da1

Browse files
committed
Validate app IDs in CA policy applications
Added logic to filter included and excluded application IDs in CA policy to ensure only app IDs with a corresponding service principal in the tenant are retained. Also replaced usage of $User with $Headers in log messages for consistency.
1 parent a60bd84 commit 4c78da1

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function New-CIPPCAPolicy {
1313
$Headers
1414
)
1515

16-
$User = $Request.Headers
17-
1816
function Remove-EmptyArrays ($Object) {
1917
if ($Object -is [Array]) {
2018
foreach ($Item in $Object) { Remove-EmptyArrays $Item }
@@ -45,14 +43,14 @@ function New-CIPPCAPolicy {
4543
$GroupIds = [System.Collections.Generic.List[string]]::new()
4644
$groupNames | ForEach-Object {
4745
if (Test-IsGuid $_) {
48-
Write-LogMessage -Headers $User -API 'Create CA Policy' -message "Already GUID, no need to replace: $_" -Sev 'Debug'
46+
Write-LogMessage -Headers $Headers -API 'Create CA Policy' -message "Already GUID, no need to replace: $_" -Sev 'Debug'
4947
$GroupIds.Add($_) # it's a GUID, so we keep it
5048
} else {
5149
$groupId = ($groups | Where-Object -Property displayName -EQ $_).id # it's a display name, so we get the group ID
5250
if ($groupId) {
5351
foreach ($gid in $groupId) {
5452
Write-Warning "Replaced group name $_ with ID $gid"
55-
$null = Write-LogMessage -Headers $User -API 'Create CA Policy' -message "Replaced group name $_ with ID $gid" -Sev 'Debug'
53+
$null = Write-LogMessage -Headers $Headers -API 'Create CA Policy' -message "Replaced group name $_ with ID $gid" -Sev 'Debug'
5654
$GroupIds.Add($gid) # add the ID to the list
5755
}
5856
} elseif ($CreateGroups) {
@@ -141,6 +139,31 @@ function New-CIPPCAPolicy {
141139
}
142140
}
143141

142+
#if we have excluded or included applications, we need to remove any appIds that do not have a service principal in the tenant
143+
144+
if (($JSONobj.conditions.applications.includeApplications -and $JSONobj.conditions.applications.includeApplications -notcontains 'All') -or ($JSONobj.conditions.applications.excludeApplications -and $JSONobj.conditions.applications.excludeApplications -notcontains 'All')) {
145+
$AllServicePrincipals = New-GraphGETRequest -uri 'https://graph.microsoft.com/v1.0/servicePrincipals?$select=appId' -tenantid $TenantFilter -asApp $true
146+
147+
if ($JSONobj.conditions.applications.excludeApplications -and $JSONobj.conditions.applications.excludeApplications -notcontains 'All') {
148+
$ValidExclusions = [system.collections.generic.list[string]]::new()
149+
foreach ($appId in $JSONobj.conditions.applications.excludeApplications) {
150+
if ($AllServicePrincipals.appId -contains $appId) {
151+
$ValidExclusions.Add($appId)
152+
}
153+
}
154+
$JSONobj.conditions.applications.excludeApplications = $ValidExclusions
155+
}
156+
if ($JSONobj.conditions.applications.includeApplications -and $JSONobj.conditions.applications.includeApplications -notcontains 'All') {
157+
$ValidInclusions = [system.collections.generic.list[string]]::new()
158+
foreach ($appId in $JSONobj.conditions.applications.includeApplications) {
159+
if ($AllServicePrincipals.appId -contains $appId) {
160+
$ValidInclusions.Add($appId)
161+
}
162+
}
163+
$JSONobj.conditions.applications.includeApplications = $ValidInclusions
164+
}
165+
}
166+
144167
#for each of the locations, check if they exist, if not create them. These are in $JSONobj.LocationInfo
145168
$LocationLookupTable = foreach ($locations in $JSONobj.LocationInfo) {
146169
if (!$locations) { continue }

0 commit comments

Comments
 (0)