fix: recheck process absence after an owner observation exit race
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
## 1.20.1 · 真实案例任务控制修复
|
||||
|
||||
- Windows停止核验处理进程在快照和用户查询之间退出的竞态,仅经新查询确认进程不存在才判定退出;用户权限失败、查询失败或PID复用继续拒绝。
|
||||
|
||||
- 原目标、检查约定与短句续接独立保存。错误诊断使用只读Run,答复结束保留原任务状态;明确修复要求仍正常执行。
|
||||
- 审查后继续改为中性系统操作,不再新增通用测试要求。无需测试的文件在审查应用后直接反馈路径,模型文字不替代文件交付或检查通过。
|
||||
- 完整终结响应中的空参数只对schema明确允许空对象的工具兼容;必填参数、坏JSON、截断流继续整批拒绝,原流与适配凭据分别保存。
|
||||
|
||||
@@ -22,17 +22,25 @@ export function protectUserDirectory(directory) {
|
||||
catch { throw hostError('无法为本机秘密目录设置当前Windows用户权限。', 'HOST_DIRECTORY_UNPROTECTED'); }
|
||||
}
|
||||
|
||||
export function observeWindowsProcess(pid) {
|
||||
if (process.platform !== 'win32') throw hostError('此桌面宿主核验需要Windows。', 'HOST_PLATFORM_UNSUPPORTED');
|
||||
export function windowsProcessObservationScript(pid) {
|
||||
if (!Number.isInteger(pid) || pid < 1 || pid > 2147483647) throw hostError('宿主PID无效。');
|
||||
const script = "$ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue';[Console]::OutputEncoding=New-Object System.Text.UTF8Encoding($false);"
|
||||
+ '$awbProcess=Get-CimInstance Win32_Process -Filter "ProcessId=' + pid + '";'
|
||||
const query = 'Get-CimInstance Win32_Process -Filter "ProcessId=' + pid + '"';
|
||||
return "$ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue';[Console]::OutputEncoding=New-Object System.Text.UTF8Encoding($false);"
|
||||
+ '$awbProcess=' + query + ';'
|
||||
+ 'if($null -eq $awbProcess){Write-Output "null";exit 0};'
|
||||
+ '$awbOwner=Invoke-CimMethod -InputObject $awbProcess -MethodName GetOwnerSid;'
|
||||
+ 'try{$awbOwner=Invoke-CimMethod -InputObject $awbProcess -MethodName GetOwnerSid;'
|
||||
+ 'if($awbOwner.ReturnValue -ne 0 -or -not $awbOwner.Sid){throw "Owner identity unavailable"};'
|
||||
// A process can exit after the snapshot and before its owner is queried.
|
||||
// Only a fresh successful absence query can establish that it has gone.
|
||||
+ '}catch{$awbRemaining=' + query + ';if($null -eq $awbRemaining){Write-Output "null";exit 0};throw};'
|
||||
+ '[pscustomobject]@{pid=[int]$awbProcess.ProcessId;parentPid=[int]$awbProcess.ParentProcessId;'
|
||||
+ 'createdAt=$awbProcess.CreationDate.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ");'
|
||||
+ 'executable=$awbProcess.ExecutablePath;userSid=$awbOwner.Sid}|ConvertTo-Json -Compress';
|
||||
}
|
||||
|
||||
export function observeWindowsProcess(pid) {
|
||||
if (process.platform !== 'win32') throw hostError('此桌面宿主核验需要Windows。', 'HOST_PLATFORM_UNSUPPORTED');
|
||||
const script = windowsProcessObservationScript(pid);
|
||||
try {
|
||||
const shell = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32/WindowsPowerShell/v1.0/powershell.exe');
|
||||
const output = execFileSync(shell, ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(script, 'utf16le').toString('base64')],
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import path from 'node:path';
|
||||
import { spawn, execFileSync } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
import { observeWindowsProcess, windowsProcessObservationScript } from '../src/core/desktop/process-identity.mjs';
|
||||
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
||||
|
||||
const shell = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32/WindowsPowerShell/v1.0/powershell.exe');
|
||||
const encoded = script => ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(script, 'utf16le').toString('base64')];
|
||||
const execute = script => execFileSync(shell, encoded(script), { windowsHide: true, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 10000, env: projectEnvironment() });
|
||||
|
||||
test('a process exiting between the Windows snapshot and owner query is absent only after a fresh query', { timeout: 30000 }, async t => {
|
||||
const current = observeWindowsProcess(process.pid);
|
||||
assert.equal(current.pid, process.pid); assert.equal(current.executable, process.execPath);
|
||||
const child = spawn(process.execPath, ['-e', "process.stdin.once('data',()=>process.exit(0));setInterval(()=>{},1000)"], { windowsHide: true, stdio: ['pipe', 'ignore', 'ignore'], env: projectEnvironment() });
|
||||
const ended = once(child, 'close');
|
||||
t.after(async () => { if (child.exitCode === null) child.stdin.end('exit'); await ended; });
|
||||
const script = windowsProcessObservationScript(child.pid).replace('$awbOwner=Invoke-CimMethod', '[Console]::Out.WriteLine("SNAPSHOT");[Console]::Out.Flush();[Console]::In.ReadLine()|Out-Null;$awbOwner=Invoke-CimMethod');
|
||||
const observer = spawn(shell, encoded(script), { windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'], env: projectEnvironment() });
|
||||
const observed = once(observer, 'close'); let output = '', errors = '';
|
||||
const snapshot = new Promise(resolve => observer.stdout.on('data', bytes => { output += bytes.toString('utf8'); if (output.includes('SNAPSHOT')) resolve(); }));
|
||||
observer.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
|
||||
t.after(() => { if (observer.exitCode === null) { observer.stdin.end('\n'); observer.kill(); } });
|
||||
await Promise.race([snapshot, observed.then(() => { throw new Error('Observer ended before the snapshot: ' + errors); })]);
|
||||
child.stdin.end('exit'); assert.equal((await ended)[0], 0);
|
||||
observer.stdin.end('\n'); assert.equal((await observed)[0], 0, errors);
|
||||
assert.equal(output.replace('SNAPSHOT', '').trim(), 'null');
|
||||
assert.equal(observeWindowsProcess(child.pid), null);
|
||||
});
|
||||
|
||||
test('owner observation errors remain errors while a process is present, replaced, or cannot be rechecked', { timeout: 30000 }, () => {
|
||||
const script = windowsProcessObservationScript(process.pid);
|
||||
assert.throws(() => execute('function Invoke-CimMethod {throw "injected owner denial"};' + script));
|
||||
assert.throws(() => execute('function Invoke-CimMethod {[pscustomobject]@{ReturnValue=2;Sid=$null}};' + script));
|
||||
const fakeSnapshot = '[pscustomobject]@{ProcessId=' + process.pid + ';ParentProcessId=1;CreationDate=[DateTime]::UtcNow;ExecutablePath="existing"}';
|
||||
assert.throws(() => execute('$awbQueries=0;function Get-CimInstance {$script:awbQueries++;if($script:awbQueries -gt 1){throw "injected query denial"};' + fakeSnapshot + '};function Invoke-CimMethod {throw "injected owner denial"};' + script));
|
||||
assert.throws(() => execute('function Get-CimInstance {' + fakeSnapshot + '};function Invoke-CimMethod {throw "injected replaced process owner"};' + script));
|
||||
assert.throws(() => windowsProcessObservationScript('1;exit 0'));
|
||||
});
|
||||
Reference in New Issue
Block a user