|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +/** |
| 4 | + * CI preflight that runs @kilocode/shell-security through the REAL |
| 5 | + * OpenClaw install path — the same code path an end user hits when |
| 6 | + * they type `openclaw plugins install @kilocode/shell-security`. |
| 7 | + * |
| 8 | + * Steps: |
| 9 | + * 1. Pack this plugin into a tarball (strips `private: true` the |
| 10 | + * same way script/publish.ts does, then restores it). |
| 11 | + * 2. Install `openclaw@latest` into a throwaway node_modules. |
| 12 | + * 3. Run `openclaw plugins install <tarball>` against a throwaway |
| 13 | + * OPENCLAW_STATE_DIR. |
| 14 | + * 4. Exit with whatever exit code `openclaw plugins install` gave |
| 15 | + * us. Non-zero = scanner/denylist rejection = CI fails. |
| 16 | + * |
| 17 | + * NO bypass flags. Never add `--dangerously-force-unsafe-install`, |
| 18 | + * `--force`, `--skip-scan`, or any other "ignore the safety gate" |
| 19 | + * option to this script. The whole point is to exercise the real |
| 20 | + * gate. If the gate rejects the plugin, the fix is in the plugin |
| 21 | + * source (see src/env.ts for the project's env-isolation convention) |
| 22 | + * — never in this script. |
| 23 | + * |
| 24 | + * `OPENCLAW_DISABLE_BUNDLED_PLUGIN_POSTINSTALL=1` below is NOT a |
| 25 | + * bypass flag. It skips openclaw's postinstall step that sets up |
| 26 | + * compat sidecars for bundled channel plugins (Telegram, Slack, |
| 27 | + * Matrix, etc.) used by the gateway itself — none of which is |
| 28 | + * relevant to `plugins install`. The scanner code path this script |
| 29 | + * exercises is unaffected. Removing it just makes CI slower. |
| 30 | + * |
| 31 | + * Run locally: `bun run install-preflight` |
| 32 | + * CI: `.github/workflows/install-preflight.yml` |
| 33 | + */ |
| 34 | + |
| 35 | +import { $ } from "bun"; |
| 36 | +import fs from "node:fs/promises"; |
| 37 | +import os from "node:os"; |
| 38 | +import path from "node:path"; |
| 39 | +import { fileURLToPath } from "node:url"; |
| 40 | + |
| 41 | +const repoRoot = fileURLToPath(new URL("..", import.meta.url)); |
| 42 | +process.chdir(repoRoot); |
| 43 | + |
| 44 | +const pkgPath = path.join(repoRoot, "package.json"); |
| 45 | +const originalPkgText = await fs.readFile(pkgPath, "utf8"); |
| 46 | + |
| 47 | +// The finally block restores package.json and cleans up artifacts. It |
| 48 | +// is critical that ANY mutation of pkgPath happens inside the try so |
| 49 | +// the restore is always armed, and that we NEVER call `process.exit()` |
| 50 | +// inside the try — that would terminate the process immediately and |
| 51 | +// skip finally, leaving `private: true` stripped permanently (which |
| 52 | +// is exactly the publish-safety regression this script must not cause). |
| 53 | +// Instead we set `process.exitCode` at the bottom and let the event |
| 54 | +// loop drain normally. |
| 55 | +let tarball: string | undefined; |
| 56 | +let ciTmp: string | undefined; |
| 57 | +let openclawExit = 0; |
| 58 | +try { |
| 59 | + // --- Pack -------------------------------------------------------- |
| 60 | + const pkg = JSON.parse(originalPkgText); |
| 61 | + delete pkg.private; |
| 62 | + await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + "\n"); |
| 63 | + |
| 64 | + // `bun pm pack --quiet` emits only the tarball filename. |
| 65 | + tarball = (await $`bun pm pack --quiet`.text()).trim(); |
| 66 | + if (!tarball) throw new Error("bun pm pack produced no tarball"); |
| 67 | + console.log(`Packed: ${tarball}`); |
| 68 | + |
| 69 | + // --- Install openclaw -------------------------------------------- |
| 70 | + ciTmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-preflight-")); |
| 71 | + const stateDir = path.join(ciTmp, "state"); |
| 72 | + const nodeRoot = path.join(ciTmp, "node"); |
| 73 | + await fs.mkdir(stateDir, { recursive: true }); |
| 74 | + await fs.mkdir(nodeRoot, { recursive: true }); |
| 75 | + |
| 76 | + const latest = (await $`npm view openclaw dist-tags.latest`.text()).trim(); |
| 77 | + console.log(`Resolved openclaw@${latest}`); |
| 78 | + |
| 79 | + // Minimal scratch package so npm has somewhere to land node_modules. |
| 80 | + await fs.writeFile( |
| 81 | + path.join(nodeRoot, "package.json"), |
| 82 | + JSON.stringify( |
| 83 | + { name: "openclaw-preflight-scratch", private: true }, |
| 84 | + null, |
| 85 | + 2, |
| 86 | + ) + "\n", |
| 87 | + ); |
| 88 | + |
| 89 | + process.env.OPENCLAW_DISABLE_BUNDLED_PLUGIN_POSTINSTALL = "1"; |
| 90 | + await $`npm install --prefix ${nodeRoot} --no-save openclaw@${latest}`; |
| 91 | + |
| 92 | + // --- Run the real install ---------------------------------------- |
| 93 | + process.env.OPENCLAW_STATE_DIR = stateDir; |
| 94 | + const tarballAbs = path.resolve(repoRoot, tarball); |
| 95 | + const cli = path.join(nodeRoot, "node_modules", ".bin", "openclaw"); |
| 96 | + console.log(`Running: ${cli} plugins install ${tarballAbs}`); |
| 97 | + const result = await $`${cli} plugins install ${tarballAbs}`.nothrow(); |
| 98 | + |
| 99 | + if (result.exitCode !== 0) { |
| 100 | + console.error( |
| 101 | + `\nFAIL: openclaw plugins install exited ${result.exitCode}. See output above.`, |
| 102 | + ); |
| 103 | + console.error( |
| 104 | + "If this is an env-harvesting or other scanner rejection, the fix is in the plugin source (see src/env.ts for the project's env-isolation convention). Do NOT add a bypass flag to this script.", |
| 105 | + ); |
| 106 | + openclawExit = result.exitCode; |
| 107 | + } else { |
| 108 | + console.log("\nOK: openclaw plugins install succeeded"); |
| 109 | + } |
| 110 | +} finally { |
| 111 | + // Always restore package.json (restores `private: true`), always |
| 112 | + // clean up the tarball, always clean up the scratch openclaw install |
| 113 | + // dir so repeated local runs don't accumulate ~400MB of node_modules |
| 114 | + // under os.tmpdir(). |
| 115 | + await fs.writeFile(pkgPath, originalPkgText); |
| 116 | + if (tarball) { |
| 117 | + await $`rm -f ${tarball}`.nothrow(); |
| 118 | + } |
| 119 | + if (ciTmp) { |
| 120 | + await $`rm -rf ${ciTmp}`.nothrow(); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +process.exitCode = openclawExit; |
0 commit comments