OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(cli): use semver for update notices (#603)
mldangelo-oaia2cd1d6ed949
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.jsonindex 6621f707f..91efc2769 100644--- a/sdk/typescript/package.json+++ b/sdk/typescript/package.json@@ -68,6 +68,7 @@"papaparse": "5.5.3","pdfjs-dist": "6.2.108","react": "19.2.4",+ "semver": "7.8.5","smol-toml": "1.6.1"},"devDependencies": {@@ -76,6 +77,7 @@"@types/node": "22.19.17","@types/papaparse": "5.3.15","@types/react": "19.2.14",+ "@types/semver": "7.8.0","fast-check": "4.9.0","ink-testing-library": "4.0.0","json-schema-to-typescript": "15.0.4",diff --git a/sdk/typescript/pnpm-lock.yaml b/sdk/typescript/pnpm-lock.yamlindex d9f0706c0..f3b3b0729 100644--- a/sdk/typescript/pnpm-lock.yaml+++ b/sdk/typescript/pnpm-lock.yaml@@ -50,6 +50,9 @@ importers:react:specifier: 19.2.4version: 19.2.4+ semver:+ specifier: 7.8.5+ version: 7.8.5smol-toml:specifier: 1.6.1version: 1.6.1@@ -69,6 +72,9 @@ importers:'@types/react':specifier: 19.2.14version: 19.2.14+ '@types/semver':+ specifier: 7.8.0+ version: 7.8.0fast-check:specifier: 4.9.0version: 4.9.0@@ -619,6 +625,9 @@ packages:'@types/[email protected]':resolution: {integrity: sha512-ilcTH/UniCkMdtexkoCN0bI7pMcJDvmQFPvuPvmEaYA/NSfFTAgdUSLAoVjaRJm7+6PvcM+q1zYOwS4wTYMF9w==}+ '@types/[email protected]':+ resolution: {integrity: sha512-1mAINjtQCXXeLkJ9ehXkwOcBpqtLxiVtKhpUf83DdRNdQKV0iXZpaHYqRr7nj+wvxuJzoAmAwXI+sCNMv1CzLQ==}+'@types/[email protected]':resolution: {integrity: sha512-oJoftv0LSuaDZE3Le4DbKX+KS9G36NzOeSap90UIK0yMA/NhKJhqlSGtNDORNRaIbQfzjXDrQa0ytJ6mNRGz/Q==}@@ -1928,6 +1937,8 @@ snapshots:dependencies:csstype: 3.2.3+ '@types/[email protected]': {}+'@types/[email protected]':dependencies:'@types/node': 22.19.17diff --git a/sdk/typescript/src/version.ts b/sdk/typescript/src/version.tsindex a54bf022d..241666585 100644--- a/sdk/typescript/src/version.ts+++ b/sdk/typescript/src/version.ts@@ -1,5 +1,6 @@import { readFileSync } from "node:fs";import { fileURLToPath } from "node:url";+import semverGt from "semver/functions/gt.js";const PACKAGE_VERSIONS = packageVersions(new URL("../package.json", import.meta.url),@@ -11,8 +12,6 @@ export const CODEX_EXECUTABLE_VERSION = PACKAGE_VERSIONS.executable;export const BUNDLED_PLUGIN_VERSION = "0.1.22" as const;const PACKAGE_NAME = "@openai/codex-security";-const VERSION_PATTERN =- /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/u;export interface UpdateNotice {readonly currentVersion: string;@@ -116,7 +115,7 @@ export async function checkForUpdate({manifest === null ||!("version" in manifest) ||typeof manifest.version !== "string" ||- !isNewerVersion(manifest.version, currentVersion)+ !semverGt(manifest.version, currentVersion)) {return undefined;}@@ -146,46 +145,6 @@ export function formatUpdateNotice(notice: UpdateNotice): string {].join("\n");}-function isNewerVersion(latest: string, current: string): boolean {- const candidate = VERSION_PATTERN.exec(latest);- const installed = VERSION_PATTERN.exec(current);- if (candidate === null || installed === null) return false;-- for (let index = 1; index <= 3; index += 1) {- const difference = Number(candidate[index]) - Number(installed[index]);- if (difference !== 0) return difference > 0;- }-- if (candidate[4] === installed[4]) return false;- if (candidate[4] === undefined) return true;- if (installed[4] === undefined) return false;- 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): {package: string;sdk: string;diff --git a/sdk/typescript/tests-ts/update-notice.test.ts b/sdk/typescript/tests-ts/update-notice.test.tsindex ed1b24645..e8b68c446 100644--- a/sdk/typescript/tests-ts/update-notice.test.ts+++ b/sdk/typescript/tests-ts/update-notice.test.ts@@ -123,8 +123,10 @@ describe("CLI update notice", () => {test("ignores current, older, invalid, and lower prerelease versions", async () => {for (const [current, latest, available] of [["0.1.0", "0.1.0", false],+ ["0.1.0+local", "0.1.0+registry", false],["0.2.0", "0.1.0", false],["0.2.0", "not-a-version", false],+ ["not-a-version", "0.2.0", false],["0.2.0", "0.2.0-beta.1", false],["0.2.0-beta.2", "0.2.0-beta.1", false],["0.2.0-beta.1", "0.2.0-beta.2", true],