Skip to content

Commit 144ddf4

Browse files
lidge-junjunalan7629
authored
fix(server): allow POST /v1/alpha/search on the loopback listener (lidge-jun#3205)
The unauthenticated loopback listener admits routes through an allowlist in loopbackRouteAllowed(). /v1/alpha/search - the native Codex web-search relay - was never on it, so a directly-spawned codex app-server got 404 for every web search (lidge-jun#3192). Admit POST on that path. The handler runs its own admission (resolveApiAuth + validateForwardAdmissionCredential), so a loopback caller without a ChatGPT credential is still refused inside the relay; only the listener's 404 goes away. The public listener is unchanged. Clean reimplementation of lidge-jun#3193, whose branch was byte-corrupted by an encoding round-trip (em-dashes and emoji in ~30 unrelated comment lines). Supersedes lidge-jun#3193. Fixes lidge-jun#3192. Co-authored-by: jun <[email protected]> Co-authored-by: alan7629 <[email protected]>
1 parent 9661611 commit 144ddf4

6 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎docs-site/src/content/docs/fr/reference/configuration/server.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ Le port est obligatoire et doit différer du port proxy. Il n'est jamais attribu
105105
changerait au fil des redémarrages tandis que les serveurs d'applications déjà en cours d'exécution conservaient le `base_url` précédent.
106106

107107
L'écouteur ne sert que `POST /v1/responses`, sa mise à niveau WebSocket, `POST /v1/responses/compact`,
108-
et `GET /v1/models`. Tout le reste, y compris `/api/*` et le tableau de bord, renvoie `404`.
108+
`POST /v1/alpha/search` (le relais de recherche web natif de Codex), `GET /v1/models` et les mises à
109+
niveau WebSocket vocales autonomes. Tout le reste, y compris `/api/*` et le tableau de bord, renvoie `404`.
109110

110111
:::danger[Surface non authentifiée]
111112
Chaque processus de la machine peut utiliser cet écouteur. Il consomme le quota du compte et utilise les identifiants de

‎docs-site/src/content/docs/reference/configuration/server.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ The port is required and must differ from the proxy port. It is never OS-assigne
113113
would change across restarts while already-running app-servers kept the previous `base_url`.
114114

115115
The listener serves only `POST /v1/responses`, its WebSocket upgrade, `POST /v1/responses/compact`,
116-
and `GET /v1/models`. Everything else, including `/api/*` and the dashboard, returns `404`.
116+
`POST /v1/alpha/search` (the native Codex web-search relay), `GET /v1/models`, and the standalone
117+
realtime voice WebSocket upgrades. Everything else, including `/api/*` and the dashboard, returns
118+
`404`.
117119

118120
:::danger[This is an unauthenticated surface]
119121
Every process on the machine can use this listener. It spends account quota and paid provider

‎docs-site/src/content/docs/tr/reference/configuration/server.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ tarafından atanmaz: geçici bir port yeniden başlatmalar arasında değişirke
111111
zaten çalışan app-server'lar önceki `base_url`'i tutardı.
112112

113113
Dinleyici yalnızca `POST /v1/responses`, onun WebSocket yükseltmesi, `POST
114-
/v1/responses/compact` ve `GET /v1/models` sunar. `/api/*` ve kontrol paneli
115-
dahil diğer her şey `404` döndürür.
114+
/v1/responses/compact`, `POST /v1/alpha/search` (yerel Codex web arama aktarımı),
115+
`GET /v1/models` ve bağımsız sesli WebSocket yükseltmelerini sunar. `/api/*` ve
116+
kontrol paneli dahil diğer her şey `404` döndürür.
116117

117118
:::danger[Bu kimliği doğrulanmamış bir yüzeydir]
118119
Makinedeki her süreç bu dinleyiciyi kullanabilir. Hesap kotasını ve ücretli

‎docs-site/src/content/docs/zh-tw/reference/configuration/server.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ stream 開啟前以 `401` 失敗。
8989
該 port 是必填的,且必須與 proxy port 不同。它絕不會由 OS 指派:臨時 port 會在重啟時改變,而
9090
已執行的 app-server 仍保留先前的 `base_url`。
9191

92-
該 listener 只服務 `POST /v1/responses`、其 WebSocket upgrade、`POST /v1/responses/compact` 與
93-
`GET /v1/models`。其他一切,包括 `/api/*` 與儀表板,都會回傳 `404`。
92+
該 listener 只服務 `POST /v1/responses`、其 WebSocket upgrade、`POST /v1/responses/compact`、
93+
`POST /v1/alpha/search`(Codex 原生網頁搜尋中繼)、`GET /v1/models`,以及獨立語音 WebSocket upgrade。
94+
其他一切,包括 `/api/*` 與儀表板,都會回傳 `404`。
9495

9596
:::danger[這是一個未認證的介面]
9697
機器上的每個 process 都可以使用此 listener。它會耗用帳號配額與付費 provider 憑證,也可能耗盡

‎src/server/index.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,13 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
780780
* Routes the unauthenticated loopback listener will serve. Everything else 404s.
781781
*
782782
* This is an allowlist rather than a filter applied to the public handler, because a filter
783-
* inverts the failure mode: a route added later would be reachable here by default. The four
784-
* entries are exactly what a directly-spawned `codex app-server` needs.
783+
* inverts the failure mode: a route added later would be reachable here by default. The
784+
* entries below are exactly what a directly-spawned `codex app-server` needs.
785+
*
786+
* `POST /v1/alpha/search` is the native Codex web-search relay. Codex issues it against the
787+
* same base URL as `/v1/responses`, so leaving it off the list turned every native web search
788+
* on the direct-spawn host into a 404 (#3192). The handler still runs its own admission, so a
789+
* loopback caller without a ChatGPT credential is refused inside it rather than by this gate.
785790
*
786791
* `GET /v1/models` is on the list for a reason that is easy to miss. When catalog
787792
* materialization fails or finds no source, `syncCodex` warns and injects with
@@ -795,6 +800,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
795800
return req.method === "POST" || req.headers.get("upgrade")?.toLowerCase() === "websocket";
796801
}
797802
if (path === "/v1/responses/compact") return req.method === "POST";
803+
if (path === "/v1/alpha/search") return req.method === "POST";
798804
if (path === "/v1/models") return req.method === "GET";
799805
// Standalone realtime voice sessions (codex-rs thread/realtime/start, WebSocket
800806
// transport) — a directly-spawned `codex app-server` needs these for desktop

‎tests/loopback-listener-integration.test.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describe("unauthenticated loopback listener", () => {
268268
// 5s default on a loaded Windows box, where the test measured 5.04s.
269269
}, SERVER_BUDGET_MS);
270270

271-
test("serves only the four allowlisted routes, using each route's real method", async () => {
271+
test("serves only the allowlisted routes, using each route's real method", async () => {
272272
const loopbackPort = await freePort();
273273
saveConfig(baseConfig(loopbackPort));
274274
const server = startServer(0);
@@ -285,13 +285,13 @@ describe("unauthenticated loopback listener", () => {
285285
{ method: "POST", path: "/v1/chat/completions", body: '{"model":"x","messages":[]}' },
286286
{ method: "POST", path: "/v1/messages", body: '{"model":"x","messages":[]}' },
287287
{ method: "POST", path: "/v1/images/generations", body: '{"prompt":"x"}' },
288-
{ method: "POST", path: "/v1/alpha/search", body: '{"query":"x"}' },
289288
{ method: "GET", path: "/v1/opencodex/artifacts/x" },
290289
{ method: "POST", path: "/v1/live", body: "{}" },
291290
{ method: "POST", path: "/v1/realtime/calls", body: "{}" },
292291
// Allowlisted paths still reject the methods they do not serve.
293292
{ method: "DELETE", path: "/v1/responses" },
294293
{ method: "POST", path: "/v1/models" },
294+
{ method: "GET", path: "/v1/alpha/search" },
295295
];
296296
for (const { method, path, body } of denied) {
297297
const res = await fetch(`${base}${path}`, {
@@ -309,6 +309,41 @@ describe("unauthenticated loopback listener", () => {
309309
}
310310
});
311311

312+
test("admits POST /v1/alpha/search so native web search reaches the relay (#3192)", async () => {
313+
const loopbackPort = await freePort();
314+
saveConfig(baseConfig(loopbackPort));
315+
const server = startServer(0);
316+
const body = '{"query":"x"}';
317+
const headers = { "content-type": "application/json" };
318+
try {
319+
// Codex sends its native web-search call to the same base URL as /v1/responses. Before
320+
// the allowlist admitted it, the direct-spawn host answered 404 for every search. What
321+
// proves the gate is open is that the answer comes from BEHIND it: the admitted-turn
322+
// path (503 while native-main maintenance holds, otherwise the relay's own 401 for a
323+
// missing ChatGPT credential), never the listener's 404 and never the public
324+
// listener's "opencodex API key required".
325+
const viaLoopback = await fetch(`http://127.0.0.1:${loopbackPort}/v1/alpha/search`, {
326+
method: "POST", body, headers,
327+
});
328+
const loopbackBody = await viaLoopback.json() as { error?: { message?: string } };
329+
expect(viaLoopback.status).not.toBe(404);
330+
expect([401, 503]).toContain(viaLoopback.status);
331+
expect(loopbackBody.error?.message).toBeDefined();
332+
expect(loopbackBody.error?.message).not.toBe("opencodex API key required");
333+
334+
// The public listener is unchanged: the same request without a key is still refused
335+
// at admission, so widening the loopback allowlist did not widen the public surface.
336+
const viaPublic = await fetch(`http://127.0.0.1:${server.port}/v1/alpha/search`, {
337+
method: "POST", body, headers,
338+
});
339+
expect(viaPublic.status).toBe(401);
340+
const publicBody = await viaPublic.json() as { error?: { message?: string } };
341+
expect(publicBody.error?.message).toBe("opencodex API key required");
342+
} finally {
343+
await server.stop(true);
344+
}
345+
});
346+
312347
test("admits standalone realtime voice WebSocket upgrades, HTTP stays rejected", async () => {
313348
const loopbackPort = await freePort();
314349
saveConfig(baseConfig(loopbackPort));

0 commit comments

Comments
 (0)