diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 67d397ff..e7b8a2d2 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)。 + ## [0.5.6] — 2026-11-21 — Patch release - ops-ptc's batching teacher trajectory now teaches sandbox authority: the lesson states the 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