Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(release): reject a version that is not a release version

Etienne Lescotbd5e71ad0284
diff --git a/.github/scripts/set-release-version.mjs b/.github/scripts/set-release-version.mjs
index 24f6d2315..1baf9ca4c 100644
--- a/.github/scripts/set-release-version.mjs
+++ b/.github/scripts/set-release-version.mjs
@@ -26,8 +26,23 @@ import { argv } from "node:process";
* @param {string} version Version to write, e.g. "1.9.0" or "1.9.0-rc.2".
* @param {string} dir Directory holding package.json and package-lock.json.
*/
+// MAJOR.MINOR.PATCH with the optional prerelease suffix promote.yml and
+// prerelease.yml actually produce ("1.9.0", "2.0.0-rc.3"). Deliberately not full
+// semver: this is a gate on what may be written into a published manifest, and
+// build metadata or a leading "v" would be a caller bug, not a version to honour.
+const RELEASE_VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
+
export function setReleaseVersion(version, dir) {
if (!version) throw new Error("a version is required");
+ // Truthiness alone let 123, " " and "not-a-version" through into both
+ // manifests. The callers compute this from a validated tag, so it is a
+ // defence-in-depth check — but a script whose whole purpose is to stop bad
+ // version metadata should not be the thing that writes it.
+ if (typeof version !== "string" || !RELEASE_VERSION.test(version)) {
+ throw new Error(
+ `invalid version ${JSON.stringify(version)}; expected MAJOR.MINOR.PATCH with an optional prerelease suffix`,
+ );
+ }
// Both manifests are read and validated before a single byte is written. The
// obvious order — write package.json, then validate the lockfile — left
diff --git a/.github/scripts/set-release-version.test.mjs b/.github/scripts/set-release-version.test.mjs
index 5e7a15619..77d69e64e 100644
--- a/.github/scripts/set-release-version.test.mjs
+++ b/.github/scripts/set-release-version.test.mjs
@@ -120,6 +120,19 @@ describe("setReleaseVersion", () => {
expect(() => setReleaseVersion("", dir)).toThrow(/version is required/);
});
+ // Truthiness alone let all of these reach both manifests. A number in
+ // particular writes `"version": 123`, which is not even a legal package.json.
+ it.each([
+ [123, "a number"],
+ [" ", "whitespace"],
+ ["not-a-version", "a malformed version"],
+ ["v1.9.0", "a leading v"],
+ ])("rejects %o (%s)", (bad) => {
+ expect(() => setReleaseVersion(bad, dir)).toThrow(/invalid version/);
+ expect(JSON.parse(read("package.json")).version).toBe("1.8.0");
+ expect(read("package-lock.json")).not.toContain("1.9.0");
+ });
+
// The workflows invoke this with a repository-relative path, and the
// direct-invocation guard compares against `import.meta.filename`, which is
// absolute. Node resolves argv[1] before exposing it, so the two match — but