Skip to content

Commit f8a8f92

Browse files
author
hzq
committed
Read profile ACLs with the native Windows runtime and system modules
1 parent c163394 commit f8a8f92

3 files changed

Lines changed: 52 additions & 19 deletions

File tree

‎src/core/win-acl.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7579
export 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+
/^(identity|directory-descriptor|key-descriptor|access-rules|serialize):[A-Za-z]{1,64}Exception$/.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';

‎test/profile-init.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ import {
1919
import os from 'node:os';
2020
import path from 'node:path';
2121
import { initialize, loadConfig } from '../src/config/config.js';
22-
import { classifyWindowsProfileAcl, restrictToOwner } from '../src/core/win-acl.js';
22+
import {
23+
classifyWindowsProfileAcl,
24+
restrictToOwner,
25+
runWindowsProfileScript,
26+
} from '../src/core/win-acl.js';
27+
28+
test(
29+
'native profile probes do not load modules advertised by the invoking shell',
30+
{ skip: process.platform !== 'win32' },
31+
async () => {
32+
const answer = await runWindowsProfileScript(
33+
"if ($env:PSModulePath.Contains('oaa-untrusted-module-path')) {exit 2}; [Console]::Out.Write('system-module-path')",
34+
{ PSModulePath: 'C:\\oaa-untrusted-module-path' },
35+
);
36+
assert.equal(answer, 'system-module-path');
37+
},
38+
);
2339

2440
test('Windows owner-key decisions distinguish readable directories from writable directories or shared keys', () => {
2541
const self = 'S-1-5-21-123-1001',

‎test/profile-update.test.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,9 @@ test('a profile edit publishes whole bytes, preserves protection and rejects sta
4141
execFileSync('/bin/chmod', ['+a', 'everyone deny execute', file]);
4242
execFileSync('/usr/bin/xattr', ['-w', 'dev.oaa.test', 'retained', file]);
4343
}
44-
const descriptor = () =>
45-
execFileSync(
46-
'powershell.exe',
47-
[
48-
'-NoProfile',
49-
'-NonInteractive',
50-
'-Command',
51-
'(Get-Acl -LiteralPath $env:OAA_TEST_CONFIG).Sddl',
52-
],
53-
{ env: { ...process.env, OAA_TEST_CONFIG: file }, encoding: 'utf8' },
54-
).trim();
44+
const descriptor = () => execFileSync('icacls.exe', [file], { encoding: 'utf8' }).trim();
5545
const originalAcl = process.platform === 'win32' ? descriptor() : null;
46+
if (originalAcl !== null) await writeFile(file + ':oaa-profile-test', 'retained');
5647
const mode = (await lstat(file)).mode;
5748
await replaceProfileConfig(file, original, changed);
5849
assert.equal(await readFile(file, 'utf8'), changed);
@@ -64,7 +55,10 @@ test('a profile edit publishes whole bytes, preserves protection and rejects sta
6455
'retained',
6556
);
6657
}
67-
if (originalAcl !== null) assert.equal(descriptor(), originalAcl);
58+
if (originalAcl !== null) {
59+
assert.ok(descriptor() === originalAcl, 'the destination DACL changed');
60+
assert.equal(await readFile(file + ':oaa-profile-test', 'utf8'), 'retained');
61+
}
6862
await assert.rejects(replaceProfileConfig(file, original, 'stale'), {
6963
code: 'profile_config_conflict',
7064
});

0 commit comments

Comments
 (0)