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): guide recovery when a refresh token was already used (#2289)

Michael D'Angelo9f3d97babcc5
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 722a99f1a..589a3fc96 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -130,8 +130,19 @@ after logout completes.
Scans and status checks expand a home-relative `CODEX_HOME` using the caller's
`HOME` or `USERPROFILE` environment setting.
-If credentials cannot refresh, run `login status`. Retry if the sign-in recently
-changed; otherwise run `logout`, then `login`.
+If you use file-based ChatGPT credentials with both Codex and Codex Security,
+run `codex-security login` before your first scan or status check to give Codex
+Security its own session. An imported sign-in shares a single-use refresh token.
+When either client renews it, the other client's next refresh can fail even
+while its login status reports that it is signed in.
+
+If Codex Security reports that the refresh token was already used, run
+`codex-security logout`, then `codex-security login`. If your Codex CLI or app
+reports that error, sign out and sign back in there.
+
+For other refresh errors, check `codex-security login status`. Retry if the
+sign-in recently changed. Otherwise run `codex-security logout`, then
+`codex-security login`.
Some cybersecurity requests and protected findings require
[Trusted Access for Cyber](https://chatgpt.com/cyber).
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index c92a6a391..6f0ffee5a 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -11264,17 +11264,19 @@ function scanFailureMessage(
: nativeRefreshRecovery;
const detail =
error instanceof IncompleteScanError ? `${diagnosticValue(error)}\n` : "";
- if (
- /\byour access token could not be refreshed(?: because your refresh token (?:has expired|was already used|was revoked))?\. Please log out and sign in again\./iu.test(
- message,
- )
- ) {
+ const refreshFailure = message.match(
+ /\byour access token could not be refreshed(?: because your refresh token (has expired|was already used|was revoked))?\. Please log out and sign in again\./iu,
+ );
+ if (refreshFailure) {
+ const recovery =
+ refreshFailure[1]?.toLowerCase() === "was already used"
+ ? "The refresh token was already used. Run"
+ : "If the sign-in recently changed, check 'npx @openai/codex-security login status' and retry. Otherwise run";
return (
detail +
"Codex Security's stored ChatGPT sign-in could not be refreshed. " +
"Codex may still need it to load workspace-managed policies when an API key is selected for model authentication. " +
- "If the sign-in recently changed, check 'npx @openai/codex-security login status' and retry. " +
- "Otherwise run 'npx @openai/codex-security logout', then 'npx @openai/codex-security login'."
+ `${recovery} 'npx @openai/codex-security logout', then 'npx @openai/codex-security login'.`
);
}
switch (classification) {
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index 1da891be5..276f482e3 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -1042,9 +1042,14 @@ describe("CLI authentication", () => {
expect(stderr.text()).toContain(
"API key is selected for model authentication",
);
- expect(stderr.text()).toContain(
- "npx @openai/codex-security login status",
- );
+ if (detail.includes("was already used")) {
+ expect(stderr.text()).toContain("refresh token was already used");
+ expect(stderr.text()).not.toContain("login status");
+ } else {
+ expect(stderr.text()).toContain(
+ "npx @openai/codex-security login status",
+ );
+ }
expect(stderr.text()).toContain(
"npx @openai/codex-security logout', then 'npx @openai/codex-security login",
);