Skip to content

Commit bb984ad

Browse files
authored
Merge pull request #1881 from lidge-jun/fix/windows-icacls-trusted-path
fix(windows): make the suite pass on an unelevated Windows checkout
2 parents 8b67220 + e7a71c1 commit bb984ad

49 files changed

Lines changed: 615 additions & 161 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎gui/tests/models-native-group-controls.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ test("the native group keeps its window readable with the cap switched off", asy
6060
expect(src).toContain("{(capOn || nativeProviderGroup) && (");
6161
// With the cap off the stored value is only what a future toggle would apply — the 350k
6262
// default — so the display falls back to the widest window the rows actually advertise.
63-
expect(src).toContain("const capDisplayValue = capOn ? providerCap : (widestRowWindow ?? providerCap);");
63+
// Matched as separate fragments because the expression is wrapped across lines now, and
64+
// it grew a native branch: with the cap off the native group shows its default window
65+
// rather than the widest advertised row. A single-line literal pinned the formatting
66+
// instead of the behaviour and broke on the reflow that introduced that branch.
67+
expect(src).toContain("const capDisplayValue = capOn");
68+
expect(src).toContain("nativeProviderGroup ? NATIVE_GPT56_DEFAULT_WINDOW : (widestRowWindow ?? providerCap)");
6469
// The select is inert until the cap is actually on: showing a number is not the same as
6570
// offering to change one.
6671
expect(src).toContain("disabled={busy || !capOn}");

‎scripts/test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export function createIsolatedTestEnvironment(
3737
// real-home write guard can still know which path to protect.
3838
// (devlog 260730_codex_rs_upstream_v2_live_handoff/070.)
3939
OCX_REAL_HOME: baseEnv.OCX_REAL_HOME ?? homedir(),
40+
// Pin git's global config to the developer's real one before HOME moves.
41+
//
42+
// git resolves ~/.gitconfig from HOME, so a sandboxed HOME makes it invisible.
43+
// That silently drops `safe.directory`, and on a checkout whose directory owner
44+
// differs from the running account -- ordinary on Windows when a tool or
45+
// installer created the tree -- every `git` call a test makes then fails with
46+
// "detected dubious ownership". The test reads that as "this is not a git
47+
// repository" and asserts against a fallback, which looks like a product bug in
48+
// whichever adapter collected the metadata. Naming the file keeps the sandbox
49+
// (git still writes nothing here) while leaving git's own trust decisions intact.
50+
GIT_CONFIG_GLOBAL: baseEnv.GIT_CONFIG_GLOBAL ?? join(homedir(), ".gitconfig"),
4051
HOME: root,
4152
USERPROFILE: root,
4253
OPENCODEX_HOME: opencodexHome,

‎src/codex/injected-marker.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* them here breaks that cycle. `inject.ts` imports them back and re-exports the
88
* two public predicates, so external callers see no change.
99
*/
10+
import { parseTomlString } from "./paths";
11+
1012
export const OCX_SECTION_MARKER = "# Auto-injected by opencodex";
1113

1214
export function isRootOpenaiBaseUrlLine(line: string): boolean {
@@ -16,7 +18,11 @@ export function isRootOpenaiBaseUrlLine(line: string): boolean {
1618
export function tomlStringPattern(key: string): RegExp {
1719
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1820
const keyToken = `(?:${escaped}|"${escaped}"|'${escaped}')`;
19-
return new RegExp(`^\\s*${keyToken}\\s*=\\s*["']([^"']+)["']\\s*(?:#.*)?$`);
21+
// The quoted value is captured WITH its quotes so callers can decode it as TOML.
22+
// A basic string escapes backslashes, so a Windows path is stored doubled; reading
23+
// the raw bytes back returned a path that matched nothing on disk and made the
24+
// journal's recorded catalog path un-restorable (#1798).
25+
return new RegExp(`^\\s*${keyToken}\\s*=\\s*("(?:\\\\.|[^"])*"|'[^']*')\\s*(?:#.*)?$`);
2026
}
2127

2228
export function rootTomlString(content: string, key: string): string | null {
@@ -26,7 +32,7 @@ export function rootTomlString(content: string, key: string): string | null {
2632
const pattern = tomlStringPattern(key);
2733
for (const line of rootLines) {
2834
const match = pattern.exec(line);
29-
if (match?.[1]) return match[1].trim();
35+
if (match?.[1]) return parseTomlString(match[1]).trim();
3036
}
3137
return null;
3238
}
@@ -45,7 +51,7 @@ export function providerTableString(content: string, provider: string, key: stri
4551
const pattern = tomlStringPattern(key);
4652
for (let index = start + 1; index < lines.length && !/^\s*\[/.test(lines[index]); index += 1) {
4753
const match = pattern.exec(lines[index]);
48-
if (match?.[1]) return match[1].trim();
54+
if (match?.[1]) return parseTomlString(match[1]).trim();
4955
}
5056
return null;
5157
}

‎src/codex/user-identity.ts‎

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ const SID_PATTERN = /^S-1-(?:\d+-)+\d+$/i;
3939
*/
4040
const WINDOWS_POWERSHELL_LOOKUP_TIMEOUT_MS = 8_000;
4141

42+
/**
43+
* FOLDERID_LocalAppData, and the flag that makes the lookup ignore the caller's
44+
* environment.
45+
*
46+
* The obvious spelling, .NET's
47+
* `GetFolderPath(SpecialFolder.LocalApplicationData)`, is unusable here: on
48+
* Windows it resolves through `USERPROFILE`, and when the profile named there has
49+
* no local AppData directory on disk it returns an EMPTY STRING rather than an
50+
* error. Any
51+
* caller that redirected `USERPROFILE` (the test sandbox does, and so does a
52+
* service account whose profile has not been materialized) therefore refused every
53+
* coordinator lookup with "returned an empty value", which is the exact
54+
* environment dependence this module exists to eliminate.
55+
*
56+
* `SHGetKnownFolderPath` with a null token and `KF_FLAG_DEFAULT_PATH` reads the
57+
* known-folder registration for the effective token instead: it returns the real
58+
* per-user path whether or not the directory exists, and it is unaffected by
59+
* `USERPROFILE`, `LOCALAPPDATA`, `HOMEDRIVE`, or `HOMEPATH`. A non-null token argument
60+
* is NOT equivalent: passing (HANDLE)-1 resolves the DEFAULT USER profile
61+
* (the built-in "Default" profile), which would key coordination to a namespace
62+
* no real account writes to.
63+
*/
64+
const WINDOWS_LOCAL_APPDATA_FOLDER_ID = "F1B32785-6FBA-4FCF-9D55-7B8E7F157091";
65+
const WINDOWS_KF_FLAG_DEFAULT_PATH = "0x00000400";
66+
4267
export class CodexUserIdentityRefusal extends Error {
4368
readonly code = "CODEX_USER_IDENTITY_REFUSED";
4469

@@ -93,6 +118,42 @@ export function windowsIdentityPowerShellCommandForTests(expression: string): st
93118
return windowsIdentityPowerShellCommand(expression);
94119
}
95120

121+
/**
122+
* PowerShell expression yielding the effective account's local AppData path.
123+
*
124+
* P/Invoke rather than a .NET convenience wrapper, for the reason recorded on
125+
* WINDOWS_LOCAL_APPDATA_FOLDER_ID: the wrapper follows `USERPROFILE` and answers
126+
* an empty string for a profile whose directory is absent, which is precisely
127+
* the environment dependence this module refuses to inherit. The type is added
128+
* under a unique name per process because `Add-Type` cannot redefine one.
129+
*
130+
* The whole sequence is wrapped in one `$(...)` subexpression because the caller
131+
* substitutes this text into `[string](<expression>)`; several statements
132+
* spliced in bare would close that cast's parenthesis early and fail to parse.
133+
*/
134+
function windowsLocalAppDataExpression(): string {
135+
const signature =
136+
'[DllImport("shell32.dll", CharSet = CharSet.Unicode)] public static extern int '
137+
+ 'SHGetKnownFolderPath(ref System.Guid id, uint flags, System.IntPtr token, out System.IntPtr path);';
138+
const statements = [
139+
`$ocxShell = Add-Type -MemberDefinition '${signature}'`
140+
+ " -Name OcxKnownFolder -Namespace OcxIdentity -PassThru",
141+
`$ocxFolderId = [System.Guid]'${WINDOWS_LOCAL_APPDATA_FOLDER_ID}'`,
142+
"$ocxPathPtr = [System.IntPtr]::Zero",
143+
"$ocxHr = $ocxShell::SHGetKnownFolderPath([ref]$ocxFolderId, "
144+
+ `${WINDOWS_KF_FLAG_DEFAULT_PATH}, [System.IntPtr]::Zero, [ref]$ocxPathPtr)`,
145+
"if ($ocxHr -ne 0) { throw 'SHGetKnownFolderPath failed' }",
146+
"try { [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ocxPathPtr) }"
147+
+ " finally { [System.Runtime.InteropServices.Marshal]::FreeCoTaskMem($ocxPathPtr) }",
148+
];
149+
return `$(${statements.join("; ")})`;
150+
}
151+
152+
/** Test-only readback of the environment-independent known-folder expression. */
153+
export function windowsLocalAppDataExpressionForTests(): string {
154+
return windowsLocalAppDataExpression();
155+
}
156+
96157
/** Test-only readback of the spawn options shared by the identity lookups (#1278). */
97158
export function windowsIdentityPowerShellSpawnOptionsForTests(): ReturnType<
98159
typeof windowsIdentityPowerShellSpawnOptions
@@ -122,7 +183,31 @@ export function decodeWindowsIdentityPowerShellOutputForTests(output: Uint8Array
122183
return decodeWindowsIdentityPowerShellOutput(output);
123184
}
124185

186+
/**
187+
* Per-process memo for the Windows lookups.
188+
*
189+
* Both values -- the effective token's SID and its known-folder local AppData --
190+
* are fixed for the lifetime of a process: neither can change without a new logon
191+
* token, and the lookups deliberately ignore the environment, so nothing a caller
192+
* does between two calls can alter the answer. Each call otherwise spawns a fresh
193+
* PowerShell, roughly 150ms for the SID and 310ms for the folder, and the
194+
* coordinator asks for both on every config write and lock acquisition. That cost
195+
* pushed real multi-process injection tests past their budget while proving
196+
* nothing: the second spawn re-derives what the first already established.
197+
*
198+
* Only successful lookups are memoized, so a transient failure cannot pin the
199+
* process into a permanently refusing state.
200+
*/
201+
const windowsIdentityValueCache = new Map<string, string>();
202+
203+
/** Test-only reset so a suite can force a fresh lookup. */
204+
export function resetWindowsIdentityValueCacheForTests(): void {
205+
windowsIdentityValueCache.clear();
206+
}
207+
125208
function powershellValue(expression: string): string {
209+
const memoized = windowsIdentityValueCache.get(expression);
210+
if (memoized !== undefined) return memoized;
126211
let command: string[];
127212
try {
128213
command = windowsIdentityPowerShellCommand(expression);
@@ -145,6 +230,7 @@ function powershellValue(expression: string): string {
145230
if (result.exitCode !== 0) refuse("Windows effective-account lookup failed.");
146231
const value = decodeWindowsIdentityPowerShellOutput(result.stdout ?? Buffer.alloc(0));
147232
if (!value) refuse("Windows effective-account lookup returned an empty value.");
233+
windowsIdentityValueCache.set(expression, value);
148234
return value;
149235
}
150236

@@ -265,9 +351,7 @@ export function probeCodexCoordinatorNamespace(identity: UserIdentity): Coordina
265351
}
266352

267353
if (!SID_PATTERN.test(identity.sid)) refuse("The coordinator identity contains an invalid SID.");
268-
const localAppData = powershellValue(
269-
"[Environment]::GetFolderPath([Environment+SpecialFolder]::LocalApplicationData)",
270-
);
354+
const localAppData = powershellValue(windowsLocalAppDataExpression());
271355
if (!isAbsolute(localAppData)) refuse("Windows LocalAppData resolution returned a relative path.");
272356
const root = resolve(localAppData, "OpenCodex", "Runtime", "v1", identity.sid.toUpperCase());
273357
let entry;
@@ -297,9 +381,7 @@ export function probeCodexCoordinatorNamespace(identity: UserIdentity): Coordina
297381

298382
function resolveWindowsRuntimeRoot(identity: Extract<UserIdentity, { platform: "win32" }>): string {
299383
if (!SID_PATTERN.test(identity.sid)) refuse("The coordinator identity contains an invalid SID.");
300-
const localAppData = powershellValue(
301-
"[Environment]::GetFolderPath([Environment+SpecialFolder]::LocalApplicationData)",
302-
);
384+
const localAppData = powershellValue(windowsLocalAppDataExpression());
303385
if (!isAbsolute(localAppData)) refuse("Windows LocalAppData resolution returned a relative path.");
304386

305387
// The SID and known-folder values come from the effective token/.NET OS APIs,

‎src/lab/projection/rebuild.ts‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,57 +122,66 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
122122

123123
const db = new Database(paths.sqlitePath);
124124
let transactionOpen = false;
125+
// Every statement prepared below, so they can be finalized before the close.
126+
//
127+
// Bun keeps a prepared statement alive until it is finalized or garbage collected,
128+
// and on Windows an unfinalized statement holds the database file open: `close()`
129+
// leaves the handle, and `close(true)` throws "database is locked". A second
130+
// rebuild then failed to unlink the previous projection with EBUSY, and the retry
131+
// loop in `wipeSqlite` could only turn that into a slower failure. POSIX allows
132+
// unlinking an open file, which is why this never surfaced there.
133+
const prepared: Array<{ finalize(): void }> = [];
134+
const prepare = (sql: string) => {
135+
const statement = db.prepare(sql);
136+
prepared.push(statement);
137+
return statement;
138+
};
125139
try {
126140
db.exec("PRAGMA journal_mode=DELETE;");
127141
db.exec("PRAGMA foreign_keys=OFF;");
128142
db.exec("BEGIN IMMEDIATE;");
129143
transactionOpen = true;
130144
resetProjectionSchema(db);
131145

132-
db.prepare(
133-
"INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)",
134-
).run("schema_version", String(LAB_SQLITE_SCHEMA_VERSION));
135-
db.prepare(
136-
"INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)",
137-
).run("projection_spec_version", LAB_PROJECTION_SPEC_VERSION);
138-
db.prepare(
139-
"INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)",
140-
).run("built_at_ms", String(Date.now()));
146+
const insertMeta = prepare("INSERT OR REPLACE INTO schema_meta(key, value) VALUES (?, ?)");
147+
insertMeta.run("schema_version", String(LAB_SQLITE_SCHEMA_VERSION));
148+
insertMeta.run("projection_spec_version", LAB_PROJECTION_SPEC_VERSION);
149+
insertMeta.run("built_at_ms", String(Date.now()));
141150

142-
const insertCorruption = db.prepare(
151+
const insertCorruption = prepare(
143152
"INSERT INTO corruption(kind, line_number, event_id, detail) VALUES (?, ?, ?, ?)",
144153
);
145154
for (const c of corruptions) {
146155
insertCorruption.run(c.kind, c.lineNumber ?? null, c.eventId ?? null, c.detail);
147156
}
148157

149-
const insertEvent = db.prepare(
158+
const insertEvent = prepare(
150159
`INSERT INTO events(event_id, event_kind, recorded_at, producer, producer_version, payload_json, excluded, exclusion_reason)
151160
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
152161
);
153-
const insertSubject = db.prepare(
162+
const insertSubject = prepare(
154163
`INSERT OR IGNORE INTO subjects(subject_id, subject_kind, subject_json) VALUES (?, ?, ?)`,
155164
);
156-
const insertObs = db.prepare(
165+
const insertObs = prepare(
157166
`INSERT INTO observations(
158167
event_id, subject_id, evidence_layer, suite_id, suite_version, suite_manifest_digest,
159168
scenario_id, scenario_version, scenario_manifest_digest, outcome, completed_at, execution_mode
160169
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
161170
);
162-
const insertClaim = db.prepare(
171+
const insertClaim = prepare(
163172
`INSERT INTO claims(
164173
event_id, subject_id, capability, polarity, source_manifest_digest,
165174
effective_at, recorded_at, supersedes_json, current, usable
166175
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
167176
);
168-
const insertInv = db.prepare(
177+
const insertInv = prepare(
169178
`INSERT INTO invalidations(event_id, reason, targets_json, recorded_at, applied) VALUES (?, ?, ?, ?, ?)`,
170179
);
171-
const insertPurge = db.prepare(
180+
const insertPurge = prepare(
172181
`INSERT INTO purges(event_id, target_event_ids_json, target_artifact_digests_json, purge_actions_json, recorded_at)
173182
VALUES (?, ?, ?, ?, ?)`,
174183
);
175-
const insertArtifact = db.prepare(
184+
const insertArtifact = prepare(
176185
`INSERT INTO artifacts(digest, artifact_class, media_type, byte_count, status, last_error)
177186
VALUES (?, ?, ?, ?, ?, ?)
178187
ON CONFLICT(digest) DO UPDATE SET
@@ -319,7 +328,7 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
319328
}
320329
}
321330

322-
const insertVerdict = db.prepare(
331+
const insertVerdict = prepare(
323332
`INSERT INTO verdicts(
324333
projection_key, subject_id, evidence_layer, suite_id, suite_version, suite_manifest_digest,
325334
projection_spec_version, verdict, as_of, scenario_manifest_digests_json, claim_source_digest,
@@ -369,6 +378,15 @@ export function rebuildLabProjection(configDir?: string): RebuildResult {
369378
} catch {
370379
// Closing the disposable DB is still safe if pragma restoration fails.
371380
}
381+
// Finalize before closing: an outstanding statement keeps the file open on
382+
// Windows, and the next rebuild cannot unlink the projection it is replacing.
383+
for (const statement of prepared) {
384+
try {
385+
statement.finalize();
386+
} catch {
387+
// A statement already finalized by an error path is not a rebuild failure.
388+
}
389+
}
372390
db.close();
373391
artifactStore.close();
374392
}

‎src/lib/windows-elevation.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function assertTrustedSystemExecutableForTests(candidate: string, label:
175175
return assertTrustedSystemExecutable(candidate, label);
176176
}
177177

178-
type ElevationExeOverrides = { powershell?: string; schtasks?: string; taskkill?: string };
178+
type ElevationExeOverrides = { powershell?: string; schtasks?: string; taskkill?: string; icacls?: string };
179179
let elevationExeOverridesForTests: ElevationExeOverrides | null = null;
180180

181181
/**
@@ -220,6 +220,15 @@ export function resolveTrustedWindowsTaskkillExe(): string {
220220
return assertTrustedSystemExecutable(candidate, "taskkill.exe");
221221
}
222222

223+
/** Absolute path to System32\\icacls.exe from a trusted system directory. */
224+
export function resolveTrustedWindowsIcaclsExe(): string {
225+
if (elevationExeOverridesForTests?.icacls) {
226+
return elevationExeOverridesForTests.icacls;
227+
}
228+
const candidate = join(resolveTrustedWindowsSystemDirectory(), "icacls.exe");
229+
return assertTrustedSystemExecutable(candidate, "icacls.exe");
230+
}
231+
223232
/** Stable machine-readable marker for a denied `schtasks /create`. Crosses the CLI→proxy boundary. */
224233
export const WINDOWS_SCHTASKS_CREATE_ACCESS_DENIED_MARKER =
225234
"OCX_ERROR_CODE=WINDOWS_SCHTASKS_CREATE_ACCESS_DENIED";

0 commit comments

Comments
 (0)