From 3a6407060c237908484ae0289151e837346ee95d Mon Sep 17 00:00:00 2001 From: Chirag Singhal <76880977+chirag127@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:02:15 +0000 Subject: [PATCH] fix(test): parse complete fixture owner SIDs from whoami CSV --- docs/CHANGELOG.md | 3 ++ test/fixture-owner.test.mjs | 73 +++++++++++++++++++++++++++++++++++++ test/lib/fixture-owner.mjs | 14 +++++++ test/lib/harness.mjs | 3 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 test/fixture-owner.test.mjs create mode 100644 test/lib/fixture-owner.mjs diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d66194e1..78f970fa 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -7,6 +7,9 @@ this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +- Test fixtures on Windows now read the complete SID column from successful `whoami` CSV output, including Entra `S-1-12-1` identities. Failed commands, malformed records and invalid SIDs remain rejected; fixture ownership still uses the existing checked `icacls /setowner` operation (#1031). +- Windows 测试夹具现在从成功的 `whoami` CSV 输出中读取完整 SID 列,支持 Entra `S-1-12-1` 身份。命令失败、记录格式错误及无效 SID 仍被拒绝;夹具所有权继续使用原有的 `icacls /setowner` 操作及结果检查(#1031)。 + - Native tool construction and the publication refusal queue no longer attach listeners or lifetime effects to the composition root. The evidence observer and result admission subscribe on the caller's context with global dispatch, and the refusal queue falls back to the caller lifetime when the root refuses an effect. dsh-tui 0.20 refuses `root.events.on` and `root.effect` from a plugin activation, and the native-tools row is one, so every `dsh-tui --resume` aborted with "cannot resume session" before the agent mounted. - 原生工具构造与发布拒绝队列不再向组合根挂监听或生命周期 effect:证据观察者与 result admission 改为在调用方上下文订阅并声明 global 分发,拒绝队列在 root 拒绝 effect 时回落到调用方生命周期。dsh-tui 0.20 会拒绝插件激活期内的 `root.events.on` 与 `root.effect`,而原生工具行正是插件激活,导致每次 `dsh-tui --resume` 都在 agent 挂载前报 "cannot resume session"。 - The Bash tool description now states the parse contract of `stdout`/`stderr` where the model reads it: `text` may be a partial fragment, `truncated` marks one that must not be parsed as complete structured output, and `spillPath` is optional and not a completeness guarantee. The typed output schema is unchanged: repeating the contract there would cost every PTC request without saying anything the tool text does not. Command semantics, exit codes, result values, limits, spill files, permissions and rendering are unchanged. diff --git a/test/fixture-owner.test.mjs b/test/fixture-owner.test.mjs new file mode 100644 index 00000000..df0b4ed7 --- /dev/null +++ b/test/fixture-owner.test.mjs @@ -0,0 +1,73 @@ +import fs from 'node:fs' +import path from 'node:path' +import { spawnSync } from 'node:child_process' +import { fixtureOwnerSid } from './lib/fixture-owner.mjs' +import { assert, test, report, withTempDir } from './lib/harness.mjs' + +const traditional = 'S-1-5-21-100-200-300-1001' +const entra = 'S-1-12-1-100-200-300-400' +const probe = (sid = traditional, name = 'DOMAIN\\fixture') => ({ + status: 0, signal: null, stdout: `"${name}","${sid}"\r\n`, stderr: '', +}) + +await test('traditional and Entra user SIDs are returned intact', () => { + for (const sid of [traditional, entra]) assert.equal(fixtureOwnerSid(probe(sid)), sid) +}) +await test('only the SID column identifies the owner, even with misleading CSV names', () => { + for (const name of ['S-1-5-18', 'DOMAIN\\with,comma', 'DOMAIN\\escaped""quote', 'AzureAD\\用户']) { + assert.equal(fixtureOwnerSid(probe(entra, name)), entra) + } + assert.equal(fixtureOwnerSid(probe('invalid', traditional)), undefined) +}) +await test('one quoted record accepts LF, CRLF or no line terminator', () => { + for (const ending of ['', '\n', '\r\n']) { + const value = probe() + value.stdout = value.stdout.trimEnd() + ending + assert.equal(fixtureOwnerSid(value), traditional) + } +}) +await test('failed, interrupted and unspawned commands never supply an owner', () => { + for (const extra of [{ status: 1 }, { status: null }, { signal: 'SIGTERM' }, { error: new Error('spawn failed') }, { stderr: 'ERROR: command failed' }]) { + assert.equal(fixtureOwnerSid({ ...probe(), ...extra }), undefined) + } + assert.equal(fixtureOwnerSid(undefined), undefined) +}) +await test('malformed CSV, extra records, headers and diagnostics are rejected', () => { + const row = probe().stdout + for (const stdout of ['', row + row, '"User Name","SID"\r\n' + row, '\n' + row, row + '\n', + row.trimEnd() + ',"extra"', `fixture,"${traditional}"`, `"","${traditional}"`, + `"bad"quote","${traditional}"`, `ERROR: ${traditional}`, `"fixture","${traditional}`, `"fixture\0","${traditional}"`]) { + assert.equal(fixtureOwnerSid({ ...probe(), stdout }), undefined, JSON.stringify(stdout)) + } + assert.equal(fixtureOwnerSid({ ...probe(), stdout: Buffer.from(row) }), undefined) +}) +await test('complete SID syntax and numeric bounds are validated', () => { + for (const sid of ['S-1-0-0', 'S-1-281474976710655-4294967295', 'S-1-0xffffffffffff-0', 'S-1-5' + '-1'.repeat(15)]) { + assert.equal(fixtureOwnerSid(probe(sid)), sid) + } + for (const sid of ['S-1-5', 'S-2-5-18', 'S-1-5-18-', 'S-1-5-18junk', 'prefixS-1-5-18', + 'S-1-5--18', 'S-1-5-+18', 'S-1-5-1.5', 'S-1-5-4294967296', 'S-1-281474976710656-1', + 'S-1-0x1000000000000-1', 'S-1-5' + '-1'.repeat(16), ' S-1-5-18', 'S-1-5-18 ']) { + assert.equal(fixtureOwnerSid(probe(sid)), undefined, sid) + } +}) + +if (process.platform === 'win32') { + await test('a fresh temporary fixture gets the actual current user as owner', () => withTempDir('fixture-owner', directory => { + const system32 = path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32') + const identity = spawnSync(path.join(system32, 'whoami.exe'), ['/user', '/fo', 'csv', '/nh'], { encoding: 'utf8' }) + const sid = fixtureOwnerSid(identity) + assert.ok(sid, 'successful whoami must identify the user') + const owner = spawnSync(path.join(system32, 'WindowsPowerShell/v1.0/powershell.exe'), [ + '-NoProfile', '-NonInteractive', '-Command', + "$ErrorActionPreference='Stop'; [System.IO.Directory]::GetAccessControl($env:DSH_OPS_FIXTURE_OWNER_PATH).GetOwner([System.Security.Principal.SecurityIdentifier]).Value", + ], { encoding: 'utf8', env: { ...process.env, DSH_OPS_FIXTURE_OWNER_PATH: directory } }) + assert.equal(owner.status, 0, 'temporary fixture ACL must be readable') + assert.equal(owner.stdout.trim() === sid, true, 'temporary fixture owner must equal current SID') + fs.writeFileSync(path.join(directory, 'fixture.txt'), 'fixture') + assert.equal(fs.readFileSync(path.join(directory, 'fixture.txt'), 'utf8'), 'fixture') + })) +} else { + console.log('Windows fixture ownership unavailable: requires Windows whoami/icacls; pure parser checks still run') +} +report('fixture-owner') diff --git a/test/lib/fixture-owner.mjs b/test/lib/fixture-owner.mjs new file mode 100644 index 00000000..b193d417 --- /dev/null +++ b/test/lib/fixture-owner.mjs @@ -0,0 +1,14 @@ +/** Read the owner SID from whoami /user /fo csv /nh. */ +export function fixtureOwnerSid(probe) { + if (probe?.status !== 0 || probe.error || probe.signal + || typeof probe.stdout !== 'string' || typeof probe.stderr !== 'string' || probe.stderr.trim()) return undefined + const record = /^"(?:[^"\x00-\x1f\x7f]|"")+","([^"\r\n]+)"(?:\r?\n)?$/.exec(probe.stdout) + if (!record || record[0].length !== probe.stdout.length) return undefined + const sid = record[1] + const parts = /^S-1-([0-9]{1,15}|0x[0-9a-fA-F]{1,12})((?:-[0-9]{1,10}){1,15})$/.exec(sid) + if (!parts) return undefined + // SID identifier authorities are 48-bit; each of the at most 15 subauthorities is 32-bit. + if (BigInt(parts[1]) > 0xffffffffffffn + || parts[2].slice(1).split('-').some(value => BigInt(value) > 0xffffffffn)) return undefined + return sid +} diff --git a/test/lib/harness.mjs b/test/lib/harness.mjs index de991f44..5d02fde4 100644 --- a/test/lib/harness.mjs +++ b/test/lib/harness.mjs @@ -20,6 +20,7 @@ import { mkdirSync, mkdtempSync, realpathSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { fileURLToPath } from 'node:url' +import { fixtureOwnerSid } from './fixture-owner.mjs' /** The repository root, resolved from this file (`test/lib/`). */ export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..') @@ -100,7 +101,7 @@ export function ownWindowsFixture(directory) { const system32 = path.join(process.env.SystemRoot ?? 'C:\\Windows', 'System32') if (!fixtureOwner) { const probe = spawnSync(path.join(system32, 'whoami.exe'), ['/user', '/fo', 'csv', '/nh'], { encoding: 'utf8' }) - fixtureOwner = probe.stdout?.match(/S-1-5-[0-9-]+/)?.[0] + fixtureOwner = fixtureOwnerSid(probe) if (!fixtureOwner) throw new Error(`Cannot identify fixture owner: ${probe.stderr}`) } // Elevated hosted runners default to Administrators as owner. Change only the