-
Notifications
You must be signed in to change notification settings - Fork 719
fix(plugin): resolve relocated Windows Deep Scan executables #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ianw-oai
wants to merge
6
commits into
main
Choose a base branch
from
dev/ianw/windows-deep-scan-executable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be9471a
fix(plugin): resolve relocated Windows Deep Scan executables
ianw-oai d7959f3
fix(plugin): preserve parent deny rules in Deep Scan workers (#597)
ianw-oai b99859c
Merge main and preserve Windows worker fixes
ianw-oai 75dedd9
fix(plugin): keep accessible Windows executable overrides
ianw-oai b5694c1
fix(plugin): preserve worker startup diagnostics
ianw-oai f27cf8f
revert: restore original Deep Scan runtime behavior
ianw-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file modified
BIN
+550 Bytes
(100%)
sdk/typescript/_bundled_plugin/mcp/server.mjs.br.part-001
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import * as path from "node:path"; | ||
| import * as url from "node:url"; | ||
| import * as util from "node:util"; | ||
| import { expect, test } from "bun:test"; | ||
| import { parse } from "smol-toml"; | ||
| import { loadBundledRuntime } from "./plugin-root.js"; | ||
|
|
||
| type Sandbox = { filesystemDenies: string[]; globScanMaxDepth?: number }; | ||
|
|
||
| async function bundledPolicy() { | ||
| const runtime = await loadBundledRuntime(); | ||
| const start = runtime.indexOf("var CODEX_SANDBOX_STATE_META_CAPABILITY ="); | ||
| const end = runtime.indexOf("\n// ", start); | ||
| expect(start).toBeGreaterThan(0); | ||
| expect(end).toBeGreaterThan(start); | ||
| const source = runtime.slice(start, end); | ||
| const imports = [ | ||
| ...new Set(source.match(/import_node_(?:path|url|util)\d*/gu)), | ||
| ]; | ||
| const resolve = new Function( | ||
| ...imports, | ||
| "DeepScanNonRetryableError", | ||
| `${source}\nreturn resolveDeepWorkerParentSandbox;`, | ||
| )( | ||
| ...imports.map((name) => | ||
| name.startsWith("import_node_path") | ||
| ? path | ||
| : name.startsWith("import_node_url") | ||
| ? url | ||
| : util, | ||
| ), | ||
| Error, | ||
| ) as (metadata: unknown) => Sandbox; | ||
| const serializer = [ | ||
| "workerPermissionProfile", | ||
| "workerPermissionProfileConfigOverrides", | ||
| "tomlInlineValue", | ||
| "tomlKey", | ||
| "tomlString", | ||
| ] | ||
| .map((name) => { | ||
| const definition = new RegExp( | ||
| `function ${name}\\([^\\n]*\\) \\{[\\s\\S]*?\\n\\}`, | ||
| "u", | ||
| ).exec(runtime)?.[0]; | ||
| if (!definition) throw new Error(`Missing bundled function: ${name}`); | ||
| return definition; | ||
| }) | ||
| .join("\n"); | ||
| const overrides = new Function( | ||
| "DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID", | ||
| `${serializer}\nreturn sandbox => workerPermissionProfileConfigOverrides(workerPermissionProfile(sandbox));`, | ||
| )("codex_security_deep_scan_worker") as (sandbox: Sandbox) => string[]; | ||
| return { resolve, overrides }; | ||
| } | ||
|
|
||
| function metadata(entries: unknown[]) { | ||
| return { | ||
| _meta: { | ||
| "codex/sandbox-state-meta": { | ||
| permissionProfile: { | ||
| type: "managed", | ||
| network: "enabled", | ||
| file_system: { | ||
| type: "restricted", | ||
| glob_scan_max_depth: 8, | ||
| entries: [ | ||
| { | ||
| access: "read", | ||
| path: { type: "special", value: { kind: "root" } }, | ||
| }, | ||
| ...entries, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("preserves literal parent deny paths and globs without parent write grants", async () => { | ||
| const policy = await bundledPolicy(); | ||
| const denied = path.resolve("synthetic", "secret.with.dots"); | ||
| const glob = path.resolve("synthetic", "**", "*.secret"); | ||
| const sandbox = policy.resolve( | ||
| metadata([ | ||
| { | ||
| access: "write", | ||
| path: { type: "path", path: path.resolve("synthetic") }, | ||
| }, | ||
| { access: "deny", path: { type: "path", path: denied } }, | ||
| { access: "none", path: { type: "glob_pattern", pattern: glob } }, | ||
| ]), | ||
| ); | ||
| expect(sandbox).toEqual({ | ||
| filesystemDenies: [denied, glob], | ||
| globScanMaxDepth: 8, | ||
| }); | ||
| expect(parse(policy.overrides(sandbox).join("\n"))).toEqual({ | ||
| default_permissions: "codex_security_deep_scan_worker", | ||
| permissions: { | ||
| codex_security_deep_scan_worker: { | ||
| extends: ":read-only", | ||
| filesystem: { | ||
| ":root": "read", | ||
| [denied]: "deny", | ||
| [glob]: "deny", | ||
| glob_scan_max_depth: 8, | ||
| }, | ||
| network: { enabled: false }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("rejects parent denials that cannot be preserved", async () => { | ||
| const policy = await bundledPolicy(); | ||
| for (const entry of [ | ||
| { access: "deny", path: { type: "path", path: "relative/secret" } }, | ||
| { access: "deny", path: { type: "special", value: { kind: "tmpdir" } } }, | ||
| { | ||
| access: "write", | ||
| path: { type: "glob_pattern", pattern: path.resolve("synthetic", "*") }, | ||
| }, | ||
| ]) { | ||
| expect(() => policy.resolve(metadata([entry]))).toThrow( | ||
| "cannot be preserved", | ||
| ); | ||
| } | ||
| expect(() => policy.resolve({})).toThrow("trusted parent sandbox metadata"); | ||
| }); |
120 changes: 120 additions & 0 deletions
120
sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import * as fs from "node:fs"; | ||
| import { | ||
| mkdir, | ||
| mkdtemp, | ||
| realpath, | ||
| rm, | ||
| utimes, | ||
| writeFile, | ||
| } from "node:fs/promises"; | ||
| import * as module from "node:module"; | ||
| import { tmpdir } from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { expect, test } from "bun:test"; | ||
| import { loadBundledRuntime } from "./plugin-root.js"; | ||
|
|
||
| async function bundledResolver() { | ||
| const runtime = await loadBundledRuntime(); | ||
| const start = runtime.indexOf("function resolveCodexPath("); | ||
| const end = runtime.indexOf("\n// server.ts", start); | ||
| expect(start).toBeGreaterThan(0); | ||
| expect(end).toBeGreaterThan(start); | ||
| const source = runtime.slice(start, end); | ||
| const imports = [ | ||
| ...new Set(source.match(/import_node_(?:fs|path|module)\d*/gu)), | ||
| ]; | ||
| return new Function(...imports, `${source}\nreturn resolveCodexPath;`)( | ||
| ...imports.map((name) => | ||
| name.startsWith("import_node_fs") | ||
| ? fs | ||
| : name.startsWith("import_node_path") | ||
| ? path | ||
| : module, | ||
| ), | ||
| ) as ( | ||
| env: NodeJS.ProcessEnv, | ||
| platform: NodeJS.Platform, | ||
| architecture: NodeJS.Architecture, | ||
| ) => string; | ||
| } | ||
|
|
||
| test("uses the newest relocated Windows executable when WindowsApps is inaccessible", async () => { | ||
| const root = await realpath( | ||
| await mkdtemp(path.join(tmpdir(), "codex-security-executable-")), | ||
| ); | ||
| try { | ||
| const older = path.join( | ||
| root, | ||
| "OpenAI", | ||
| "Codex", | ||
| "bin", | ||
| "11111111", | ||
| "codex.exe", | ||
| ); | ||
| const newer = path.join( | ||
| root, | ||
| "OpenAI", | ||
| "Codex", | ||
| "bin", | ||
| "22222222", | ||
| "codex.exe", | ||
| ); | ||
| const empty = path.join( | ||
| root, | ||
| "OpenAI", | ||
| "Codex", | ||
| "bin", | ||
| "33333333", | ||
| "codex.exe", | ||
| ); | ||
| for (const executable of [older, newer, empty]) { | ||
| await mkdir(path.dirname(executable), { recursive: true }); | ||
| await writeFile( | ||
| executable, | ||
| executable === empty ? "" : "synthetic executable", | ||
| ); | ||
| } | ||
| await utimes(older, 1, 1); | ||
| await utimes(newer, 2, 2); | ||
| const resolve = await bundledResolver(); | ||
| const environment = { | ||
| PATH: "", | ||
| LOCALAPPDATA: root, | ||
| CODEX_CLI_PATH: "C:\\Program Files\\WindowsApps\\Codex\\codex.exe", | ||
| }; | ||
| expect(resolve(environment, "win32", "x64")).toBe(newer); | ||
| expect( | ||
| resolve( | ||
| { ...environment, CODEX_CLI_PATH: "C:\\Tools\\codex.exe" }, | ||
| "win32", | ||
| "x64", | ||
| ), | ||
| ).toBe("C:\\Tools\\codex.exe"); | ||
| expect(resolve({ PATH: "" }, "win32", "x64")).toBe( | ||
| path.resolve("codex.exe"), | ||
| ); | ||
| expect(resolve({}, "linux", "x64")).toBe(path.resolve("codex")); | ||
| } finally { | ||
| await rm(root, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| test("does not retry Windows executable permission failures", async () => { | ||
| const runtime = await loadBundledRuntime(); | ||
| const source = | ||
| /function classifyCodexWorkerError\([^\n]*\) \{[\s\S]*?\n\}/u.exec( | ||
| runtime, | ||
| )?.[0]; | ||
| expect(source).toBeDefined(); | ||
| class NonRetryableError extends Error {} | ||
| const classify = new Function( | ||
| "DeepScanNonRetryableError", | ||
| `${source}\nreturn classifyCodexWorkerError;`, | ||
| )(NonRetryableError) as (error: Error) => Error; | ||
| const original = Object.assign(new Error("spawn codex EPERM"), { | ||
| code: "EPERM", | ||
| }); | ||
| const result = classify(original); | ||
| expect(result).toBeInstanceOf(NonRetryableError); | ||
| expect(result.cause).toBe(original); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.