Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/oauth/login-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export async function notifyRunningProxy(name: string, provider: unknown): Promi
// Identity-checked runtime-port lookup: reaches a fallback-port proxy and avoids
// posting credentials-adjacent config to whatever else answers on config.port.
const live = await findLiveProxy();
if (!live) return;
// A public /healthz response is only a discovery hint. Do not disclose provider
// credentials unless the reported/discovered process passed PID identity checks.
if (!live || live.pid === null) return;
try {
await fetch(`http://${probeHostname(live.hostname)}:${live.port}/api/providers`, {
method: "POST",
Expand Down
25 changes: 24 additions & 1 deletion tests/oauth-login-cli-live-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { loadConfig, saveConfig } from "../src/config";
import { upsertOAuthProvider } from "../src/oauth";
import { notifyRunningProxyAfterOAuthLogin } from "../src/oauth/login-cli";
import { notifyRunningProxy, notifyRunningProxyAfterOAuthLogin } from "../src/oauth/login-cli";
import { startServer } from "../src/server";
import type { OcxConfig } from "../src/types";
import { installIsolatedCodexHome, type IsolatedCodexHome } from "./helpers/isolated-codex-home";
Expand Down Expand Up @@ -53,6 +53,29 @@ afterEach(() => {
});

describe("CLI OAuth live-update credential preservation", () => {
test("does not send provider credentials to a spoofed configured-port listener", async () => {
const requests: Array<{ path: string; body: string }> = [];
const fake = Bun.serve({
port: 0,
hostname: "127.0.0.1",
async fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/healthz") {
return Response.json({ status: "ok", service: "opencodex", version: "test", uptime: 1, pid: 999_999 });
}
requests.push({ path: url.pathname, body: await request.text() });
return Response.json({ ok: true });
},
});
try {
saveConfig(keyModeXaiConfig(fake.port));
await notifyRunningProxy("xai", { apiKey: "live-update-sentinel-key" });
expect(requests).toEqual([]);
} finally {
await fake.stop(true);
}
});

test("notify after OAuth login keeps key billing on live and disk configs", async () => {
const server = startServer(0);
try {
Expand Down
Loading