Skip to content

Commit b417887

Browse files
committed
fix: flag malformed codex auth payloads in doctor
1 parent 3e80d52 commit b417887

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

lib/codex-manager/repair-commands.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,14 @@ export async function runDoctor(
16871687
try {
16881688
const raw = await fs.readFile(codexAuthPath, "utf-8");
16891689
const parsed = JSON.parse(raw) as unknown;
1690-
if (parsed && typeof parsed === "object") {
1690+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
1691+
addCheck({
1692+
key: "codex-auth-readable",
1693+
severity: "error",
1694+
message: "Codex auth file has invalid structure",
1695+
details: codexAuthPath,
1696+
});
1697+
} else {
16911698
const payload = parsed as Record<string, unknown>;
16921699
const tokens = payload.tokens && typeof payload.tokens === "object"
16931700
? payload.tokens as Record<string, unknown>
@@ -1708,19 +1715,19 @@ export async function runDoctor(
17081715
emailFromFile ?? extractAccountEmail(accessToken, idToken),
17091716
);
17101717
codexAuthAccountId = accountIdFromFile ?? extractAccountId(accessToken);
1718+
addCheck({
1719+
key: "codex-auth-readable",
1720+
severity: "ok",
1721+
message: "Codex auth file is readable",
1722+
details:
1723+
codexAuthEmail || codexAuthAccountId
1724+
? formatDoctorIdentitySummary({
1725+
email: codexAuthEmail,
1726+
accountId: codexAuthAccountId,
1727+
})
1728+
: undefined,
1729+
});
17111730
}
1712-
addCheck({
1713-
key: "codex-auth-readable",
1714-
severity: "ok",
1715-
message: "Codex auth file is readable",
1716-
details:
1717-
codexAuthEmail || codexAuthAccountId
1718-
? formatDoctorIdentitySummary({
1719-
email: codexAuthEmail,
1720-
accountId: codexAuthAccountId,
1721-
})
1722-
: undefined,
1723-
});
17241731
} catch (error) {
17251732
addCheck({
17261733
key: "codex-auth-readable",

test/repair-commands.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,25 @@ describe("repair-commands direct deps coverage", () => {
751751
);
752752
});
753753

754+
it("runDoctor marks malformed codex auth payloads as invalid instead of healthy", async () => {
755+
existsSyncMock.mockImplementation((path) => path === "/mock/auth.json");
756+
readFileMock.mockResolvedValueOnce("[]");
757+
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
758+
759+
const exitCode = await runDoctor(["--json"], createDeps());
760+
761+
expect(exitCode).toBe(1);
762+
expect(
763+
JSON.parse(String(consoleSpy.mock.calls.at(-1)?.[0] ?? "{}")).checks,
764+
).toContainEqual(
765+
expect.objectContaining({
766+
key: "codex-auth-readable",
767+
severity: "error",
768+
message: "Codex auth file has invalid structure",
769+
}),
770+
);
771+
});
772+
754773
it("runDoctor derives auto-fix state from the final action set", async () => {
755774
const now = Date.now();
756775
let persistedAccountStorage: unknown;

0 commit comments

Comments
 (0)