|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe 'tests for policy folder security validation' { |
| 5 | + BeforeDiscovery { |
| 6 | + $isElevated = if ($IsWindows) { |
| 7 | + ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( |
| 8 | + [Security.Principal.WindowsBuiltInRole]::Administrator) |
| 9 | + } else { |
| 10 | + (id -u) -eq 0 |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + Context 'Windows policy folder with insecure ACL emits warning' -Skip:(!$IsWindows -or !$isElevated) { |
| 15 | + BeforeAll { |
| 16 | + $script:policyDirPath = Join-Path $env:ProgramData "dsc" |
| 17 | + $script:policyFilePath = Join-Path $script:policyDirPath "dsc.settings.json" |
| 18 | + $script:existedBefore = Test-Path $script:policyDirPath |
| 19 | + |
| 20 | + if (-not $script:existedBefore) { |
| 21 | + New-Item -ItemType Directory -Path $script:policyDirPath -Force | Out-Null |
| 22 | + } |
| 23 | + |
| 24 | + # Write a simple policy settings file |
| 25 | + @{ tracing = @{ level = "TRACE" } } | ConvertTo-Json -Depth 5 | Set-Content -Path $script:policyFilePath |
| 26 | + |
| 27 | + # Grant Everyone write access to make the folder insecure |
| 28 | + icacls $script:policyDirPath /grant "Everyone:(OI)(CI)(W)" /T | Out-Null |
| 29 | + } |
| 30 | + |
| 31 | + AfterAll { |
| 32 | + # Remove the Everyone ACE to restore security |
| 33 | + icacls $script:policyDirPath /remove "Everyone" /T | Out-Null |
| 34 | + |
| 35 | + Remove-Item -Path $script:policyFilePath -ErrorAction SilentlyContinue |
| 36 | + if (-not $script:existedBefore) { |
| 37 | + Remove-Item -Recurse -Force -Path $script:policyDirPath -ErrorAction SilentlyContinue |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + It 'Should emit a warning about insecure policy folder' { |
| 42 | + dsc -l warn resource list 2> $TestDrive/tracing.txt |
| 43 | + "$TestDrive/tracing.txt" | Should -FileContentMatch "is not secure, settings file will not be used" |
| 44 | + } |
| 45 | + |
| 46 | + It 'Should not exit with an error code' { |
| 47 | + dsc -l warn resource list 2> $TestDrive/tracing.txt |
| 48 | + $LASTEXITCODE | Should -Be 0 |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + Context 'Linux policy folder with insecure permissions emits warning' -Skip:($IsWindows -or !$isElevated) { |
| 53 | + BeforeAll { |
| 54 | + $script:policyDirPath = "/etc/dsc" |
| 55 | + $script:policyFilePath = Join-Path $script:policyDirPath "dsc.settings.json" |
| 56 | + $script:existedBefore = Test-Path $script:policyDirPath |
| 57 | + |
| 58 | + if (-not $script:existedBefore) { |
| 59 | + New-Item -ItemType Directory -Path $script:policyDirPath -Force | Out-Null |
| 60 | + } |
| 61 | + |
| 62 | + # Write a simple policy settings file |
| 63 | + @{ tracing = @{ level = "TRACE" } } | ConvertTo-Json -Depth 5 | Set-Content -Path $script:policyFilePath |
| 64 | + |
| 65 | + # Make the folder world-writable (insecure) |
| 66 | + chmod 777 $script:policyDirPath |
| 67 | + } |
| 68 | + |
| 69 | + AfterAll { |
| 70 | + chmod 755 $script:policyDirPath |
| 71 | + |
| 72 | + Remove-Item -Path $script:policyFilePath -ErrorAction SilentlyContinue |
| 73 | + if (-not $script:existedBefore) { |
| 74 | + Remove-Item -Recurse -Force -Path $script:policyDirPath -ErrorAction SilentlyContinue |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + It 'Should emit a warning about insecure policy folder' { |
| 79 | + dsc -l warn resource list 2> $TestDrive/tracing.txt |
| 80 | + "$TestDrive/tracing.txt" | Should -FileContentMatch "is not secure, settings file will not be used" |
| 81 | + } |
| 82 | + |
| 83 | + It 'Should not exit with an error code' { |
| 84 | + dsc -l warn resource list 2> $TestDrive/tracing.txt |
| 85 | + $LASTEXITCODE | Should -Be 0 |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments