verify-git-package.mjs 5.0 KB

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