@reliantlabs/forge 1.0.12 → 1.0.15

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/README.md CHANGED
@@ -17,12 +17,17 @@ forge version
17
17
  This package is a thin shim. On first run it downloads the prebuilt `forge`
18
18
  binary for your OS and CPU from the matching
19
19
  [GitHub release](https://github.com/reliant-labs/forge/releases), verifies its
20
- sha256 against the release's `checksums.txt`, caches it, and runs it. There is no
20
+ sha256 against the release checksums pinned into this package when it was
21
+ published (pinned only after the release's Sigstore signature verified), caches
22
+ it, and runs it. `FORGE_REQUIRE_SIGNATURE=1` refuses to run from a copy that
23
+ carries no pin. There is no
21
24
  postinstall script, so `--ignore-scripts` is fine. The package version is the forge
22
25
  version.
23
26
 
24
27
  - Cache: `~/.cache/forge/npm/<version>` (Linux), `~/Library/Caches/forge/npm/<version>` (macOS),
25
- `%LOCALAPPDATA%\forge\npm\<version>` (Windows). Override with `FORGE_NPM_CACHE_DIR`.
28
+ `%LOCALAPPDATA%\forge\npm\<version>` (Windows), one subdirectory per pinned
29
+ digest (`sha256-<digest>`; `unverified` for a copy with no pin, which a
30
+ pinned or `FORGE_REQUIRE_SIGNATURE=1` run never uses). Override with `FORGE_NPM_CACHE_DIR`.
26
31
  - Needs Node 18+ and `tar` on `PATH` (present on Linux, macOS and Windows 10+).
27
32
  - Platforms: linux, darwin, windows on amd64 and arm64.
28
33
 
package/bin/forge.js CHANGED
@@ -2,7 +2,8 @@
2
2
  "use strict";
3
3
  // `npx @reliantlabs/forge <args>` runs the forge CLI. The binary matching this
4
4
  // package's version is downloaded from the GitHub release on first use, checked
5
- // against the release's sha256 sums, cached, and executed with stdio inherited.
5
+ // against the signed sha256 sums the package pins (lib/install.js), cached, and
6
+ // executed with stdio inherited.
6
7
 
7
8
  const { spawn } = require("node:child_process");
8
9
  const { version } = require("../package.json");
package/lib/install.js CHANGED
@@ -3,6 +3,19 @@
3
3
  // version. Zero dependencies and no install scripts: the download happens on
4
4
  // first run, so `npm install --ignore-scripts` and `npx` both work.
5
5
  //
6
+ // WHAT THE DOWNLOAD IS CHECKED AGAINST. Not the release's checksums.txt: it
7
+ // comes from the same release as the archive, so whoever can replace one asset
8
+ // can replace both. A published package carries lib/release-checksums.txt
9
+ // instead, written by .github/workflows/release-npm.yml only after the
10
+ // release's Sigstore signature (release-attest.yml) verified, so the archive
11
+ // must match the signed digest, and a release asset swapped after publication
12
+ // cannot reach an npm user. Verifying the signature itself here would need the
13
+ // sigstore package and its dependency tree; the pin needs nothing, and the
14
+ // package that carries it is covered by npm's own provenance attestation.
15
+ //
16
+ // A copy with no pin (a checkout, a hand-packed tarball) falls back to the
17
+ // release's checksums.txt with a warning; FORGE_REQUIRE_SIGNATURE=1 refuses.
18
+ //
6
19
  // Asset names come from scripts/build-release-assets.sh; keep this in step.
7
20
 
8
21
  const crypto = require("node:crypto");
@@ -35,10 +48,44 @@ function platformAsset(platform = process.platform, arch = process.arch) {
35
48
  };
36
49
  }
37
50
 
51
+ // The release checksums release-npm.yml pins into a published package.
52
+ const PINNED_CHECKSUMS = path.join(__dirname, "release-checksums.txt");
53
+
54
+ function readPinnedChecksums(file = PINNED_CHECKSUMS) {
55
+ try {
56
+ return fs.readFileSync(file, "utf8");
57
+ } catch (err) {
58
+ if (err.code === "ENOENT") return null;
59
+ throw err;
60
+ }
61
+ }
62
+
63
+ function requireSignature(env = process.env) {
64
+ const raw = env.FORGE_REQUIRE_SIGNATURE || "";
65
+ const v = raw.toLowerCase();
66
+ if (v === "1" || v === "true" || v === "yes") return true;
67
+ if (v === "" || v === "0" || v === "false" || v === "no") return false;
68
+ throw new Error(`FORGE_REQUIRE_SIGNATURE must be 1 or 0 (got: ${raw})`);
69
+ }
70
+
71
+ // checkTransport allows https, and plain http only to this machine (a local
72
+ // mirror or the tests' fixture server). It is applied to every redirect hop,
73
+ // so a redirect cannot downgrade a download to http.
74
+ function checkTransport(url) {
75
+ const u = new URL(url);
76
+ if (u.protocol === "https:") return;
77
+ const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(u.hostname);
78
+ if (u.protocol === "http:" && loopback) return;
79
+ throw new Error(
80
+ `refusing to download ${u.protocol}//${u.host}${u.pathname}: only https is allowed`,
81
+ );
82
+ }
83
+
38
84
  function releaseUrl(version, env = process.env) {
39
85
  const base = (
40
86
  env.FORGE_RELEASE_BASE_URL || `https://github.com/${REPO}/releases`
41
87
  ).replace(/\/+$/, "");
88
+ checkTransport(base);
42
89
  return `${base}/download/v${version.replace(/^v/, "")}`;
43
90
  }
44
91
 
@@ -62,6 +109,11 @@ function cacheRoot(env = process.env, platform = process.platform) {
62
109
 
63
110
  function download(url, redirects = 5) {
64
111
  return new Promise((resolve, reject) => {
112
+ try {
113
+ checkTransport(url);
114
+ } catch (err) {
115
+ return reject(err);
116
+ }
65
117
  const get = url.startsWith("http:") ? http.get : https.get;
66
118
  get(url, { headers: { "user-agent": "@reliantlabs/forge" } }, (res) => {
67
119
  if (
@@ -99,21 +151,65 @@ function expectedChecksum(checksums, asset) {
99
151
  return null;
100
152
  }
101
153
 
154
+ // cacheDir is where the binary for `version` verified against `want` (the
155
+ // pinned sha256 of its archive) is cached, or, with no pin, the separate
156
+ // directory for binaries checked only against the release's own checksums.txt.
157
+ // Keying verified entries by the signed digest means a binary cached
158
+ // unverified, or verified against another pin, never satisfies a pinned run.
159
+ function cacheDir(version, want, env = process.env) {
160
+ return path.join(
161
+ cacheRoot(env),
162
+ version.replace(/^v/, ""),
163
+ want === null ? "unverified" : `sha256-${want}`,
164
+ );
165
+ }
166
+
167
+ const NO_PIN =
168
+ "this copy of @reliantlabs/forge carries no signed release checksums (lib/release-checksums.txt; packages published to npm do)";
169
+
102
170
  // ensureBinary returns the path of the cached forge binary for `version`,
103
- // downloading and verifying it first when it is not cached yet.
104
- async function ensureBinary(version, env = process.env) {
171
+ // downloading and verifying it first when it is not cached yet. `pinned` is
172
+ // the release checksums the package carries (null when it carries none).
173
+ // The policy (FORGE_REQUIRE_SIGNATURE, and the pin it demands) is checked
174
+ // before the cache is consulted, so a cache hit never bypasses it.
175
+ async function ensureBinary(
176
+ version,
177
+ env = process.env,
178
+ { pinned = readPinnedChecksums() } = {},
179
+ ) {
105
180
  const { asset, binary } = platformAsset();
106
- const dir = path.join(cacheRoot(env), version.replace(/^v/, ""));
181
+ const strict = requireSignature(env);
182
+ if (pinned === null && strict)
183
+ throw new Error(
184
+ `${NO_PIN}, and FORGE_REQUIRE_SIGNATURE=1 refuses an unverified install; nothing was installed`,
185
+ );
186
+ const source =
187
+ pinned === null
188
+ ? "checksums.txt"
189
+ : `the checksums pinned in @reliantlabs/forge@${version.replace(/^v/, "")}`;
190
+ let want = null;
191
+ if (pinned !== null) {
192
+ want = expectedChecksum(pinned, asset);
193
+ if (!want) throw new Error(`${source} has no entry for ${asset}`);
194
+ }
195
+ const dir = cacheDir(version, want, env);
107
196
  const target = path.join(dir, binary);
108
197
  if (fs.existsSync(target)) return target;
109
198
 
110
199
  const url = releaseUrl(version, env);
200
+ if (pinned === null) {
201
+ process.stderr.write(
202
+ `forge: warning: ${NO_PIN}.\n Checking the download against the release's own checksums.txt, which cannot detect a tampered release.\n Set FORGE_REQUIRE_SIGNATURE=1 to refuse an unverified install instead.\n`,
203
+ );
204
+ }
111
205
  process.stderr.write(
112
206
  `forge: downloading ${asset} for v${version.replace(/^v/, "")}\n`,
113
207
  );
114
- let checksums, archive;
208
+ let checksums = pinned;
209
+ let archive;
115
210
  try {
116
- checksums = (await download(`${url}/checksums.txt`)).toString("utf8");
211
+ if (checksums === null)
212
+ checksums = (await download(`${url}/checksums.txt`)).toString("utf8");
117
213
  archive = await download(`${url}/${asset}`);
118
214
  } catch (err) {
119
215
  throw new Error(
@@ -121,12 +217,17 @@ async function ensureBinary(version, env = process.env) {
121
217
  );
122
218
  }
123
219
 
124
- const want = expectedChecksum(checksums, asset);
125
- if (!want) throw new Error(`checksums.txt has no entry for ${asset}`);
220
+ if (want === null) {
221
+ want = expectedChecksum(checksums, asset);
222
+ if (!want) throw new Error(`${source} has no entry for ${asset}`);
223
+ }
126
224
  const got = crypto.createHash("sha256").update(archive).digest("hex");
127
225
  if (got !== want)
128
226
  throw new Error(
129
- `sha256 mismatch for ${asset} (expected ${want}, got ${got}); nothing was installed`,
227
+ `sha256 mismatch for ${asset} (${source} expect ${want}, the download is ${got}); nothing was installed` +
228
+ (pinned === null
229
+ ? ""
230
+ : `.\n The release asset differs from the one that was signed: it may have been tampered with. Report it: https://github.com/${REPO}/security`),
130
231
  );
131
232
 
132
233
  fs.mkdirSync(dir, { recursive: true });
@@ -157,6 +258,9 @@ module.exports = {
157
258
  platformAsset,
158
259
  releaseUrl,
159
260
  cacheRoot,
261
+ cacheDir,
160
262
  expectedChecksum,
263
+ readPinnedChecksums,
264
+ requireSignature,
161
265
  ensureBinary,
162
266
  };
@@ -0,0 +1,6 @@
1
+ cebfae4b0584d69149dc3672732975cfc9a8670c1473c12c82245a25ff579a03 forge_linux_amd64.tar.gz
2
+ f6795b207c3588734cdc2624b343abf7b3c234143446c68c57fff1380ad7488a forge_linux_arm64.tar.gz
3
+ cc35dfc3d5ae537f09091959524a9b9db23005dab760cecdc018ad7be678373f forge_darwin_amd64.tar.gz
4
+ fff0e15eeb7a9b5b182cf13fcab2b0367b41f23aa338802d7c2145145c2fe221 forge_darwin_arm64.tar.gz
5
+ 9774bf006ef871b328e6521b35402eb55264d396ce1f07ae870af3c1e2b1b3e9 forge_windows_amd64.tar.gz
6
+ 3ea58d1d8879b7bd356777109b3b78b032d44de9ad9c7acaafc15dc46b393f0d forge_windows_arm64.tar.gz
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reliantlabs/forge",
3
- "version": "1.0.12",
3
+ "version": "1.0.15",
4
4
  "description": "The forge CLI via npm: `npx @reliantlabs/forge ...`. A thin shim that downloads the matching prebuilt forge binary from the GitHub release and runs it. No Go toolchain, no postinstall script.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {