Skip to content

Commit b727f8f

Browse files
committed
fix(service): forgive only what the code page mangled in a scheduler path
Reimplements #3067 (author @ntdatt812). The diagnosis is right and the relocation is right: schtasks converts its XML through the console code page before the bytes exist, so runFile reading as a buffer cannot help. A profile named outside that page comes back as C:\Users\???\... and the exact comparison rejected a registration this process had just created correctly, so `ocx service install` rolled it back (#3064). The remedy needed narrowing. #3067 compiles every unrepresentable run to `[^\\/]*`, which forbids a path separator but allows arbitrary ASCII. A segment that is ENTIRELY non-ASCII then has no anchors left, so C:\Users\<CJK>\.opencodex\service-launcher.vbs matches C:\Users\Admin\.opencodex\service-launcher.vbs and this process would adopt, repair, or delete another account's task. The same hole applies to <UserId>, where MACHINE\<CJK> would match MACHINE\Admin. Its tests use "Người", whose surviving Ng and i letters hide the case. Here an unrepresentable run may match only a run of substitution characters -- '?' per character, U+FFFD, or nothing -- and every ASCII segment, including every separator, is matched literally. A foreign account's path fails because "admin" is not a run of substitutions. Mutation-checked: widening the class back to `[^\\/]*` gives 186 pass / 1 fail, exactly "rejects another account's path that is merely the same shape". Closes #3064.
1 parent 7b1b4ce commit b727f8f

2 files changed

Lines changed: 121 additions & 3 deletions

File tree

‎src/service.ts‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,67 @@ function taskXmlDecodedValueEquals(xml: string, tag: string, expected: string):
19631963
return taskXmlDecodeEntities(value).trim().toLowerCase() === expected.trim().toLowerCase();
19641964
}
19651965

1966+
/**
1967+
* Characters a console code page substitutes when it cannot carry the original.
1968+
* Windows writes `?` per unrepresentable character, some layers write U+FFFD, and a
1969+
* few drop them entirely.
1970+
*/
1971+
const CODE_PAGE_SUBSTITUTIONS = /^[?\uFFFD]*$/;
1972+
1973+
/**
1974+
* Compare a value that OpenCodex itself wrote against what `schtasks /query /xml` read
1975+
* back, tolerating ONLY the characters the console code page could not carry.
1976+
*
1977+
* `runFile` already reads the query as bytes, so this is not a spawn-decoding bug: the
1978+
* conversion happens inside `schtasks` before the bytes exist. A profile named outside
1979+
* the active code page — `C:\\Users\\김병준\\...` — comes back as `C:\\Users\\???\\...`, so an
1980+
* exact comparison rejected a registration this process had just created correctly and
1981+
* `ocx service install` rolled it back (#3064).
1982+
*
1983+
* The tolerance is deliberately narrow. Each unrepresentable RUN in the expected value
1984+
* may match only a run of substitution characters — never arbitrary text, and never a
1985+
* path separator. A wildcard as wide as `[^\\\\/]*` would leave a fully non-ASCII segment with
1986+
* no anchors at all, so `C:\\Users\\김병준\\x.vbs` would match `C:\\Users\\Admin\\x.vbs` and this
1987+
* process would adopt, repair, or delete another account's task. Accepting a foreign
1988+
* live task is a worse failure than the rollback this fixes.
1989+
*/
1990+
function taskXmlLossyValueEquals(reported: string, expected: string): boolean {
1991+
const a = reported.trim().toLowerCase();
1992+
const b = expected.trim().toLowerCase();
1993+
if (a === b) return true;
1994+
// Nothing unrepresentable in the expectation means there was nothing to mangle,
1995+
// so any difference is a real one.
1996+
if (!/[^\x00-\x7F]/.test(b)) return false;
1997+
const parts = b.split(/([^\x00-\x7F]+)/);
1998+
let rest = a;
1999+
for (let i = 0; i < parts.length; i += 1) {
2000+
const part = parts[i]!;
2001+
if (i % 2 === 0) {
2002+
// Literal ASCII run: it must be present verbatim, which is what keeps every
2003+
// directory boundary and file name in the path verified.
2004+
if (!rest.startsWith(part)) return false;
2005+
rest = rest.slice(part.length);
2006+
continue;
2007+
}
2008+
// Unrepresentable run: consume only substitution characters, and stop at the
2009+
// next literal so a trailing run cannot swallow the remainder of the string.
2010+
const next = parts[i + 1] ?? "";
2011+
const end = next === "" ? rest.length : rest.indexOf(next);
2012+
if (end < 0) return false;
2013+
if (!CODE_PAGE_SUBSTITUTIONS.test(rest.slice(0, end))) return false;
2014+
rest = rest.slice(end);
2015+
}
2016+
return rest === "";
2017+
}
2018+
2019+
function taskXmlDecodedLossyValueEquals(xml: string, tag: string, expected: string): boolean {
2020+
if (taskXmlHasPrefixedTag(xml, tag)) return false;
2021+
if (taskXmlElementCount(xml, tag) !== 1) return false;
2022+
const value = new RegExp(`<${tag}(?:\\s[^>]*?)?>([^<]*)<\\/${tag}>`, "i").exec(xml)?.[1];
2023+
if (value === undefined) return false;
2024+
return taskXmlLossyValueEquals(taskXmlDecodeEntities(value), expected);
2025+
}
2026+
19662027
function taskXmlOptionalValueEquals(xml: string, tag: string, expected: string): boolean {
19672028
// Check the prefixed form first: treating `<t:Enabled>false</t:Enabled>` as an
19682029
// omission would turn an explicitly disabled task into a healthy one.
@@ -2025,7 +2086,14 @@ function windowsTaskTriggerScopeAcceptable(element: string, expectedUserId: stri
20252086
if (userIdCount === 0) return true;
20262087
if (userIdCount !== 1) return false;
20272088
if (expectedUserId === undefined) return false;
2028-
return taskXmlDecodedValueEquals(element, "UserId", expectedUserId);
2089+
// Same tolerance as Command and Arguments, and for the same reason:
2090+
// buildWindowsTaskXml writes the live account name here, so an account named
2091+
// outside the console code page comes back mangled and an exact comparison
2092+
// rejects a task that is in fact correctly scoped (#3064). The literal-ASCII
2093+
// requirement is what keeps this safe -- a DOMAIN\\User scope keeps its backslash
2094+
// verified, so a substitution run can never swallow the domain and let another
2095+
// domain's account match.
2096+
return taskXmlDecodedLossyValueEquals(element, "UserId", expectedUserId);
20292097
}
20302098

20312099
/** Validate the stable OpenCodex action, principal, settings, and logon trigger. */
@@ -2058,8 +2126,12 @@ function windowsTaskRegistrationBaseHealthy(
20582126
// quotes we wrote as `&quot;` back to literal `"` on export, so an escaped
20592127
// needle never matched and a healthy task read as permanently stale (#608).
20602128
// Case-insensitive: elevated `schtasks /create` may rewrite System32 casing.
2061-
&& taskXmlDecodedValueEquals(action, "Command", wscript)
2062-
&& taskXmlDecodedValueEquals(action, "Arguments", `/b /nologo "${launcher}"`);
2129+
// Lossy on purpose: both name paths under the user profile, which the query
2130+
// cannot carry when the profile is named outside the code page (#3064). Only
2131+
// unrepresentable characters are forgiven; every ASCII segment and every
2132+
// separator is still matched literally.
2133+
&& taskXmlDecodedLossyValueEquals(action, "Command", wscript)
2134+
&& taskXmlDecodedLossyValueEquals(action, "Arguments", `/b /nologo "${launcher}"`);
20632135
}
20642136

20652137
/** Validate the security/lifecycle-critical fields of the registered scheduler task. */

‎tests/service.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,52 @@ describe("Windows service task", () => {
488488
expect(windowsTaskRegistrationHealthy(disabled, wscript, launcher)).toBe(false);
489489
});
490490

491+
/**
492+
* #3064: `schtasks /query /xml` converts the document through the console code
493+
* page before the bytes exist, so a profile named outside that page comes back
494+
* with substitution characters. An exact comparison rejected a registration this
495+
* process had just created correctly, and `ocx service install` rolled it back.
496+
*
497+
* The tolerance has to stay narrow enough that a MANGLED path still cannot match
498+
* a DIFFERENT account's path. A wildcard as wide as `[^\\/]*` leaves a fully
499+
* non-ASCII segment with no anchors at all, so `...\\김병준\\...` would match
500+
* `...\\Admin\\...` and this process would adopt another account's task.
501+
*/
502+
describe("a scheduler path the console code page could not carry", () => {
503+
const wscript = "C:\\Windows\\System32\\wscript.exe";
504+
const launcher = "C:\\Users\\김병준\\.opencodex\\service-launcher.vbs";
505+
const healthy = (reportedLauncher: string, expectedLauncher = launcher) =>
506+
windowsTaskRegistrationHealthy(
507+
buildWindowsTaskXml("ignored.cmd", reportedLauncher)
508+
.replace(/<Command>.*?<\/Command>/, `<Command>${wscript}</Command>`),
509+
wscript,
510+
expectedLauncher,
511+
);
512+
513+
test.each([
514+
["question marks, one per character", "C:\\Users\\???\\.opencodex\\service-launcher.vbs"],
515+
["a single replacement character", "C:\\Users\\\uFFFD\\.opencodex\\service-launcher.vbs"],
516+
])("accepts a registration whose profile came back as %s", (_label, reported) => {
517+
expect(healthy(reported)).toBe(true);
518+
});
519+
520+
// The reason the tolerance is a substitution class and not a wildcard.
521+
test("rejects another account's path that is merely the same shape", () => {
522+
expect(healthy("C:\\Users\\Admin\\.opencodex\\service-launcher.vbs")).toBe(false);
523+
});
524+
525+
test("rejects a path whose ASCII structure differs", () => {
526+
expect(healthy("C:\\Users\\???\\.opencodex\\other-launcher.vbs")).toBe(false);
527+
expect(healthy("D:\\Users\\???\\.opencodex\\service-launcher.vbs")).toBe(false);
528+
});
529+
530+
// An expectation with nothing unrepresentable in it has nothing to forgive.
531+
test("does not forgive substitutions when the expected path is pure ASCII", () => {
532+
const ascii = "C:\\Users\\Test\\.opencodex\\service-launcher.vbs";
533+
expect(healthy("C:\\Users\\???\\.opencodex\\service-launcher.vbs", ascii)).toBe(false);
534+
});
535+
});
536+
491537
/**
492538
* `UserId` is optional in the schema, and omitting it makes a SessionStateChangeTrigger fire
493539
* for any account's session change. Scope it to the installing account when that account is

0 commit comments

Comments
 (0)