verify-git-package.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env node
  2. import assert from "node:assert/strict";
  3. import { execFile } from "node:child_process";
  4. import { constants } from "node:fs";
  5. import { access, lstat, mkdtemp, readFile, realpath, rm } from "node:fs/promises";
  6. import { tmpdir } from "node:os";
  7. import { isAbsolute, join, relative } from "node:path";
  8. import { promisify } from "node:util";
  9. const execFileAsync = promisify(execFile);
  10. const PACKAGE_NAME = "codex-app-server-bridge";
  11. const DEFAULT_CACHE = "/private/tmp/codex-app-server-bridge-npm-cache";
  12. const USAGE = "usage: npm run verify:git-package -- <git+https|git+ssh|git+file URL>#<vX.Y.Z|40-character-commit>";
  13. function selectedGitPackage(argv) {
  14. if (argv.length !== 1) throw new Error(USAGE);
  15. const packageSpec = argv[0];
  16. const fragmentAt = packageSpec.lastIndexOf("#");
  17. const repository = packageSpec.slice(0, fragmentAt);
  18. const revision = packageSpec.slice(fragmentAt + 1);
  19. const supportedRepository = /^(?:git\+https|git\+ssh|git\+file):\/\/.+\.git$/.test(repository);
  20. const immutableRevision = /^(?:[0-9a-fA-F]{40}|v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.test(revision);
  21. if (fragmentAt <= 0 || !supportedRepository || !immutableRevision) {
  22. throw new Error("Git package must use a supported URL pinned to a full commit or release tag");
  23. }
  24. return { packageSpec, revision };
  25. }
  26. function isWithin(parent, child) {
  27. const pathFromParent = relative(parent, child);
  28. return pathFromParent === "" || (!pathFromParent.startsWith("..") && !isAbsolute(pathFromParent));
  29. }
  30. function commandExit(error) {
  31. return typeof error === "object" && error !== null && "code" in error
  32. ? String(error.code)
  33. : "unknown";
  34. }
  35. async function installPackage(prefix, cache, packageSpec) {
  36. try {
  37. await execFileAsync("npm", [
  38. "install",
  39. "--global",
  40. "--prefix",
  41. prefix,
  42. "--ignore-scripts",
  43. "--cache",
  44. cache,
  45. packageSpec,
  46. ], { maxBuffer: 10 * 1024 * 1024 });
  47. } catch (error) {
  48. throw new Error(`isolated npm installation failed with exit code ${commandExit(error)}`);
  49. }
  50. }
  51. async function installedPackage(prefix, cache) {
  52. const packageRoot = join(prefix, "lib", "node_modules", PACKAGE_NAME);
  53. const executable = join(prefix, "bin", PACKAGE_NAME);
  54. await Promise.all([
  55. access(executable, constants.X_OK),
  56. access(join(packageRoot, "dist", "cli.js"), constants.R_OK),
  57. access(join(packageRoot, "dist", "config.js"), constants.R_OK),
  58. ]);
  59. const packageMetadata = await lstat(packageRoot);
  60. if (packageMetadata.isSymbolicLink()) {
  61. throw new Error("installed package must not be a temporary link");
  62. }
  63. const [realPrefix, realCache, realPackage, realExecutable] = await Promise.all([
  64. realpath(prefix),
  65. realpath(cache),
  66. realpath(packageRoot),
  67. realpath(executable),
  68. ]);
  69. if (!isWithin(realPrefix, realPackage) || isWithin(realCache, realPackage)) {
  70. throw new Error("installed package resolved outside the isolated prefix");
  71. }
  72. if (!isWithin(realPackage, realExecutable) || isWithin(realCache, realExecutable)) {
  73. throw new Error("installed executable resolved outside the installed package");
  74. }
  75. const manifest = JSON.parse(await readFile(join(realPackage, "package.json"), "utf8"));
  76. assert.equal(manifest.name, PACKAGE_NAME);
  77. assert.deepEqual(manifest.bin, { [PACKAGE_NAME]: `bin/${PACKAGE_NAME}.js` });
  78. return { executable, version: String(manifest.version) };
  79. }
  80. async function verifyExecutable(executable) {
  81. try {
  82. await execFileAsync(executable, ["--unsupported"], {
  83. env: process.env,
  84. maxBuffer: 1024 * 1024,
  85. });
  86. throw new Error("installed executable unexpectedly accepted an unsupported invocation");
  87. } catch (error) {
  88. if (typeof error !== "object" || error === null || !("code" in error) || error.code !== 1) {
  89. throw error;
  90. }
  91. const stdout = "stdout" in error ? String(error.stdout) : "";
  92. const stderr = "stderr" in error ? String(error.stderr) : "";
  93. assert.equal(stdout, "");
  94. assert.match(stderr, /Usage: codex-app-server-bridge app-server/);
  95. }
  96. }
  97. async function verify() {
  98. const { packageSpec, revision } = selectedGitPackage(process.argv.slice(2));
  99. const prefix = await mkdtemp(join(tmpdir(), "codex-app-server-bridge-git-package-"));
  100. const cache = process.env.CODEX_APP_SERVER_BRIDGE_NPM_CACHE ?? DEFAULT_CACHE;
  101. try {
  102. await installPackage(prefix, cache, packageSpec);
  103. const installed = await installedPackage(prefix, cache);
  104. await verifyExecutable(installed.executable);
  105. process.stdout.write(`${JSON.stringify({
  106. ok: true,
  107. package: `${PACKAGE_NAME}@${installed.version}`,
  108. revision,
  109. executable: `bin/${PACKAGE_NAME}`,
  110. storage: "isolated-prefix",
  111. invocation: "verified",
  112. })}\n`);
  113. } finally {
  114. await rm(prefix, { recursive: true, force: true });
  115. }
  116. }
  117. verify().catch((error) => {
  118. const message = error instanceof Error ? error.message : String(error);
  119. process.stderr.write(`Git-package verification failed: ${message}\n`);
  120. process.exitCode = 1;
  121. });