-
Notifications
You must be signed in to change notification settings - Fork 753
refactor(plugin): port security policy resolution to TypeScript #799
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
kmbroai
wants to merge
7
commits into
dev/kyleb/python-free-windows-wide-paths
from
dev/kyleb/python-free-policy-resolver
+2,020
−588
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8c64f9
Port security policy helper with native path support
kmbroai 27ed2a8
fix(plugin): preserve Windows policy path components
kmbroai acd3079
test(plugin): make policy fixtures portable across hosts
kmbroai 985edea
fix(plugin): clarify policy helper compatibility and invocation
kmbroai a922ec8
test(native): record platform-specific file-parent resolution
kmbroai e5e2a7a
Preserve policy helper malformed help errors
kmbroai 9986e31
test(plugin): remove completed runtime characterization
kmbroai 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
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
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,34 @@ | ||
| import { resolveSecurityMdCommand } from "./src/helpers/resolve-security-md"; | ||
| import { decodePosixBytes } from "./src/helpers/posix-path"; | ||
| import { windowsBinding } from "./src/native"; | ||
|
|
||
| let commandLine = process.argv.slice(2); | ||
| if (process.platform === "win32") { | ||
| const original = windowsBinding().windowsArguments(); | ||
| commandLine = original | ||
| .slice(original.length - commandLine.length) | ||
| .map((argument) => argument.toString("utf16le")); | ||
| } | ||
| let posixHome = process.env.HOME; | ||
| if (commandLine[0] === "--helper") { | ||
| if (process.platform === "win32") { | ||
| commandLine = commandLine.slice(1); | ||
| } else { | ||
| const [homeSet, home, ...args] = decodePosixBytes( | ||
| Buffer.from(commandLine[1] ?? "", "hex"), | ||
| ) | ||
| .split("\0") | ||
| .slice(0, -1); | ||
| posixHome = homeSet ? home : undefined; | ||
| commandLine = args; | ||
| } | ||
| } | ||
| const [command, ...args] = commandLine; | ||
| if (command === "resolve-security-md") { | ||
| process.exitCode = resolveSecurityMdCommand(args, posixHome); | ||
| } else { | ||
| console.error( | ||
| "Usage: launch_codex_security_mcp[.cmd] --helper resolve-security-md [options]", | ||
| ); | ||
| process.exitCode = 2; | ||
| } |
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
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,88 @@ | ||
| const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); | ||
|
|
||
| export function decodePosixBytes(bytes: Buffer): string { | ||
| try { | ||
| return utf8.decode(bytes); | ||
| } catch { | ||
| // Match Python's surrogateescape for undecodable POSIX path bytes. | ||
| let value = ""; | ||
| for (let offset = 0; offset < bytes.length; ) { | ||
| let decoded = false; | ||
| for (let size = 1; size <= 4 && offset + size <= bytes.length; size++) { | ||
| try { | ||
| value += utf8.decode(bytes.subarray(offset, offset + size)); | ||
| offset += size; | ||
| decoded = true; | ||
| break; | ||
| } catch { | ||
| // A UTF-8 character can occupy up to four bytes. | ||
| } | ||
| } | ||
| if (!decoded) value += String.fromCharCode(0xdc00 + bytes[offset++]!); | ||
| } | ||
| return value; | ||
| } | ||
| } | ||
|
|
||
| export function encodePosixPath(value: string): Buffer { | ||
| return Buffer.concat( | ||
| value | ||
| .split(/([\udc80-\udcff])/u) | ||
| .map((part) => | ||
| /^[\udc80-\udcff]$/u.test(part) | ||
| ? Buffer.from([part.charCodeAt(0) - 0xdc00]) | ||
| : Buffer.from(part), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| export class SymlinkLoopError extends Error {} | ||
|
|
||
| export function resolvePosixPath(value: Buffer): Buffer { | ||
| // GNU Linux native realpath rejects file/.. and links targeting it with | ||
| // ENOTDIR. Retain the shipped pathlib contract for those inputs. | ||
| const seen = new Map<string, string | null>(); | ||
| // Latin-1 is a lossless internal representation of pathname bytes. | ||
| function follow(directory: string, path: string): string { | ||
| if (path.startsWith("/")) directory = "/"; | ||
| for (const name of path.split("/")) { | ||
| if (name === "" || name === ".") continue; | ||
| if (name === "..") { | ||
| directory = directory.slice(0, directory.lastIndexOf("/")) || "/"; | ||
| continue; | ||
| } | ||
| const candidate = `${directory === "/" ? "" : directory}/${name}`; | ||
| const bytes = Buffer.from(candidate, "latin1"); | ||
| if (!lstatSync(bytes).isSymbolicLink()) { | ||
| directory = candidate; | ||
| continue; | ||
| } | ||
| const cached = seen.get(candidate); | ||
| if (cached === null) { | ||
| throw new SymlinkLoopError( | ||
| `Symlink loop from ${decodePosixBytes(bytes)}`, | ||
| ); | ||
| } | ||
| if (cached !== undefined) { | ||
| directory = cached; | ||
| continue; | ||
| } | ||
| seen.set(candidate, null); | ||
| directory = follow( | ||
| directory, | ||
| readlinkSync(bytes, { encoding: "buffer" }).toString("latin1"), | ||
| ); | ||
| seen.set(candidate, directory); | ||
| } | ||
| return directory; | ||
| } | ||
| const cwd = | ||
| value[0] === 0x2f | ||
| ? Buffer.from("/") | ||
| : realpathSync.native(".", { encoding: "buffer" }); | ||
| return Buffer.from( | ||
| follow(cwd.toString("latin1"), value.toString("latin1")), | ||
| "latin1", | ||
| ); | ||
| } | ||
| import { lstatSync, readlinkSync, realpathSync } from "node:fs"; | ||
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.