@@ -33,6 +33,8 @@ import { selectProactiveApiKeyTransport } from "../providers/key-failover"; |
33 | 33 | import { getProviderRegistryEntry } from "../providers/registry"; |
34 | 34 | import { readJsonRequestBody, resolveInboundBodyLimitBytes } from "./request-decompress"; |
35 | 35 | import { ForwardAdmissionCredentialError, validateForwardAdmissionCredential } from "./auth-cors"; |
| 36 | +import type { DataPlaneAdmission } from "./auth-cors"; |
| 37 | +import { admissionScopeDenial } from "./admission-model-scope"; |
36 | 38 | import type { RequestLogContext } from "./request-log"; |
37 | 39 | import { codexLogAccountId, decodeRequestErrorResponse } from "./responses"; |
38 | 40 | import { getValidAccessToken, getOAuthCredentialProjectId } from "../oauth/index"; |
@@ -115,6 +117,24 @@ export async function readImageResponseBytes( |
115 | 117 | } |
116 | 118 |
|
117 | 119 | const CCA_IMAGE_MODEL = "gemini-3.1-flash-image"; |
| 120 | +const XAI_IMAGE_BRIDGE_MODEL = "grok-imagine-image-quality"; |
| 121 | + |
| 122 | +/** |
| 123 | + * What a body that names no model resolves to for a scope decision. |
| 124 | + * |
| 125 | + * This relay copies the body through, so an absent `model` leaves the upstream |
| 126 | + * to pick one and there is no destination a scoped key could be shown to be |
| 127 | + * allowed to reach. A key with a model list is refused rather than sent to |
| 128 | + * whatever the provider defaults to; a key scoped only by provider is not |
| 129 | + * affected, and an unscoped key never reaches this at all. |
| 130 | + */ |
| 131 | +export const UNNAMED_IMAGE_MODEL = "(unnamed)"; |
| 132 | + |
| 133 | +/** The selector to echo in a refusal: the string the caller itself sent. */ |
| 134 | +function requestedImageSelector(body: unknown): string { |
| 135 | + const model = (body as { model?: unknown } | null)?.model; |
| 136 | + return typeof model === "string" && model.trim() ? model : UNNAMED_IMAGE_MODEL; |
| 137 | +} |
118 | 138 |
|
119 | 139 | /** |
120 | 140 | * Google Gemini finishReasons that indicate a permanent content/safety block. |
@@ -207,11 +227,21 @@ async function tryCcaImageGeneration( |
207 | 227 | logCtx: RequestLogContext, |
208 | 228 | signal: AbortSignal, |
209 | 229 | endpoint: ImagesEndpoint, |
| 230 | + admission: DataPlaneAdmission | undefined, |
210 | 231 | ): Promise<Response | undefined> { |
211 | 232 | if (endpoint !== "generations") return undefined; |
212 | 233 | const provider = config.providers?.["google-antigravity"]; |
213 | 234 | if (!provider || provider.disabled) return undefined; |
214 | 235 |
|
| 236 | + // The destination is decided here, not by the caller: this branch always |
| 237 | + // bills Antigravity for CCA_IMAGE_MODEL whatever the body named. That is the |
| 238 | + // pair a scoped key is held to. |
| 239 | + const denial = admissionScopeDenial(config, admission, requestedImageSelector(body), { |
| 240 | + providerName: "google-antigravity", |
| 241 | + modelId: CCA_IMAGE_MODEL, |
| 242 | + }); |
| 243 | + if (denial) return denial; |
| 244 | + |
215 | 245 | const prompt = (body as { prompt?: unknown })?.prompt; |
216 | 246 | if (typeof prompt !== "string" || !prompt.trim()) { |
217 | 247 | return formatErrorResponse(400, "invalid_request_error", "prompt is required and must not be empty"); |
@@ -446,10 +476,20 @@ async function tryXaiImageRelay( |
446 | 476 | logCtx: RequestLogContext, |
447 | 477 | signal: AbortSignal | undefined, |
448 | 478 | endpoint: ImagesEndpoint, |
| 479 | + admission: DataPlaneAdmission | undefined, |
449 | 480 | ): Promise<Response | undefined> { |
450 | 481 | if (config.images?.bridgeEnabled !== true) return undefined; |
451 | 482 | const found = findXaiProvider(config); |
452 | 483 | if (!found) return undefined; |
| 484 | + const bridgeModel = config.images?.bridgeModel ?? XAI_IMAGE_BRIDGE_MODEL; |
| 485 | + // The bridge sends this request to the configured xAI provider and the bridge |
| 486 | + // model regardless of the selector in the body, so that is what the key is |
| 487 | + // checked against. |
| 488 | + const denial = admissionScopeDenial(config, admission, requestedImageSelector(body), { |
| 489 | + providerName: found.name, |
| 490 | + modelId: bridgeModel, |
| 491 | + }); |
| 492 | + if (denial) return denial; |
453 | 493 | const obj = body && typeof body === "object" && !Array.isArray(body) |
454 | 494 | ? body as Record<string, unknown> |
455 | 495 | : {}; |
@@ -496,11 +536,11 @@ async function tryXaiImageRelay( |
496 | 536 | } |
497 | 537 | if (!token) return xaiImageAuthMissing(); |
498 | 538 | logCtx.provider = "xai"; |
499 | | - logCtx.model = config.images?.bridgeModel ?? "grok-imagine-image-quality"; |
| 539 | + logCtx.model = bridgeModel; |
500 | 540 | const result = await callXaiImages( |
501 | 541 | { |
502 | 542 | prompt, |
503 | | - model: logCtx.model, |
| 543 | + model: bridgeModel, |
504 | 544 | n, |
505 | 545 | size, |
506 | 546 | quality, |
@@ -602,6 +642,7 @@ export async function handleImages( |
602 | 642 | endpoint: ImagesEndpoint, |
603 | 643 | logCtx: RequestLogContext, |
604 | 644 | turnAdmissionLease?: AdmissionLease, |
| 645 | + admission?: DataPlaneAdmission, |
605 | 646 | ): Promise<Response> { |
606 | 647 | let body: unknown; |
607 | 648 | try { |
@@ -616,7 +657,7 @@ export async function handleImages( |
616 | 657 | // Explicit images.provider owns the route, including its validation errors. |
617 | 658 | // Do not divert that selection to the xAI Imagine relay. |
618 | 659 | if (config.images?.provider === undefined) { |
619 | | - const xaiRelay = await tryXaiImageRelay(body, config, logCtx, req.signal, endpoint); |
| 660 | + const xaiRelay = await tryXaiImageRelay(body, config, logCtx, req.signal, endpoint, admission); |
620 | 661 | if (xaiRelay) return xaiRelay; |
621 | 662 | } |
622 | 663 | if (candidates.error) { |
@@ -641,7 +682,7 @@ export async function handleImages( |
641 | 682 | const canUseOpenAiForward = !skipOpenAiForwardForAdmissionBearer && candidates.forwardCandidates.length > 0; |
642 | 683 |
|
643 | 684 | if (!canUseOpenAiForward && !candidates.keyed) { |
644 | | - const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint); |
| 685 | + const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint, admission); |
645 | 686 | if (ccaResponse) return ccaResponse; |
646 | 687 | // 400, not 5xx: codex retries every 5xx up to 5 total attempts, and this is a permanent |
647 | 688 | // configuration state that must surface on the first attempt. |
@@ -688,7 +729,20 @@ export async function handleImages( |
688 | 729 |
|
689 | 730 | const headers: Record<string, string> = { "content-type": "application/json" }; |
690 | 731 | let url: string; |
| 732 | + // Both relay branches copy the body upstream, so the destination is the |
| 733 | + // provider chosen in that branch and the model the caller named. Each branch |
| 734 | + // is checked as it is entered, before it resolves a credential or commits a |
| 735 | + // key rotation, so a refused request spends nothing. |
| 736 | + const relaySelector = requestedImageSelector(body); |
691 | 737 | if (forward) { |
| 738 | + const denial = admissionScopeDenial(config, admission, relaySelector, { |
| 739 | + providerName: forward.providerName, |
| 740 | + modelId: relaySelector, |
| 741 | + }); |
| 742 | + if (denial) { |
| 743 | + forward.releaseProbeLease?.(); |
| 744 | + return denial; |
| 745 | + } |
692 | 746 | const { provider } = forward; |
693 | 747 | if (provider.headers) Object.assign(headers, provider.headers); |
694 | 748 | for (const [name, value] of forward.headers) headers[name] = value; |
@@ -697,13 +751,18 @@ export async function handleImages( |
697 | 751 | } else if (forwardAuthError) { |
698 | 752 | // Before surfacing the OpenAI auth failure, try CCA — the user may have a |
699 | 753 | // valid Google Antigravity login even though their OpenAI pool is broken. |
700 | | - const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint); |
| 754 | + const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint, admission); |
701 | 755 | if (ccaResponse) return ccaResponse; |
702 | 756 | // No CCA either: a configured OpenAI pool mode owns its authentication failure. |
703 | 757 | // Do not hide a broken/expired pool behind separately billed API-key image generation. |
704 | 758 | return forwardAuthError; |
705 | 759 | } else if (candidates.keyed) { |
706 | 760 | const { providerName } = candidates.keyed; |
| 761 | + const denial = admissionScopeDenial(config, admission, relaySelector, { |
| 762 | + providerName, |
| 763 | + modelId: relaySelector, |
| 764 | + }); |
| 765 | + if (denial) return denial; |
707 | 766 | // The keyed image path builds its own URL and Authorization header and never enters |
708 | 767 | // handleResponses, so the pre-dispatch key pick happens here. |
709 | 768 | // |
@@ -737,7 +796,7 @@ export async function handleImages( |
737 | 796 | url = `${provider.baseUrl.replace(/\/v1\/?$/, "")}/v1/images/${endpoint}`; |
738 | 797 | } else { |
739 | 798 | // No usable OpenAI credential — try CCA before giving up. |
740 | | - const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint); |
| 799 | + const ccaResponse = await tryCcaImageGeneration(body, config, logCtx, req.signal, endpoint, admission); |
741 | 800 | if (ccaResponse) return ccaResponse; |
742 | 801 | return formatErrorResponse( |
743 | 802 | 401, |
|