'use strict'; const test=require('node:test'),assert=require('node:assert/strict'),cp=require('node:child_process'); const fs=require('node:fs'),os=require('node:os'),path=require('node:path'); test('native ACL policy accepts only the current owner and one protected inheritable full-control allow rule',{skip:process.platform!=='win32'},()=>{ const {privateDirectoryAcl}=require('../../src/platform/security/windows-directory.cjs'); const cases=` $sid=[System.Security.Principal.WindowsIdentity]::GetCurrent().User function Make-Acl($protected,$owner,$rights,$inherit,$propagation,$type,$identity) { $acl=New-Object System.Security.AccessControl.DirectorySecurity $acl.SetOwner($owner);$acl.SetAccessRuleProtection($protected,$false) $rule=New-Object System.Security.AccessControl.FileSystemAccessRule($identity,$rights,$inherit,$propagation,$type) $acl.AddAccessRule($rule);return $acl } $other=New-Object System.Security.Principal.SecurityIdentifier('S-1-1-0') $rows=@( (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $false $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $other 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'Read' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'FullControl' 'None' 'None' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'InheritOnly' 'Allow' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Deny' $sid) $sid), (Test-PrivateDirectoryAcl (Make-Acl $true $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $other) $sid) ) $extra=Make-Acl $true $sid 'FullControl' 'ContainerInherit,ObjectInherit' 'None' 'Allow' $sid $extra.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule($other,'Read','ContainerInherit,ObjectInherit','None','Allow'))) $rows+=(Test-PrivateDirectoryAcl $extra $sid) $hidden=New-Object System.Security.AccessControl.DirectorySecurity $hidden.SetSecurityDescriptorSddlForm('O:'+$sid.Value+'D:P(A;OICI;FA;;;'+$sid.Value+')(OA;OICI;FA;00000000-0000-0000-0000-000000000001;;WD)') $rows+=(Test-PrivateDirectoryAcl $hidden $sid) ConvertTo-Json -Compress -InputObject $rows `; const output=cp.execFileSync('powershell.exe',['-NoLogo','-NoProfile','-NonInteractive','-Command','$ErrorActionPreference="Stop";'+privateDirectoryAcl+cases],{encoding:'utf8',windowsHide:true,timeout:15000}); assert.deepEqual(JSON.parse(output),[true,false,false,false,false,false,false,false,false,false]); }); test('a compliant real directory is not rewritten and an unprotected directory is repaired',{skip:process.platform!=='win32'},t=>{ const {privateDirectoryAcl,secureDirectoryScript}=require('../../src/platform/security/windows-directory.cjs'),parent=path.resolve(os.tmpdir()),dir=fs.mkdtempSync(path.join(parent,'acl-policy-')); assert.equal(path.dirname(dir),parent);t.after(()=>fs.rmSync(dir,{recursive:true,force:true})); const run=script=>cp.execFileSync('powershell.exe',['-NoLogo','-NoProfile','-NonInteractive','-Command','$ErrorActionPreference="Stop";'+script],{input:dir,encoding:'utf8',windowsHide:true,timeout:15000}).trim(); run(secureDirectoryScript);const protectedAt=fs.statSync(dir,{bigint:true}).ctimeNs;run(secureDirectoryScript);assert.equal(fs.statSync(dir,{bigint:true}).ctimeNs,protectedAt,'read verification must not rewrite the DACL'); run('$p=[Console]::In.ReadToEnd();$acl=[IO.Directory]::GetAccessControl($p);$acl.SetAccessRuleProtection($false,$true);[IO.Directory]::SetAccessControl($p,$acl)'); run(secureDirectoryScript); const verified=run(privateDirectoryAcl+';$p=[Console]::In.ReadToEnd();Test-PrivateDirectoryAcl ([IO.Directory]::GetAccessControl($p)) ([System.Security.Principal.WindowsIdentity]::GetCurrent().User)');assert.equal(verified,'True'); run('$p=[Console]::In.ReadToEnd();$sid=[System.Security.Principal.WindowsIdentity]::GetCurrent().User;$acl=New-Object System.Security.AccessControl.DirectorySecurity;$acl.SetSecurityDescriptorSddlForm("O:"+$sid.Value+"D:P(A;OICI;FA;;;"+$sid.Value+")(OA;OICI;FA;00000000-0000-0000-0000-000000000001;;WD)",[System.Security.AccessControl.AccessControlSections]"Access,Owner");[IO.Directory]::SetAccessControl($p,$acl)'); assert.equal(run(privateDirectoryAcl+';$p=[Console]::In.ReadToEnd();Test-PrivateDirectoryAcl ([IO.Directory]::GetAccessControl($p)) ([System.Security.Principal.WindowsIdentity]::GetCurrent().User)'),'False'); run(secureDirectoryScript);assert.equal(run(privateDirectoryAcl+';$p=[Console]::In.ReadToEnd();Test-PrivateDirectoryAcl ([IO.Directory]::GetAccessControl($p)) ([System.Security.Principal.WindowsIdentity]::GetCurrent().User)'),'True'); });