opencode-codex-sandbox 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +50 -0
- package/README.md +112 -2
- package/package.json +39 -4
- package/src/binaries.ts +251 -0
- package/src/bwrap.ts +96 -0
- package/src/desktop.ts +115 -0
- package/src/index.ts +112 -0
- package/src/policy.ts +82 -0
- package/src/sbpl.ts +165 -0
- package/src/seatbelt.ts +74 -0
- package/src/shell.ts +51 -0
- package/src/tar.ts +72 -0
- package/src/windows.ts +110 -0
package/src/shell.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
|
|
3
|
+
export type ShellKind = "pwsh" | "powershell" | "cmd" | "zsh" | "fish" | "posix"
|
|
4
|
+
|
|
5
|
+
export function shellKind(shellPath: string): ShellKind {
|
|
6
|
+
const base = path.basename(shellPath).toLowerCase().replace(/\.exe$/, "")
|
|
7
|
+
if (base === "pwsh") return "pwsh"
|
|
8
|
+
if (base === "powershell") return "powershell"
|
|
9
|
+
if (base === "cmd") return "cmd"
|
|
10
|
+
if (base === "zsh") return "zsh"
|
|
11
|
+
if (base === "fish") return "fish"
|
|
12
|
+
return "posix"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Mirrors opencode's shell argument mapping (packages/core/src/shell/select.ts).
|
|
16
|
+
export function shellArgs(kind: ShellKind, command: string): string[] {
|
|
17
|
+
switch (kind) {
|
|
18
|
+
case "pwsh":
|
|
19
|
+
case "powershell":
|
|
20
|
+
return ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command]
|
|
21
|
+
case "cmd":
|
|
22
|
+
return ["/c", command]
|
|
23
|
+
case "zsh":
|
|
24
|
+
return ["+o", "nomatch", "-c", command]
|
|
25
|
+
default:
|
|
26
|
+
return ["-c", command]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function quoteArg(kind: ShellKind, arg: string): string {
|
|
31
|
+
switch (kind) {
|
|
32
|
+
case "pwsh":
|
|
33
|
+
case "powershell":
|
|
34
|
+
return `'${arg.replaceAll("'", "''")}'`
|
|
35
|
+
case "cmd":
|
|
36
|
+
return `"${arg.replaceAll('"', '\\"')}"`
|
|
37
|
+
case "fish":
|
|
38
|
+
return `'${arg.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}'`
|
|
39
|
+
default:
|
|
40
|
+
return `'${arg.replaceAll("'", `'\\''`)}'`
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function joinCommand(kind: ShellKind, argv: string[]): string {
|
|
45
|
+
const quoted = argv.map((a) => quoteArg(kind, a))
|
|
46
|
+
if ((kind === "pwsh" || kind === "powershell") && quoted.length > 0) {
|
|
47
|
+
// A quoted executable path needs the call operator in PowerShell.
|
|
48
|
+
return `& ${quoted.join(" ")}`
|
|
49
|
+
}
|
|
50
|
+
return quoted.join(" ")
|
|
51
|
+
}
|
package/src/tar.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import zlib from "node:zlib"
|
|
2
|
+
|
|
3
|
+
export interface TarEntry {
|
|
4
|
+
name: string
|
|
5
|
+
type: string
|
|
6
|
+
data: Buffer
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function cstr(buf: Buffer): string {
|
|
10
|
+
const end = buf.indexOf(0)
|
|
11
|
+
return buf.subarray(0, end === -1 ? buf.length : end).toString("utf8")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function octal(buf: Buffer): number {
|
|
15
|
+
const s = cstr(buf).trim()
|
|
16
|
+
return s ? parseInt(s, 8) : 0
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function extractTarGz(buf: Buffer): TarEntry[] {
|
|
20
|
+
const raw = zlib.gunzipSync(buf)
|
|
21
|
+
const entries: TarEntry[] = []
|
|
22
|
+
let offset = 0
|
|
23
|
+
let pendingName: string | undefined
|
|
24
|
+
let pendingPax: Record<string, string> = {}
|
|
25
|
+
while (offset + 512 <= raw.length) {
|
|
26
|
+
const header = raw.subarray(offset, offset + 512)
|
|
27
|
+
if (header.every((b) => b === 0)) break
|
|
28
|
+
const size = octal(header.subarray(124, 136))
|
|
29
|
+
const type = String.fromCharCode(header[156] || 0x30)
|
|
30
|
+
const data = raw.subarray(offset + 512, offset + 512 + size)
|
|
31
|
+
offset += 512 + Math.ceil(size / 512) * 512
|
|
32
|
+
|
|
33
|
+
if (type === "L") {
|
|
34
|
+
pendingName = cstr(data)
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
if (type === "x" || type === "g") {
|
|
38
|
+
const text = data.toString("utf8")
|
|
39
|
+
let pos = 0
|
|
40
|
+
while (pos < text.length) {
|
|
41
|
+
const space = text.indexOf(" ", pos)
|
|
42
|
+
if (space === -1) break
|
|
43
|
+
const len = parseInt(text.slice(pos, space), 10)
|
|
44
|
+
if (!Number.isFinite(len)) break
|
|
45
|
+
const record = text.slice(space + 1, pos + len)
|
|
46
|
+
const eq = record.indexOf("=")
|
|
47
|
+
if (eq !== -1) {
|
|
48
|
+
const key = record.slice(0, eq)
|
|
49
|
+
const value = record.slice(eq + 1).replace(/\n$/, "")
|
|
50
|
+
if (type === "x") pendingPax[key] = value
|
|
51
|
+
}
|
|
52
|
+
pos += len
|
|
53
|
+
}
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let name = pendingName ?? cstr(header.subarray(0, 100))
|
|
58
|
+
const prefix = cstr(header.subarray(345, 500))
|
|
59
|
+
if (prefix && !pendingName) name = `${prefix}/${name}`
|
|
60
|
+
if (pendingPax.path) name = pendingPax.path
|
|
61
|
+
const entrySize = pendingPax.size ? parseInt(pendingPax.size, 10) : size
|
|
62
|
+
pendingName = undefined
|
|
63
|
+
pendingPax = {}
|
|
64
|
+
|
|
65
|
+
if (type === "0" || type === "\0" || type === "7") {
|
|
66
|
+
entries.push({ name, type: "0", data: data.subarray(0, entrySize) })
|
|
67
|
+
} else if (type === "5") {
|
|
68
|
+
entries.push({ name, type: "5", data: Buffer.alloc(0) })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return entries
|
|
72
|
+
}
|
package/src/windows.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Launch/env transport ported from openai/codex (codex-rs/windows-sandbox-rs). Apache-2.0; see NOTICE.
|
|
2
|
+
import type { ResolvedPolicy } from "./policy.ts"
|
|
3
|
+
|
|
4
|
+
export const WINDOWS_SANDBOX_ARG1 = "--run-as-windows-sandbox"
|
|
5
|
+
export const LAUNCH_PAYLOAD_ENV_ARG = "--launch-payload-env"
|
|
6
|
+
const TRANSPORT_PREFIX = "CODEX_SANDBOX_LAUNCH_"
|
|
7
|
+
const CHUNK_BYTES = 16 * 1024
|
|
8
|
+
const MAX_BYTES = 16 * 1024 * 1024
|
|
9
|
+
|
|
10
|
+
interface ProfileEntry {
|
|
11
|
+
path: { type: "special"; value: { kind: string; subpath?: string } } | { type: "path"; path: string }
|
|
12
|
+
access: "read" | "write" | "deny"
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Standard managed profile understood by the legacy restricted-token backend.
|
|
16
|
+
// Special kinds (project_roots/tmpdir/slash_tmp) must be used instead of literal
|
|
17
|
+
// paths so the legacy writable-root projection matches the resolved policy.
|
|
18
|
+
export function buildWindowsProfile(policy: ResolvedPolicy): object {
|
|
19
|
+
const entries: ProfileEntry[] = [
|
|
20
|
+
{ path: { type: "special", value: { kind: "root" } }, access: "read" },
|
|
21
|
+
]
|
|
22
|
+
if (policy.mode === "workspace-write") {
|
|
23
|
+
entries.push(
|
|
24
|
+
{ path: { type: "special", value: { kind: "project_roots" } }, access: "write" },
|
|
25
|
+
{ path: { type: "special", value: { kind: "tmpdir" } }, access: "write" },
|
|
26
|
+
{ path: { type: "special", value: { kind: "slash_tmp" } }, access: "write" },
|
|
27
|
+
)
|
|
28
|
+
for (const name of policy.protectedNames) {
|
|
29
|
+
entries.push({
|
|
30
|
+
path: { type: "special", value: { kind: "project_roots", subpath: name } },
|
|
31
|
+
access: "read",
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
type: "managed",
|
|
37
|
+
file_system: { type: "restricted", entries },
|
|
38
|
+
network: policy.network ? "enabled" : "restricted",
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface WindowsLaunch {
|
|
43
|
+
args: string[]
|
|
44
|
+
env: Record<string, string>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function buildWindowsLaunch(input: {
|
|
48
|
+
policy: ResolvedPolicy
|
|
49
|
+
codexHome: string
|
|
50
|
+
cwd: string
|
|
51
|
+
workspaceRoots: string[]
|
|
52
|
+
desktopName: string
|
|
53
|
+
childEnv: Record<string, string>
|
|
54
|
+
innerArgv: string[]
|
|
55
|
+
}): WindowsLaunch {
|
|
56
|
+
const flags: string[] = [
|
|
57
|
+
"--codex-home", input.codexHome,
|
|
58
|
+
"--command-cwd", input.cwd,
|
|
59
|
+
]
|
|
60
|
+
for (const root of input.workspaceRoots) flags.push("--workspace-root", root)
|
|
61
|
+
flags.push(
|
|
62
|
+
"--permission-profile", JSON.stringify(buildWindowsProfile(input.policy)),
|
|
63
|
+
"--env-json", JSON.stringify(input.childEnv),
|
|
64
|
+
"--windows-sandbox-level", "restricted-token",
|
|
65
|
+
"--windows-sandbox-private-desktop-name", input.desktopName,
|
|
66
|
+
"--",
|
|
67
|
+
...input.innerArgv,
|
|
68
|
+
)
|
|
69
|
+
const payload = JSON.stringify(flags)
|
|
70
|
+
const env = encodeLaunchPayload(payload)
|
|
71
|
+
return { args: [WINDOWS_SANDBOX_ARG1, LAUNCH_PAYLOAD_ENV_ARG], env }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Mirrors codex windows-sandbox-rs/src/environment_transport.rs: UTF-8 chunks
|
|
75
|
+
// of at most 16 KiB on char boundaries, plus COUNT/BYTES metadata variables.
|
|
76
|
+
export function encodeLaunchPayload(payload: string): Record<string, string> {
|
|
77
|
+
const bytes = Buffer.byteLength(payload, "utf8")
|
|
78
|
+
if (bytes === 0 || bytes > MAX_BYTES) {
|
|
79
|
+
throw new Error(`sandbox launch payload must contain 1..=${MAX_BYTES} bytes`)
|
|
80
|
+
}
|
|
81
|
+
if (payload.includes("\0")) throw new Error("sandbox launch payload contains a NUL")
|
|
82
|
+
const chunks: string[] = []
|
|
83
|
+
let current = ""
|
|
84
|
+
let currentBytes = 0
|
|
85
|
+
for (const ch of payload) {
|
|
86
|
+
const chBytes = Buffer.byteLength(ch, "utf8")
|
|
87
|
+
if (currentBytes + chBytes > CHUNK_BYTES) {
|
|
88
|
+
chunks.push(current)
|
|
89
|
+
current = ""
|
|
90
|
+
currentBytes = 0
|
|
91
|
+
}
|
|
92
|
+
current += ch
|
|
93
|
+
currentBytes += chBytes
|
|
94
|
+
}
|
|
95
|
+
if (current) chunks.push(current)
|
|
96
|
+
const env: Record<string, string> = {
|
|
97
|
+
[`${TRANSPORT_PREFIX}COUNT`]: String(chunks.length),
|
|
98
|
+
[`${TRANSPORT_PREFIX}BYTES`]: String(bytes),
|
|
99
|
+
}
|
|
100
|
+
chunks.forEach((chunk, i) => {
|
|
101
|
+
env[`${TRANSPORT_PREFIX}${i}`] = chunk
|
|
102
|
+
})
|
|
103
|
+
return env
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function scrubTransportEnv(env: Record<string, string | undefined>): void {
|
|
107
|
+
for (const key of Object.keys(env)) {
|
|
108
|
+
if (key.toUpperCase().startsWith(TRANSPORT_PREFIX)) delete env[key]
|
|
109
|
+
}
|
|
110
|
+
}
|