Files
codex-security/plugins/codex-security/native/check.mts
T
mldangelo-oaiandCodex 7eba900035 fix(build): keep usable bundles after failures and preserve release labels (#1263)
* fix(build): preserve artifacts and align CI and release checks

* Preserve platform compatibility and prevent stale container promotion retries

* test(build): compile stdin import helpers in a temporary fixture

* test: distinguish Markdown workflow fixture identities

* fix(ci): restore changed Markdown formatting checks

* test(ci): follow renamed Markdown formatting step

* fix(build): allow maintainer imports from virtual entrypoints

* fix(build): resolve both standalone entrypoint paths

* Keep maintainer eval imports inert with matching positional paths

* fix(build): keep combined evaluation imports inactive

* test(build): preserve portable preload paths across runtimes

* Preserve manual release categories after pull request retitling

* fix: reconcile automatic labels after manual release overrides

* test: batch workflow condition evaluations

* Disambiguate manual release category test identities

* Use asynchronous Bash workflow fixture and preserve process diagnostics

* Pass workflow condition scripts through existing stdin helper

---------

Co-authored-by: Codex <codex@users.noreply.github.com>
2026-10-07 22:09:59 -04:00

161 lines
4.8 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { readFileSync, realpathSync } from "node:fs";
import { binaryPath } from "./binding.mjs";
import { libc } from "./platform.mjs";
export function checkPrivatePaths(
bytes: Buffer,
buildPaths: string[] = [],
): void {
for (const marker of [
"/Users/",
"/home/dev-user",
"/tmp/codex-security-python-",
...buildPaths,
]) {
if (
bytes.includes(Buffer.from(marker)) ||
bytes.includes(Buffer.from(marker, "utf16le"))
) {
throw new Error("Native payload contains a private build path.");
}
}
}
function versionAfter(value: string, floor: string): boolean {
const actual = value.split(".").map(Number);
const maximum = floor.split(".").map(Number);
for (
let index = 0;
index < Math.max(actual.length, maximum.length);
index++
) {
const difference = (actual[index] ?? 0) - (maximum[index] ?? 0);
if (difference !== 0) return difference > 0;
}
return false;
}
function isMain() {
if (
process.execArgv.some(
(argument) =>
/^(?:--(?:eval|print)(?:=|$)|-(?:e|p|pe)$)/u.test(argument) ||
(process.versions["bun"] !== undefined && /^-[ep]/u.test(argument)),
)
)
return false;
try {
return (
process.argv[1] !== undefined &&
process.argv[1] !== "-" &&
realpathSync(process.argv[1]) === realpathSync(new URL(import.meta.url))
);
} catch {
return false;
}
}
if (isMain()) {
const bytes = readFileSync(binaryPath);
checkPrivatePaths(bytes);
let floor: string;
if (process.platform === "linux") {
const versions = execFileSync("readelf", ["--version-info", binaryPath], {
encoding: "utf8",
});
const required = [...versions.matchAll(/\bGLIBC_(\d+(?:\.\d+)*)/gu)].map(
(match) => match[1]!,
);
if (libc === "musl") {
const architecture =
process.arch === "x64"
? { machine: 62, name: "x86_64" }
: process.arch === "arm64"
? { machine: 183, name: "aarch64" }
: undefined;
const dynamic = execFileSync("readelf", ["--dynamic", binaryPath], {
encoding: "utf8",
});
const dependencies = [
...dynamic.matchAll(/\(NEEDED\)[^\n]*\[([^\]]+)\]/gu),
].map((match) => match[1]!);
// libgcc uses GLIBC_2.0 for its own compatibility exports on aarch64.
const requiresGlibc = [
...versions.matchAll(/\bFile:\s+(\S+)([\s\S]*?)(?=\bFile:|$)/gu),
].some(
(match) =>
match[1] !== "libgcc_s.so.1" && /\bName:\s+GLIBC_/u.test(match[2]!),
);
if (
architecture === undefined ||
bytes.toString("latin1", 0, 4) !== "\x7fELF" ||
bytes[4] !== 2 ||
bytes[5] !== 1 ||
bytes.readUInt16LE(18) !== architecture.machine ||
!dependencies.includes(`libc.musl-${architecture.name}.so.1`) ||
requiresGlibc
) {
throw new Error(
"Native payload is not a musl ELF image for this architecture, or imports glibc.",
);
}
floor = "musl; Node 20 and 22 load proofs required";
} else {
if (
required.length === 0 ||
required.some((version) => versionAfter(version, "2.28"))
) {
throw new Error(
"Native payload requires glibc newer than 2.28, or has no inspectable glibc requirements.",
);
}
floor = "glibc 2.28";
}
} else if (process.platform === "darwin") {
const commands = execFileSync("otool", ["-l", binaryPath], {
encoding: "utf8",
});
const minimum =
/cmd LC_BUILD_VERSION\s+[\s\S]*?\bminos ([\d.]+)/u.exec(commands)?.[1] ??
/cmd LC_VERSION_MIN_MACOSX\s+[\s\S]*?\bversion ([\d.]+)/u.exec(
commands,
)?.[1];
if (minimum === undefined || versionAfter(minimum, "11.0")) {
throw new Error(
"Native payload does not declare a compatible macOS 11.0 deployment target.",
);
}
floor = "macOS 11.0";
} else if (process.platform === "win32") {
const header = bytes.readUInt32LE(0x3c);
const machine = process.arch === "arm64" ? 0xaa64 : 0x8664;
if (
bytes.toString("ascii", 0, 2) !== "MZ" ||
bytes.toString("ascii", header, header + 4) !== "PE\0\0" ||
bytes.readUInt16LE(header + 4) !== machine
) {
throw new Error(
"Native payload is not a PE image for this architecture.",
);
}
floor = "Windows MSVC; Node 20 and 22 load proofs required";
} else {
throw new Error(
"This foundation verifies Linux, macOS, and Windows artifacts only.",
);
}
console.log(
JSON.stringify({
platform: process.platform,
arch: process.arch,
libc,
nodeApi: 8,
floor,
bytes: bytes.length,
sha256: createHash("sha256").update(bytes).digest("hex"),
}),
);
}