Skip to content

Commit f2baa53

Browse files
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)Copilot
andcommitted
Add Pester test for insecure policy folder warning
Adds dsc_settings_policy_security.tests.ps1 that validates on both Windows and Linux that an insecure dsc policy folder emits a warning about the settings file not being used, and does not cause a non-zero exit code. Also fixes minor issue with the t! macro call passing path to the Windows-specific message. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 89985c3 commit f2baa53

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

lib/dsc-lib/src/util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ pub fn get_exe_path() -> Result<PathBuf, DscError> {
202202
fn get_settings_policy_file_path() -> String
203203
{
204204
// $env:ProgramData+"\dsc\dsc.settings.json"
205-
// This location is writable only by admins, but readable by all users
206205
let Ok(local_program_data_path) = std::env::var("ProgramData") else { return String::new(); };
207206
let dsc_folder = Path::new(&local_program_data_path).join("dsc");
208207
let settings_path = dsc_folder.join("dsc.settings.json").display().to_string();
@@ -212,7 +211,7 @@ fn get_settings_policy_file_path() -> String
212211
}
213212

214213
if !verify_windows_acl(&dsc_folder) {
215-
let required = t!("util.policyFolderNotSecureWindows");
214+
let required = t!("util.policyFolderNotSecureWindows", path = dsc_folder.display());
216215
warn!("{}", t!("util.policyFolderNotSecure", path = dsc_folder.display(), required = required));
217216
return String::new();
218217
}

0 commit comments

Comments
 (0)