Skip to content

Commit 0e66671

Browse files
committed
fix(service): compare a scoped trigger's UserId the same lossy way as the paths
Addresses the CodeRabbit finding on this PR: buildWindowsTaskXml writes the live Windows account name into <UserId> when it scopes a SessionStateChangeTrigger, and that value comes back through the same `schtasks /query /xml` code-page conversion as <Command> and <Arguments>. windowsTaskTriggerScopeAcceptable still compared it exactly, so an account named outside the console code page failed the readback for exactly the reason this PR exists — a correctly scoped task read as unhealthy, and a successful elevated create was rolled back. The separator rule in taskXmlPathEquals earns its keep on this field rather than merely carrying over: a placeholder run never matches a backslash, so a DOMAIN\User scope keeps its domain verified literally. "OTHER\Người" cannot match a task scoped to "MACHINE\Người", and neither can a deeper "MACHINE\Người\sub". Two tests: the mangled forms of a non-ASCII account name are accepted, and the three near-miss accounts above are still rejected alongside an ASCII name that keeps its exact comparison. Reverting the call site fails one of them.
1 parent 912912a commit 0e66671

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎src/service.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,14 @@ function windowsTaskTriggerScopeAcceptable(element: string, expectedUserId: stri
20372037
if (userIdCount === 0) return true;
20382038
if (userIdCount !== 1) return false;
20392039
if (expectedUserId === undefined) return false;
2040-
return taskXmlDecodedValueEquals(element, "UserId", expectedUserId);
2040+
// Compared the same lossy way as Command and Arguments, and for the same reason:
2041+
// buildWindowsTaskXml writes the live account name here, so an account named outside
2042+
// the console code page comes back from `schtasks /query /xml` mangled and an exact
2043+
// comparison would reject a task that is in fact correctly scoped (#3064). The
2044+
// separator rule earns its keep on this field: a `DOMAIN\User` scope keeps its
2045+
// backslash verified literally, so a placeholder run can never swallow the domain and
2046+
// let another domain's account match.
2047+
return taskXmlDecodedPathEquals(element, "UserId", expectedUserId);
20412048
}
20422049

20432050
/** Validate the stable OpenCodex action, principal, settings, and logon trigger. */

‎tests/service.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,47 @@ describe("Windows service task", () => {
583583
});
584584

585585

586+
// A scoped trigger names the installing account, and that name goes through the same
587+
// code page as the paths do (CodeRabbit review on #3067).
588+
test("accepts a scoped trigger whose account name the code page could not carry", () => {
589+
const wscript = "C:\\Windows\\System32\\wscript.exe";
590+
const launcher = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs";
591+
const user = "MACHINE\\Người";
592+
const xml = buildWindowsTaskXml("ignored.cmd", launcher, undefined, user)
593+
.replace(/<Command>.*?<\/Command>/, `<Command>${wscript}</Command>`);
594+
expect(windowsTaskRegistrationHealthy(xml, wscript, launcher, user)).toBe(true);
595+
596+
for (const mangled of [
597+
xml.replace("Người", "Ng??i"),
598+
xml.replace("Người", "Ngi"),
599+
xml.replace("Người", "Ng\uFFFDi"),
600+
]) expect(windowsTaskRegistrationHealthy(mangled, wscript, launcher, user)).toBe(true);
601+
});
602+
603+
test("a scoped trigger for a different account is still rejected", () => {
604+
const wscript = "C:\\Windows\\System32\\wscript.exe";
605+
const launcher = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs";
606+
const user = "MACHINE\\Người";
607+
const xml = buildWindowsTaskXml("ignored.cmd", launcher, undefined, user)
608+
.replace(/<Command>.*?<\/Command>/, `<Command>${wscript}</Command>`);
609+
610+
// The relaxation must not reach across the domain separator: a placeholder run
611+
// stands in for unrepresentable characters only, never for a `\`. Otherwise
612+
// "OTHER\\admin" could match a scope that names this machine.
613+
for (const other of [
614+
"MACHINE\\Nguoi",
615+
"OTHER\\Người",
616+
"MACHINE\\Người\\sub",
617+
]) expect(windowsTaskRegistrationHealthy(xml, wscript, launcher, other)).toBe(false);
618+
619+
// And an ASCII account name keeps its exact comparison.
620+
const ascii = "MACHINE\\installer";
621+
const asciiXml = buildWindowsTaskXml("ignored.cmd", launcher, undefined, ascii)
622+
.replace(/<Command>.*?<\/Command>/, `<Command>${wscript}</Command>`);
623+
expect(windowsTaskRegistrationHealthy(asciiXml, wscript, launcher, ascii)).toBe(true);
624+
expect(windowsTaskRegistrationHealthy(asciiXml, wscript, launcher, "MACHINE\\installr")).toBe(false);
625+
});
626+
586627
// --- #3064: a non-ASCII profile path survives the schtasks code page --------
587628
test("accepts a registration whose path the schtasks code page could not carry", () => {
588629
const wscript = "C:\\Windows\\System32\\wscript.exe";

0 commit comments

Comments
 (0)