Enforce forward-only updates with downgrade protection and latest keyword normalization
Binaries / Build & Release Binaries (push) Has been cancelled
Binaries / Build & Release Binaries (push) Has been cancelled
This commit is contained in:
1 parent
63d579c273
commit
5dd337e671
3 files changed
+72
-14
No files matched your search
@@ -185,6 +185,24 @@ describe("CLI version & help handling", () => {
|
||||
expect(parsed.currentVersion).toBe(VERSION);
|
||||
expect(Array.isArray(parsed.releases)).toBe(true);
|
||||
});
|
||||
|
||||
test("rejects downgrade to older version when target version is lower", () => {
|
||||
const result = runCli(["update", "0.3.3"]);
|
||||
expect(result.status).toBe(0);
|
||||
const parsed = JSON.parse(result.stdout.trim());
|
||||
expect(parsed.updated).toBe(false);
|
||||
expect(parsed.currentVersion).toBe(VERSION);
|
||||
expect(parsed.targetVersion).toBe("0.3.3");
|
||||
expect(parsed.message).toContain("Cannot downgrade seal-home");
|
||||
});
|
||||
|
||||
test("treats latest keyword as latest release without downgrade error", () => {
|
||||
const result = runCli(["update", "latest", "--check"]);
|
||||
expect(result.status).toBe(0);
|
||||
const parsed = JSON.parse(result.stdout.trim());
|
||||
expect(parsed).toHaveProperty("latestVersion");
|
||||
expect(parsed.updated).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
function makeConfigEnv(): Record<string, string> {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { isNewerVersion } from "./update.js";
|
||||
import { compareSemVer, isNewerVersion } from "./update.js";
|
||||
|
||||
describe("update version comparator", () => {
|
||||
test("correctly identifies newer semantic versions", () => {
|
||||
@@ -10,4 +10,12 @@ describe("update version comparator", () => {
|
||||
expect(isNewerVersion("0.5.9", "0.6.0")).toBe(false);
|
||||
expect(isNewerVersion("0.6.0", "0.7.0")).toBe(false);
|
||||
});
|
||||
|
||||
test("compareSemVer handles tags, major, minor, and patch", () => {
|
||||
expect(compareSemVer("v0.7.1", "0.7.0")).toBe(1);
|
||||
expect(compareSemVer("0.7.0", "v0.7.1")).toBe(-1);
|
||||
expect(compareSemVer("v0.7.1", "v0.7.1")).toBe(0);
|
||||
expect(compareSemVer("1.0.0", "0.9.9")).toBe(1);
|
||||
expect(compareSemVer("0.3.3", "0.7.1")).toBe(-1);
|
||||
});
|
||||
});
|
||||
+45
-13
@@ -32,6 +32,34 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
};
|
||||
|
||||
const repo = options.repo || process.env[GITHUB_REPO_ENV]?.trim() || DEFAULT_GITHUB_REPO;
|
||||
const rawTarget = options.targetVersion?.trim();
|
||||
const targetVersion = rawTarget && rawTarget.toLowerCase() !== "latest" ? rawTarget : undefined;
|
||||
|
||||
// Validate targetVersion: prevent downgrade to older versions
|
||||
if (targetVersion) {
|
||||
const cleanTarget = targetVersion.replace(/^v/, "");
|
||||
const cmp = compareSemVer(cleanTarget, VERSION);
|
||||
if (cmp < 0) {
|
||||
printJson({
|
||||
updated: false,
|
||||
currentVersion: VERSION,
|
||||
targetVersion: cleanTarget,
|
||||
mode: isCompiledBinary() ? "binary" : "source",
|
||||
message: `Cannot downgrade seal-home from v${VERSION} to older version v${cleanTarget}. Updates must be forward-only.`
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (cmp === 0 && !options.force) {
|
||||
printJson({
|
||||
updated: false,
|
||||
currentVersion: VERSION,
|
||||
targetVersion: cleanTarget,
|
||||
mode: isCompiledBinary() ? "binary" : "source",
|
||||
message: `seal-home is already at version v${VERSION}. Use --force to reinstall.`
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Changelog / Release History query
|
||||
if (options.changelogOnly) {
|
||||
@@ -113,9 +141,9 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
const arch = detectArch();
|
||||
|
||||
const releases = await fetchGitHubReleases(repo, 30);
|
||||
const release = options.targetVersion
|
||||
? (releases.find((r) => r.version === options.targetVersion || r.tagName === options.targetVersion) ??
|
||||
await fetchGitHubRelease(repo, options.targetVersion))
|
||||
const release = targetVersion
|
||||
? (releases.find((r) => r.version === targetVersion || r.tagName === targetVersion || r.tagName === `v${targetVersion}`) ??
|
||||
await fetchGitHubRelease(repo, targetVersion))
|
||||
: (releases[0] ?? await fetchGitHubRelease(repo));
|
||||
|
||||
if (!release) {
|
||||
@@ -131,7 +159,7 @@ export async function handleUpdateCommand(runtime: CliRuntime, options: UpdateOp
|
||||
const latestVersion = release.version;
|
||||
const currentRelease = releases.find((r) => r.version === VERSION);
|
||||
const isUpgrade = isNewerVersion(latestVersion, VERSION);
|
||||
const shouldUpdate = options.force || Boolean(options.targetVersion) || isUpgrade;
|
||||
const shouldUpdate = isUpgrade || Boolean(options.force);
|
||||
const pendingReleases = releases.filter((r) => isNewerVersion(r.version, VERSION));
|
||||
|
||||
if (options.checkOnly) {
|
||||
@@ -464,16 +492,20 @@ function extractZip(archivePath: string, targetDir: string, platform: string): s
|
||||
return files[0] || archivePath;
|
||||
}
|
||||
|
||||
export function isNewerVersion(latest: string, current: string): boolean {
|
||||
const lParts = latest.split(".").map(Number);
|
||||
const cParts = current.split(".").map(Number);
|
||||
for (let i = 0; i < Math.max(lParts.length, cParts.length); i++) {
|
||||
const l = lParts[i] ?? 0;
|
||||
const c = cParts[i] ?? 0;
|
||||
if (l > c) return true;
|
||||
if (l < c) return false;
|
||||
export function compareSemVer(v1: string, v2: string): number {
|
||||
const p1 = v1.replace(/^v/, "").split(".").map((n) => Number.parseInt(n, 10) || 0);
|
||||
const p2 = v2.replace(/^v/, "").split(".").map((n) => Number.parseInt(n, 10) || 0);
|
||||
for (let i = 0; i < Math.max(p1.length, p2.length); i++) {
|
||||
const a = p1[i] ?? 0;
|
||||
const b = p2[i] ?? 0;
|
||||
if (a > b) return 1;
|
||||
if (a < b) return -1;
|
||||
}
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function isNewerVersion(latest: string, current: string): boolean {
|
||||
return compareSemVer(latest, current) > 0;
|
||||
}
|
||||
|
||||
function runCommand(command: string, args: string[], options: { cwd?: string } = {}) {
|
||||
|
||||
Reference in new issue
Block a user