@@ -8,44 +8,94 @@ import { execFile } from 'node:child_process';
88import os from 'node:os' ;
99import path from 'node:path' ;
1010
11- // Only used when adopting an existing credential. Mode bits on Windows cannot establish ownership.
12- export async function ownerControlsOrphanKey ( file : string , dir : string ) : Promise < boolean > {
11+ /** Pass fixed application code intact to Windows PowerShell; paths remain ordinary environment values. */
12+ export async function runWindowsProfileScript (
13+ command : string ,
14+ extraEnv : NodeJS . ProcessEnv ,
15+ ) : Promise < string > {
1316 const system = process . env . SystemRoot ;
14- if ( ! system || ! path . isAbsolute ( system ) ) return false ;
17+ if ( ! system || ! path . isAbsolute ( system ) ) throw new Error ( 'Windows system directory unavailable.' ) ;
1518 const ps = path . join ( system , 'System32' , 'WindowsPowerShell' , 'v1.0' , 'powershell.exe' ) ;
16- const command = `
17- $ErrorActionPreference = 'Stop'
18- $sid = [Security.Principal.SecurityIdentifier]
19- $self = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value
20- $trusted = @($self, 'S-1-5-18', 'S-1-5-32-544')
21- foreach ($target in @($env:OAA_INIT_DIR, $env:OAA_INIT_KEY)) {
22- $acl = Get-Acl -LiteralPath $target
23- if ($trusted -notcontains $acl.GetOwner($sid).Value) { exit 1 }
24- $rules = $acl.GetAccessRules($true, $true, $sid)
25- if (!$rules.Count) { exit 1 }
26- foreach ($ace in $rules) {
27- if ($ace.AccessControlType -ne 'Allow' -or ($ace.PropagationFlags -band 2) -or
28- $trusted -contains $ace.IdentityReference.Value) { continue }
29- # Generic write/all, plus create, remove, change attributes, ACL or owner.
30- if ($target -eq $env:OAA_INIT_KEY -or ([int64]$ace.FileSystemRights -band 0x500D0156)) { exit 1 }
31- }
32- }
33- exit 0` ;
34- return new Promise ( ( resolve ) => {
35- execFile (
19+ return new Promise ( ( resolve , reject ) => {
20+ const child = execFile (
3621 ps ,
37- [ '-NoLogo' , '-NoProfile' , '-NonInteractive' , '-Command' , command ] ,
22+ [
23+ '-NoLogo' ,
24+ '-NoProfile' ,
25+ '-NonInteractive' ,
26+ '-EncodedCommand' ,
27+ Buffer . from ( command , 'utf16le' ) . toString ( 'base64' ) ,
28+ ] ,
3829 {
39- env : { ...process . env , OAA_INIT_DIR : dir , OAA_INIT_KEY : file } ,
30+ env : { ...process . env , ... extraEnv } ,
4031 windowsHide : true ,
41- timeout : 5000 ,
42- maxBuffer : 4096 ,
32+ timeout : 15000 ,
33+ maxBuffer : 128 * 1024 ,
4334 } ,
44- ( error ) => resolve ( ! error ) ,
35+ ( error , stdout ) =>
36+ error
37+ ? reject (
38+ Object . assign ( new Error ( 'Windows profile operation failed.' ) , { code : error . code } ) ,
39+ )
40+ : resolve ( stdout . trim ( ) ) ,
4541 ) ;
42+ child . stdin ?. end ( ) ;
4643 } ) ;
4744}
4845
46+ type Ace = { who : string ; kind : number ; flags : number ; rights : number } ;
47+ type Descriptor = { owner : string ; rules : Ace [ ] } ;
48+ export function classifyWindowsProfileAcl ( value : { user : string ; items : Descriptor [ ] } ) : string {
49+ const sid = ( s : unknown ) => typeof s === 'string' && / ^ S - 1 - (?: \d + - ) + \d + $ / . test ( s ) ;
50+ if ( ! sid ( value ?. user ) || ! Array . isArray ( value . items ) || value . items . length !== 2 )
51+ return 'unreadable' ;
52+ const trusted = new Set ( [ value . user , 'S-1-5-18' , 'S-1-5-32-544' ] ) ;
53+ for ( const [ index , item ] of value . items . entries ( ) ) {
54+ if ( ! item || ! trusted . has ( item . owner ) ) return index === 0 ? 'directory-owner' : 'key-owner' ;
55+ if ( ! Array . isArray ( item . rules ) || ! item . rules . length ) return 'empty-acl' ;
56+ for ( const ace of item . rules ) {
57+ if (
58+ ! ace ||
59+ ! sid ( ace . who ) ||
60+ ! [ 0 , 1 ] . includes ( ace . kind ) ||
61+ ! Number . isInteger ( ace . flags ) ||
62+ ! Number . isInteger ( ace . rights )
63+ )
64+ return 'unreadable' ;
65+ if ( ace . kind === 1 || ( ace . flags & 2 ) !== 0 || trusted . has ( ace . who ) ) continue ;
66+ if ( index === 1 ) return 'shared-key' ;
67+ // Generic write/all, plus create, remove, change attributes, ACL or owner.
68+ if ( ( ace . rights & 0x500d0156 ) !== 0 ) return 'shared-directory' ;
69+ }
70+ }
71+ return 'private' ;
72+ }
73+
74+ // Only used for an existing credential. Do not print SIDs, paths or ACL text in a diagnostic.
75+ export async function orphanKeyPrivacy ( file : string , dir : string ) : Promise < string > {
76+ const script = `
77+ $ErrorActionPreference = 'Stop'
78+ try {
79+ $sid = [System.Security.Principal.SecurityIdentifier]
80+ $list = @(foreach ($target in @($env:OAA_INIT_DIR, $env:OAA_INIT_KEY)) {
81+ $acl = Get-Acl -LiteralPath $target
82+ $rules = @(foreach ($ace in $acl.GetAccessRules($true, $true, $sid)) {
83+ @{ who=$ace.IdentityReference.Value; kind=[int]$ace.AccessControlType; flags=[int]$ace.PropagationFlags; rights=[long]$ace.FileSystemRights }
84+ })
85+ @{ owner=($acl.GetOwner($sid)).Value; rules=$rules }
86+ })
87+ $result = @{ user=[System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value; items=$list }
88+ [Console]::Out.Write((ConvertTo-Json -InputObject $result -Compress -Depth 6))
89+ } catch { [Console]::Out.Write('acl-query-error'); exit 1 }` ;
90+ try {
91+ return classifyWindowsProfileAcl (
92+ JSON . parse ( await runWindowsProfileScript ( script , { OAA_INIT_DIR : dir , OAA_INIT_KEY : file } ) ) ,
93+ ) ;
94+ } catch ( e ) {
95+ const code = ( e as NodeJS . ErrnoException ) . code ;
96+ return typeof code === 'number' ? `acl-query-exit-${ code } ` : 'acl-query-unavailable' ;
97+ }
98+ }
4999/** The `icacls` argument list that limits `file` to `user`; exported for tests. */
50100export function icaclsArgs ( file : string , user : string ) : string [ ] {
51101 return [ file , '/inheritance:r' , '/grant:r' , `${ user } :F` , '/q' ] ;
0 commit comments