@@ -16,6 +16,10 @@ export async function runWindowsProfileScript(
1616 const system = process . env . SystemRoot ;
1717 if ( ! system || ! path . isAbsolute ( system ) ) throw new Error ( 'Windows system directory unavailable.' ) ;
1818 const ps = path . join ( system , 'System32' , 'WindowsPowerShell' , 'v1.0' , 'powershell.exe' ) ;
19+ const env = { ...process . env , ...extraEnv } ;
20+ // A parent pwsh or developer shell may advertise modules for a different .NET runtime.
21+ for ( const key of Object . keys ( env ) ) if ( key . toLowerCase ( ) === 'psmodulepath' ) delete env [ key ] ;
22+ env . PSModulePath = path . join ( path . dirname ( ps ) , 'Modules' ) ;
1923 return new Promise ( ( resolve , reject ) => {
2024 const child = execFile (
2125 ps ,
@@ -27,7 +31,7 @@ export async function runWindowsProfileScript(
2731 Buffer . from ( command , 'utf16le' ) . toString ( 'base64' ) ,
2832 ] ,
2933 {
30- env : { ... process . env , ... extraEnv } ,
34+ env,
3135 windowsHide : true ,
3236 timeout : 15000 ,
3337 maxBuffer : 128 * 1024 ,
@@ -75,22 +79,41 @@ export function classifyWindowsProfileAcl(value: { user: string; items: Descript
7579export async function orphanKeyPrivacy ( file : string , dir : string ) : Promise < string > {
7680 const script = `
7781 $ErrorActionPreference = 'Stop'
82+ $phase = 'identity'
7883 try {
7984 $sid = [System.Security.Principal.SecurityIdentifier]
85+ $user = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
86+ $sections = [System.Security.AccessControl.AccessControlSections]'Owner, Access'
8087 $list = @(foreach ($target in @($env:OAA_INIT_DIR, $env:OAA_INIT_KEY)) {
81- $acl = Get-Acl -LiteralPath $target
88+ if ($target -eq $env:OAA_INIT_DIR) {
89+ $phase = 'directory-descriptor'
90+ $acl = [System.IO.Directory]::GetAccessControl($target,$sections)
91+ } else {
92+ $phase = 'key-descriptor'
93+ $acl = [System.IO.File]::GetAccessControl($target,$sections)
94+ }
95+ $phase = 'access-rules'
8296 $rules = @(foreach ($ace in $acl.GetAccessRules($true, $true, $sid)) {
8397 @{ who=$ace.IdentityReference.Value; kind=[int]$ace.AccessControlType; flags=[int]$ace.PropagationFlags; rights=[long]$ace.FileSystemRights }
8498 })
8599 @{ owner=($acl.GetOwner($sid)).Value; rules=$rules }
86100 })
87- $result = @{ user=[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value; items=$list }
101+ $phase = 'serialize'
102+ $result = @{ user=$user; items=$list }
88103 [Console]::Out.Write((ConvertTo-Json -InputObject $result -Compress -Depth 6))
89- } catch { [Console]::Out.Write('acl-query-error'); exit 1 }` ;
104+ } catch { [Console]::Out.Write(('{"failure":"' + $phase + ':' + $_.Exception.GetBaseException().GetType().Name + '"}')) }` ;
90105 try {
91- return classifyWindowsProfileAcl (
92- JSON . parse ( await runWindowsProfileScript ( script , { OAA_INIT_DIR : dir , OAA_INIT_KEY : file } ) ) ,
106+ const value = JSON . parse (
107+ await runWindowsProfileScript ( script , { OAA_INIT_DIR : dir , OAA_INIT_KEY : file } ) ,
93108 ) ;
109+ if (
110+ typeof value ?. failure === 'string' &&
111+ / ^ ( i d e n t i t y | d i r e c t o r y - d e s c r i p t o r | k e y - d e s c r i p t o r | a c c e s s - r u l e s | s e r i a l i z e ) : [ A - Z a - z ] { 1 , 64 } E x c e p t i o n $ / . test (
112+ value . failure ,
113+ )
114+ )
115+ return value . failure ;
116+ return classifyWindowsProfileAcl ( value ) ;
94117 } catch ( e ) {
95118 const code = ( e as NodeJS . ErrnoException ) . code ;
96119 return typeof code === 'number' ? `acl-query-exit-${ code } ` : 'acl-query-unavailable' ;
0 commit comments