Skip to content

Write HTML. Render video. Built for agents.

fix(cli): validate cached versions in update metadata (#5214)

Abdullah M. Nazifb407178f3a33
diff --git a/packages/cli/src/utils/updateCheck.test.ts b/packages/cli/src/utils/updateCheck.test.ts
index 0df1dd9e36..18b6b2e11b 100644
--- a/packages/cli/src/utils/updateCheck.test.ts
+++ b/packages/cli/src/utils/updateCheck.test.ts
@@ -120,6 +120,10 @@ describe("printUpdateNotice — install-method-aware command", () => {
});
expect(out).toBe("");
});
+
+ it("does not advertise an update from an invalid cached version", async () => {
+ expect(await noticeWith({ installerCommand: null, latestVersion: "not-a-version" })).toBe("");
+ });
});
/**
@@ -421,4 +425,33 @@ describe("updateCheckDue / cachedUpdateCheck — what the parent reads without f
expect(poisoned.updateAvailable).toBe(false);
expect(poisoned.latest).not.toContain(";");
});
+
+ it.each(["not-a-version", "999.0.0; invalid", 999, ["999.0.0"], {}, null, undefined])(
+ "omits invalid cached version %j from JSON metadata",
+ async (latestVersion) => {
+ const mod = await load({ latestVersion });
+ const wrapped = mod.withMeta({ ok: true });
+ expect(wrapped._meta).toEqual({
+ version: mod.cachedUpdateCheck().current,
+ latestVersion: undefined,
+ updateAvailable: false,
+ });
+ expect(JSON.parse(JSON.stringify(wrapped))).toEqual({
+ ok: true,
+ _meta: { version: wrapped._meta.version, updateAvailable: false },
+ });
+ },
+ );
+
+ it.each(["0.0.1", "999.0.0", "999.0.0-beta.1+build.5"])(
+ "preserves safe cached version %s and the update-check result in metadata",
+ async (latestVersion) => {
+ const mod = await load({ latestVersion });
+ expect(mod.getUpdateMeta()).toEqual({
+ version: mod.cachedUpdateCheck().current,
+ latestVersion,
+ updateAvailable: mod.cachedUpdateCheck().updateAvailable,
+ });
+ },
+ );
});
diff --git a/packages/cli/src/utils/updateCheck.ts b/packages/cli/src/utils/updateCheck.ts
index 9bd418096b..abcda91471 100644
--- a/packages/cli/src/utils/updateCheck.ts
+++ b/packages/cli/src/utils/updateCheck.ts
@@ -108,7 +108,7 @@ export function cachedUpdateCheck(): UpdateCheckResult {
function fallbackResult(cachedLatest?: string): UpdateCheckResult {
// Only surface a cached version we can prove is safe — a pre-existing
// poisoned cache must not leak through the fallback path either.
- const safeCached = cachedLatest && isSafeVersion(cachedLatest) ? cachedLatest : undefined;
+ const safeCached = safeCachedVersion(cachedLatest);
return {
current: VERSION,
latest: safeCached ?? VERSION,
@@ -116,16 +116,20 @@ function fallbackResult(cachedLatest?: string): UpdateCheckResult {
};
}
+function safeCachedVersion(value: unknown): string | undefined {
+ return typeof value === "string" && isSafeVersion(value) ? value : undefined;
+}
+
/**
* Synchronous read from cache — for _meta envelope on --json commands.
* Never fetches. Returns what the last background check found.
*/
export function getUpdateMeta(): UpdateMeta {
- const config = readConfig();
+ const latestVersion = safeCachedVersion(readConfig().latestVersion);
return {
version: VERSION,
- latestVersion: config.latestVersion,
- updateAvailable: config.latestVersion ? isNewerSemver(config.latestVersion, VERSION) : false,
+ latestVersion,
+ updateAvailable: latestVersion ? isNewerSemver(latestVersion, VERSION) : false,
};
}