Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(cli): order update-notice prereleases by SemVer precedence (#598)

HughChaw177d3ff67ca4
diff --git a/sdk/typescript/src/version.ts b/sdk/typescript/src/version.ts
index 1bf429fb8..a54bf022d 100644
--- a/sdk/typescript/src/version.ts
+++ b/sdk/typescript/src/version.ts
@@ -159,12 +159,31 @@ function isNewerVersion(latest: string, current: string): boolean {
if (candidate[4] === installed[4]) return false;
if (candidate[4] === undefined) return true;
if (installed[4] === undefined) return false;
- return (
- new Intl.Collator("en", { numeric: true }).compare(
- candidate[4],
- installed[4],
- ) > 0
- );
+ return comparePrerelease(candidate[4], installed[4]) > 0;
+}
+
+/** Compare two SemVer prerelease identifiers according to spec precedence. */
+function comparePrerelease(latest: string, current: string): number {
+ const candidate = latest.split(".");
+ const installed = current.split(".");
+ const length = Math.max(candidate.length, installed.length);
+ for (let index = 0; index < length; index += 1) {
+ const candidateId = candidate[index];
+ const installedId = installed[index];
+ if (candidateId === undefined) return -1;
+ if (installedId === undefined) return 1;
+ const candidateNumeric = /^\d+$/u.test(candidateId);
+ const installedNumeric = /^\d+$/u.test(installedId);
+ if (candidateNumeric && installedNumeric) {
+ const difference = Number(candidateId) - Number(installedId);
+ if (difference !== 0) return Math.sign(difference);
+ } else if (candidateNumeric !== installedNumeric) {
+ return candidateNumeric ? -1 : 1;
+ } else if (candidateId !== installedId) {
+ return candidateId < installedId ? -1 : 1;
+ }
+ }
+ return 0;
}
function packageVersions(url: URL): {
diff --git a/sdk/typescript/tests-ts/update-notice.test.ts b/sdk/typescript/tests-ts/update-notice.test.ts
index 4a41976a2..ed1b24645 100644
--- a/sdk/typescript/tests-ts/update-notice.test.ts
+++ b/sdk/typescript/tests-ts/update-notice.test.ts
@@ -148,6 +148,33 @@ describe("CLI update notice", () => {
).toBeUndefined();
});
+ test("orders prerelease identifiers by SemVer precedence, not locale collation", async () => {
+ for (const [current, latest, available] of [
+ // ASCII case ordering: "a" (97) sorts after "A" (65) in SemVer.
+ ["1.0.0-A", "1.0.0-a", true],
+ ["1.0.0-a", "1.0.0-A", false],
+ // Numeric identifiers have lower precedence than non-numeric ones.
+ ["1.0.0-1", "1.0.0-alpha", true],
+ ["1.0.0-alpha", "1.0.0-1", false],
+ // When shared identifiers are equal, the longer list wins.
+ ["1.0.0-alpha", "1.0.0-alpha.1", true],
+ ["1.0.0-alpha.1", "1.0.0-alpha", false],
+ // Numeric identifiers compare numerically regardless of width.
+ ["1.0.0-alpha.9", "1.0.0-alpha.10", true],
+ ["1.0.0-alpha.10", "1.0.0-alpha.9", false],
+ // Mixed identifier lists follow pairwise precedence.
+ ["1.0.0-alpha.2", "1.0.0-alpha.10", true],
+ ["1.0.0-alpha.2", "1.0.0-alpha.beta", true],
+ ] as const) {
+ const notice = await checkForUpdate({
+ environment: {},
+ currentVersion: current,
+ fetch: registryResponse(latest),
+ });
+ expect(notice !== undefined).toBe(available);
+ }
+ });
+
test("suppresses registry checks in CI or when disabled", async () => {
let requests = 0;
const fetchLatest = async () => {