Skip to content

Commit 57562ce

Browse files
committed
fix(server): name the model a voice join and a standalone socket actually run
Two ways a live request reached a model its key had not been granted. A Realtime standalone socket forwards its own model= query untouched, but the external audio path read that query only for the Frameless style, so a realtime-standalone join was judged as the default voice model while its query carried another. Both standalone styles now report the model they forward. A join names no model of its own. The external path has a per-key call registry, so the model a call settled on is recorded at create time and a rejoin is judged against it -- a key scoped to the default can no longer ride a call created for another model. The native compatibility path keeps no such record and gains none here: a join, and a call-create that sends no session model, name no destination, so a key carrying a model list is refused rather than admitted against an assumed default. A provider-only scope and an unscoped key are unchanged on both paths. Reported in review of #5290.
1 parent 22c4ef0 commit 57562ce

6 files changed

Lines changed: 129 additions & 34 deletions

File tree

‎src/server/audio-live.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { LIVE_AUDIO_MODEL, resolveAudioUpstream, type AudioUpstream } from "./au
1111
import { registerTurn, unregisterTurn } from "./lifecycle";
1212
import {
1313
backendJsonBodyFromApiMultipart, buildLiveSidebandUpstreamWsUrl, forwardLiveUrl, keyedLiveUrl,
14-
LIVE_CLIENT_PROTOCOL_HEADERS, LIVE_REQUEST_MAX_BYTES, LIVE_RESPONSE_MAX_BYTES, readBodyCapped,
14+
LIVE_CLIENT_PROTOCOL_HEADERS, LIVE_REQUEST_MAX_BYTES, LIVE_RESPONSE_MAX_BYTES, liveSidebandModel,
15+
readBodyCapped,
1516
type LiveSidebandTarget,
1617
} from "./live";
1718
import { LIVE_CALL_TTL_MS, LiveCallBindings, upstreamLiveCallId } from "./live-call-bindings";
@@ -115,6 +116,8 @@ export async function handleExternalLive(
115116
owner: options.client.owner, upstreamCallId: callId,
116117
joinStyle: frameless ? "frameless-path" : "realtime-query",
117118
providerName: relay.providerName,
119+
// A join carries no model of its own; this is the one the call settled on.
120+
model,
118121
accountId: context ? context.kind === "main" ? callerOwned ? undefined : MAIN_CODEX_ACCOUNT_ID : context.accountId : undefined,
119122
chatgptAccountId: new Headers(relay.headers).get("chatgpt-account-id") ?? undefined,
120123
keyedCredentialDigest: relay.keyed ? createHash("sha256").update(new Headers(relay.headers).get("authorization") ?? "").digest("hex") : undefined,
@@ -150,13 +153,16 @@ export async function resolveExternalLiveSocket(
150153
}
151154
let upstreamTarget = binding ? { style: binding.joinStyle, callId: binding.upstreamCallId } as LiveSidebandTarget : target;
152155
const frameless = upstreamTarget.style === "frameless-path" || upstreamTarget.style === "frameless-standalone";
153-
let model = LIVE_AUDIO_MODEL;
154156
if (upstreamTarget.style === "frameless-standalone") {
155157
const query = new URLSearchParams(upstreamTarget.query);
156158
if (!query.has("model") || query.get("model") === "gpt-live-1") query.set("model", LIVE_AUDIO_MODEL);
157-
model = query.get("model") ?? LIVE_AUDIO_MODEL;
158159
upstreamTarget = { ...upstreamTarget, query: query.toString() };
159160
}
161+
// What this socket will actually run: the model a bound call settled on when
162+
// this same key created it, or the one a standalone session forwards in its
163+
// own query. Reading only the Frameless query left a Realtime standalone
164+
// socket judged as the default while its query carried another model.
165+
const model = binding?.model ?? liveSidebandModel(upstreamTarget) ?? LIVE_AUDIO_MODEL;
160166
const relay = await resolveAudioUpstream(client.headers, config, log, {
161167
admission: client.admission, model, lease: options.lease,
162168
signal: options.signal,

‎src/server/live-call-bindings.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export interface LiveCallBinding {
99
upstreamCallId: string;
1010
joinStyle: "frameless-path" | "realtime-query";
1111
providerName: string;
12+
/**
13+
* The model this call settled on when it was created. A join names no model
14+
* of its own, so this is what a per-key scope is judged against on reconnect
15+
* — without it, a call created for one model could be rejoined as another.
16+
*/
17+
model?: string;
1218
accountId?: string;
1319
chatgptAccountId?: string;
1420
keyedCredentialDigest?: string;

‎src/server/live.ts‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import type { OcxConfig } from "../types";
4646
import { resolveFirstUsableOpenAiSidecar, selectOpenAiImagesProvider } from "../providers/openai-sidecar";
4747
import { ForwardAdmissionCredentialError, validateForwardAdmissionCredential, type DataPlaneAdmission } from "./auth-cors";
4848
import { admissionScopeDenial } from "./admission-model-scope";
49-
import { LIVE_AUDIO_MODEL } from "./audio-upstream";
5049
import type { RequestLogContext } from "./request-log";
5150
import { codexLogAccountId } from "./responses";
5251
import type { AdmissionLease } from "../lib/admission";
@@ -558,22 +557,24 @@ async function readRequestBodyCapped(req: Request, maxBytes: number): Promise<Ar
558557
* This path has no router to resolve a destination, so the model is read where
559558
* the client states it — the call-create session, or a standalone socket's own
560559
* query — and the provider is whichever OpenAI upstream this relay settles on.
560+
* `model` is undefined when nobody stated one: a call-create that sends no
561+
* session model, or a join onto a call this compatibility path never recorded.
561562
*/
562563
export interface LiveScopeDestination {
563564
admission?: DataPlaneAdmission;
564-
model: string;
565+
model: string | undefined;
565566
}
566567

567568
/**
568-
* The live model a call-create body names, or the default this relay would get.
569+
* The live model a call-create body names, or undefined when it names none.
569570
*
570571
* Both inbound shapes carry it at `session.model` — JSON directly, multipart in
571-
* the `session` field — and the body is relayed upstream unchanged, so the
572-
* string is the destination rather than a selector the proxy rewrites. A body
573-
* that states nothing readable resolves to the default, which is the model the
574-
* upstream would then run.
572+
* the `session` field — and this path relays the body upstream unchanged, so
573+
* the string is the destination rather than a selector the proxy rewrites. A
574+
* body that states nothing readable leaves the model to the upstream, which is
575+
* a destination no model list can describe.
575576
*/
576-
export async function liveCallCreateModel(body: ArrayBuffer, contentType: string): Promise<string> {
577+
export async function liveCallCreateModel(body: ArrayBuffer, contentType: string): Promise<string | undefined> {
577578
try {
578579
let session: unknown;
579580
if (contentType.toLowerCase().includes("multipart/form-data")) {
@@ -584,23 +585,26 @@ export async function liveCallCreateModel(body: ArrayBuffer, contentType: string
584585
session = (JSON.parse(new TextDecoder().decode(body)) as { session?: unknown } | null)?.session;
585586
}
586587
const model = (session as { model?: unknown } | null | undefined)?.model;
587-
return typeof model === "string" && model.trim() ? model.trim() : LIVE_AUDIO_MODEL;
588+
return typeof model === "string" && model.trim() ? model.trim() : undefined;
588589
} catch {
589-
return LIVE_AUDIO_MODEL;
590+
return undefined;
590591
}
591592
}
592593

593594
/**
594-
* The model a sideband upgrade is for. A standalone session names it in its own
595-
* query; a join onto an existing call names nothing, because the call it
596-
* attaches to stated its model at create time.
595+
* The model a sideband upgrade is for, or undefined when the request names none.
596+
*
597+
* A standalone session states it in the query it forwards. A join names nothing
598+
* of its own: the call it attaches to chose a model at create time, and only a
599+
* recorded binding can say which. This compatibility path keeps no such record,
600+
* so a native join is undefined here and the external path in `audio-live.ts`
601+
* supplies the model its binding stored.
597602
*/
598-
export function liveSidebandModel(target: LiveSidebandTarget): string {
603+
export function liveSidebandModel(target: LiveSidebandTarget): string | undefined {
599604
if (target.style === "realtime-standalone" || target.style === "frameless-standalone") {
600-
const model = new URLSearchParams(target.query).get("model")?.trim();
601-
if (model) return model;
605+
return new URLSearchParams(target.query).get("model")?.trim() || undefined;
602606
}
603-
return LIVE_AUDIO_MODEL;
607+
return undefined;
604608
}
605609

606610
/**
@@ -678,12 +682,14 @@ export async function resolveLiveRelay(
678682

679683
// Client protocol headers first so provider/auth headers below always win on conflict.
680684
const headers: Record<string, string> = clientProtocolHeaders(req.headers);
681-
const scopedModel = destination?.model ?? LIVE_AUDIO_MODEL;
685+
const scopedModel = destination?.model;
682686
if (forward) {
683687
const { provider } = forward;
684-
// The upstream is settled here, and voice bills it for the session model the
685-
// caller stated. Refuse before any of it is sent, and give back the probe
686-
// lease the resolution took.
688+
// The upstream is settled here and voice bills it for whatever model this
689+
// request carries. Refuse before any of it is sent, and give back the probe
690+
// lease the resolution took. A join states no model and this path keeps no
691+
// record of the call it attaches to, so a key with a model list is refused
692+
// there rather than admitted against an assumed default.
687693
const denial = admissionScopeDenial(config, destination?.admission, scopedModel, {
688694
providerName: forward.providerName,
689695
modelId: scopedModel,

‎structure/data-planes/inbound-compat.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ keep credentials and audio content out of redirects, request logs and durable st
3030

3131
`src/server/audio-upstream.ts` is also where a configured key's model and provider scope is
3232
applied, once for every audio surface that resolves through it: the model it is handed is the one
33-
the upstream will run — the transcription model, the live session model, or the model a standalone
34-
socket names in its own query — and a refused forward request releases its probe lease. The native
35-
voice path in `src/server/live.ts` applies the same predicate, reading the model from the
36-
call-create session or the socket query and the provider from the upstream it settles on. Coverage
37-
lives in `tests/server/api-key-scope-audio.test.ts` and `tests/server/api-key-scope-live.test.ts`.
33+
the upstream will run — the transcription model, the live session model, the model a standalone
34+
socket names in its own query, or the model a bound call settled on when the same key created it —
35+
and a refused forward request releases its probe lease. `LiveCallBinding` records that model for
36+
exactly this reason, so a reconnect is judged on the call it rejoins rather than on a default.
37+
38+
The native voice path in `src/server/live.ts` applies the same predicate but can name less. It
39+
records nothing about the calls it relays, so a join, and a call-create that sends no session
40+
model, name no destination at all; a key carrying a model list is refused there rather than
41+
admitted against an assumed default, while a provider-only scope and an unscoped key are
42+
unchanged. Coverage lives in `tests/server/api-key-scope-audio.test.ts` and
43+
`tests/server/api-key-scope-live.test.ts`.
3844

3945
## Streaming audio
4046

‎tests/server/api-key-scope-audio.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,48 @@ test("a standalone realtime socket is judged on the model in its own query", asy
195195
expect((await denial(response)).model).toBe(OTHER_LIVE_MODEL);
196196
});
197197

198+
test("a Realtime standalone socket is judged on its query too, not the default", async () => {
199+
// The Frameless branch rewrites its query and was the only one read. A
200+
// Realtime standalone socket forwards `model=` untouched, so reading the
201+
// default here admitted a model the caller was never allowed to run.
202+
const cfg = config({ allowedModels: [LIVE_AUDIO_MODEL] });
203+
const resolved = await resolveExternalLiveSocket(
204+
client(cfg),
205+
cfg,
206+
logContext(),
207+
{ style: "realtime-standalone", query: "intent=quicksilver&model=" + OTHER_LIVE_MODEL },
208+
{ lease: lease(), bindings: new LiveCallBindings() },
209+
);
210+
expect(resolved).toBeInstanceOf(Response);
211+
expect((resolved as Response).status).toBe(403);
212+
expect((await denial(resolved as Response)).model).toBe(OTHER_LIVE_MODEL);
213+
});
214+
215+
test("rejoining a call is judged on the model that call settled on", async () => {
216+
const cfg = config({ allowedModels: [LIVE_AUDIO_MODEL] });
217+
const audio = client(cfg);
218+
const bindings = new LiveCallBindings();
219+
const alias = bindings.create({
220+
owner: audio.owner,
221+
upstreamCallId: "call-upstream",
222+
joinStyle: "frameless-path",
223+
providerName: "openai-apikey",
224+
model: OTHER_LIVE_MODEL,
225+
callerOwned: false,
226+
});
227+
expect(alias).not.toBeNull();
228+
const resolved = await resolveExternalLiveSocket(
229+
audio,
230+
cfg,
231+
logContext(),
232+
{ style: "frameless-path", callId: alias as string },
233+
{ lease: lease(), bindings },
234+
);
235+
expect(resolved).toBeInstanceOf(Response);
236+
expect((resolved as Response).status).toBe(403);
237+
expect((await denial(resolved as Response)).model).toBe(OTHER_LIVE_MODEL);
238+
});
239+
198240
test("a key with no scope keeps every audio destination", async () => {
199241
const cfg = config({ allowedProviders: ["some-other-provider"] });
200242
const response = await handleAudioTranscriptions(
@@ -206,4 +248,3 @@ test("a key with no scope keeps every audio destination", async () => {
206248
expect(response.status).toBe(200);
207249
expect(upstreamCalls).toHaveLength(1);
208250
});
209-

‎tests/server/api-key-scope-live.test.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { afterEach, beforeEach, expect, test } from "bun:test";
1111
import { mkdtempSync } from "node:fs";
1212
import { tmpdir } from "node:os";
1313
import { join } from "node:path";
14-
import { MODEL_NOT_ALLOWED_FOR_KEY } from "../../src/server/admission-model-scope";
14+
import { MODEL_NOT_ALLOWED_FOR_KEY, UNNAMED_DESTINATION_MODEL } from "../../src/server/admission-model-scope";
1515
import type { DataPlaneAdmission } from "../../src/server/auth-cors";
1616
import { LIVE_AUDIO_MODEL } from "../../src/server/audio-upstream";
1717
import { handleLive, resolveLiveSidebandUpgrade } from "../../src/server/live";
@@ -127,7 +127,7 @@ test("a multipart call-create states its model in the session field", async () =
127127
expect(upstreamCalls).toEqual([]);
128128
});
129129

130-
test("a session that names no model resolves to the default voice model", async () => {
130+
test("a call-create that names no model has no destination a model list can allow", async () => {
131131
const response = await handleLive(
132132
jsonCallCreate(),
133133
config({ allowedModels: ["something-else"] }),
@@ -136,7 +136,20 @@ test("a session that names no model resolves to the default voice model", async
136136
SCOPED,
137137
);
138138
expect(response.status).toBe(403);
139-
expect((await denial(response)).model).toBe(LIVE_AUDIO_MODEL);
139+
expect((await denial(response)).model).toBe(UNNAMED_DESTINATION_MODEL);
140+
expect(upstreamCalls).toEqual([]);
141+
});
142+
143+
test("a provider-only scope still creates a call that names no model", async () => {
144+
const response = await handleLive(
145+
jsonCallCreate(),
146+
config({ allowedProviders: ["openai-apikey"] }),
147+
logContext(),
148+
undefined,
149+
SCOPED,
150+
);
151+
expect(response.status).toBe(200);
152+
expect(upstreamCalls).toHaveLength(1);
140153
});
141154

142155
test("a provider outside the scope cannot serve a voice call", async () => {
@@ -179,7 +192,11 @@ test("a standalone realtime socket is judged on the model in its query", async (
179192
expect((await denial(resolved as Response)).model).toBe(OTHER_LIVE_MODEL);
180193
});
181194

182-
test("joining an existing call carries the default voice model", async () => {
195+
test("a native join names no model, so a model list cannot admit it", async () => {
196+
// This compatibility path records nothing about the calls it relays, so the
197+
// model a join attaches to is unknowable here. Admitting it against an
198+
// assumed default would let a key scoped to that default ride a call created
199+
// for another model.
183200
const resolved = await resolveLiveSidebandUpgrade(
184201
new Request("http://localhost/v1/live/call-abc", { headers: { "x-opencodex-api-key": SCOPED_KEY } }),
185202
config({ allowedModels: [LIVE_AUDIO_MODEL] }),
@@ -188,6 +205,19 @@ test("joining an existing call carries the default voice model", async () => {
188205
undefined,
189206
SCOPED,
190207
);
208+
expect(resolved).toBeInstanceOf(Response);
209+
expect((resolved as Response).status).toBe(403);
210+
});
211+
212+
test("a provider-only scope still joins an existing call", async () => {
213+
const resolved = await resolveLiveSidebandUpgrade(
214+
new Request("http://localhost/v1/live/call-abc", { headers: { "x-opencodex-api-key": SCOPED_KEY } }),
215+
config({ allowedProviders: ["openai-apikey"] }),
216+
logContext(),
217+
{ style: "frameless-path", callId: "call-abc" },
218+
undefined,
219+
SCOPED,
220+
);
191221
expect(resolved).not.toBeInstanceOf(Response);
192222
expect((resolved as { upstreamWsUrl: string }).upstreamWsUrl).toContain("call-abc");
193223
});

0 commit comments

Comments
 (0)