Skip to content

Commit 763cf9b

Browse files
committed
fix: reject unknown Anthropic model cursors
1 parent c1ed203 commit 763cf9b

8 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212

1313
- Responses API (HTTP, SSE, WebSocket): optional `phase` response override (`commentary` | `final_answer`) on assistant message output items; validated on load (#531)
1414

15+
### Changed
16+
17+
- **Authentication error compatibility:** configured inbound auth no longer returns the string `error.code: "invalid_api_key"` on Anthropic Messages/count and Gemini/Vertex Google HTTP paths. Anthropic omits `error.code`; check HTTP 401 or `error.type: "authentication_error"`. Google returns numeric `error.code: 401`; check HTTP 401 or `error.status: "UNAUTHENTICATED"`. Update clients and tests that read the old field. Shared `/v1/models` and WebSocket rejections keep their existing bodies.
18+
1519
### Fixed
1620

21+
- Anthropic model lists reject unknown `after_id` and `before_id` cursors with 400 `invalid_request_error`, including when a lifecycle filter would return an empty page. Valid cursors and OpenAI-style lists keep their existing paging behavior.
1722
- Shared `/v1/models` paths select the Anthropic catalog when `anthropic-version` is present, or when `x-api-key` is present without `Authorization`. Non-Anthropic clients that use `x-api-key` alone also receive that catalog. Use `Authorization` without `anthropic-version` to retain the OpenAI format.
1823
- Configured inbound authentication returns native 401 bodies on Anthropic Messages/count and Gemini/Vertex Google HTTP paths. Anthropic adds a `request-id` response header. Shared model paths and WebSocket rejections retain their existing bodies. Anthropic's upstream diagnostic message and body `request_id` differ. The observed Gemini gateway 403 differs from aimock's intentional inbound 401.
1924

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ await createServer(fixtures, { auth: { apiKeys: ["test-key"] } });
258258
AIMOCK_API_KEYS=test-key,rotated-key npx @copilotkit/aimock --config aimock.json
259259
```
260260

261-
Use `Authorization: Bearer <key>`, `Authorization: Key <key>`, `x-api-key`, `x-goog-api-key`, `api-key`, or `xi-api-key`. Every supplied credential must resolve to one configured key; mismatches return `401` with an OpenAI-compatible authentication error. HTTP routes, control APIs, mounts, and WebSocket upgrades are protected. Genuine CORS preflights plus `GET /health`, `GET /ready`, and `GET /metrics` remain public. This is inbound test access control, distinct from `record.providerKeys`; when enabled, proxying strips test credentials and requires a configured static provider credential before egress.
261+
Use `Authorization: Bearer <key>`, `Authorization: Key <key>`, `x-api-key`, `x-goog-api-key`, `api-key`, or `xi-api-key`. Every supplied credential must resolve to one configured key; mismatches return `401`. HTTP routes, control APIs, mounts, and WebSocket upgrades are protected. Genuine CORS preflights plus `GET /health`, `GET /ready`, and `GET /metrics` remain public. This is inbound test access control, distinct from `record.providerKeys`; when enabled, proxying strips test credentials and requires a configured static provider credential before egress.
262+
263+
**Auth error compatibility:** Anthropic Messages/count now omit the old `error.code: "invalid_api_key"`; check HTTP 401 or `error.type: "authentication_error"`. Gemini/Vertex Google HTTP paths replace that string with numeric `error.code: 401`; check HTTP 401 or `error.status: "UNAUTHENTICATED"`. Shared `/v1/models`, other HTTP paths, and WebSocket rejections keep their existing OpenAI-shaped error bodies. See the [Anthropic](docs/claude-messages/index.html#authentication-errors) and [Gemini](docs/gemini/index.html#authentication-errors) examples before updating client assertions.
262264

263265
### aimock-owned upstream keys — `AIMOCK_PROVIDER_*_KEY`
264266

‎docs/claude-messages/index.html‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,11 @@ <h2 id="model-catalog">Model Catalog</h2>
120120
page.
121121
</p>
122122
<p>
123-
Invalid limits and lifecycle values return 400 <code>invalid_request_error</code>. Unknown
124-
cursors return an empty page, with null first and last IDs. These error details and cursor
125-
results are local contracts. Authenticated upstream probes with unknown
126-
<code>after_id</code> and <code>before_id</code> returned 400
127-
<code>invalid_request_error</code>. aimock intentionally retains empty pages for those
128-
cursors.
123+
Invalid limits, lifecycle values, and unknown <code>after_id</code> or
124+
<code>before_id</code> cursors return 400 <code>invalid_request_error</code>. Unknown
125+
cursors return <code>Model with id &lt;id&gt; not found</code>, matching the status, error
126+
type, and message observed in authenticated upstream probes. aimock validates cursors
127+
before applying lifecycle filters, including filters that return an empty page.
129128
</p>
130129
<p>
131130
Each model has <code>type: "model"</code>, its ID as <code>display_name</code>,
@@ -184,6 +183,12 @@ <h2 id="authentication-errors">Authentication Errors</h2>
184183
<div class="code-block-header">Inbound rejection <span class="lang-tag">json</span></div>
185184
<pre><code>{"type":"error","error":{"type":"authentication_error","message":"Invalid API key"}}</code></pre>
186185
</div>
186+
<p>
187+
Compatibility change: these paths no longer return
188+
<code>error.code: "invalid_api_key"</code>. Check HTTP 401 or
189+
<code>error.type: "authentication_error"</code> instead. Update tests and clients that
190+
read the old field.
191+
</p>
187192
<p>
188193
Shared <code>/v1/models</code> paths retain their OpenAI-shaped authentication rejection,
189194
even with Anthropic headers. WebSocket authentication also retains its existing body.

‎docs/gemini/index.html‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ <h2 id="authentication-errors">Authentication Errors</h2>
184184
<div class="code-block-header">Inbound rejection <span class="lang-tag">json</span></div>
185185
<pre><code>{"error":{"code":401,"message":"Invalid API key","status":"UNAUTHENTICATED"}}</code></pre>
186186
</div>
187+
<p>
188+
Compatibility change: Gemini and Vertex Google HTTP paths replace the string
189+
<code>error.code: "invalid_api_key"</code> with the numeric
190+
<code>error.code: 401</code> and <code>error.status: "UNAUTHENTICATED"</code>. Check HTTP
191+
401 or the status string instead of the old code. Update tests and clients that read that
192+
field.
193+
</p>
187194
<p>
188195
A live unauthenticated Gemini count probe returned 403 <code>PERMISSION_DENIED</code>
189196
for an unregistered caller. aimock intentionally retains 401

‎src/__tests__/drift/models.drift.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,30 @@ const anthropicCatalogCases = [
117117
{
118118
id: "anthropic-after-unknown",
119119
query: "after_id=aimock-nonexistent-model",
120-
expected: { status: 200, body: emptyAnthropicPage },
120+
expected: {
121+
status: 400,
122+
body: {
123+
type: "error",
124+
error: {
125+
type: "invalid_request_error",
126+
message: "Model with id aimock-nonexistent-model not found",
127+
},
128+
},
129+
},
121130
},
122131
{
123132
id: "anthropic-before-unknown",
124133
query: "before_id=aimock-nonexistent-model",
125-
expected: { status: 200, body: emptyAnthropicPage },
134+
expected: {
135+
status: 400,
136+
body: {
137+
type: "error",
138+
error: {
139+
type: "invalid_request_error",
140+
message: "Model with id aimock-nonexistent-model not found",
141+
},
142+
},
143+
},
126144
},
127145
{
128146
id: "anthropic-lifecycle-invalid",

‎src/__tests__/response-model-features.test.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ describe("native model catalogs on the source server", () => {
139139
["claude-test-2"],
140140
true,
141141
],
142-
["unknown-after", "after_id=missing", [], false],
143-
["unknown-before", "before_id=missing", [], false],
144142
["retired", "lifecycle=retired", [], false],
145143
["deprecated", "lifecycle[]=deprecated", [], false],
144+
["retired-known-after", "lifecycle=retired&after_id=claude-test-0", [], false],
145+
["deprecated-known-before", "lifecycle[]=deprecated&before_id=claude-test-3", [], false],
146146
[
147147
"union",
148148
"limit=1&lifecycle=retired&lifecycle[]=active&lifecycle=deprecated&lifecycle[]=active",
@@ -160,6 +160,25 @@ describe("native model catalogs on the source server", () => {
160160
});
161161
});
162162

163+
it.each([
164+
"after_id=missing",
165+
"before_id=missing",
166+
"after_id=missing&before_id=claude-test-3",
167+
"after_id=claude-test-0&before_id=missing",
168+
"lifecycle=retired&after_id=missing",
169+
"lifecycle[]=deprecated&before_id=missing",
170+
])("Anthropic rejects unknown cursor in %s", async (query) => {
171+
const { status, body } = await get("anthropic-unknown-cursor:" + query, "/v1/models?" + query);
172+
expect(status).toBe(400);
173+
expect(body).toEqual({
174+
type: "error",
175+
error: {
176+
type: "invalid_request_error",
177+
message: "Model with id missing not found",
178+
},
179+
});
180+
});
181+
163182
it.each(["limit=0", "limit=1001", "limit=-1", "limit=1.5", "limit=bad", "lifecycle=bad"])(
164183
"Anthropic rejects %s natively",
165184
async (query) => {

‎src/openapi.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,12 +2508,12 @@ const SCHEMA_REFS: Record<string, OperationSchemas> = {
25082508
},
25092509
{
25102510
name: "after_id",
2511-
description: "Anthropic cursor after this visible ID",
2511+
description: "Anthropic cursor after this visible ID; unknown IDs return 400",
25122512
schema: { type: "string" },
25132513
},
25142514
{
25152515
name: "before_id",
2516-
description: "Anthropic cursor before this visible ID",
2516+
description: "Anthropic cursor before this visible ID; unknown IDs return 400",
25172517
schema: { type: "string" },
25182518
},
25192519
{

‎src/response/catalog/anthropic-page.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ export function anthropicPage(
2929
firstId: null,
3030
lastId: null,
3131
});
32-
if (lifecycle.length > 0 && !lifecycle.includes("active")) return { page: empty() };
3332
const after = query.get("after_id");
3433
const before = query.get("before_id");
3534
const afterIndex = after === null ? -1 : ids.indexOf(after);
3635
const beforeIndex = before === null ? ids.length : ids.indexOf(before);
37-
if ((after !== null && afterIndex === -1) || (before !== null && beforeIndex === -1))
38-
return { page: empty() };
36+
if (after !== null && afterIndex === -1) return { error: `Model with id ${after} not found` };
37+
if (before !== null && beforeIndex === -1) return { error: `Model with id ${before} not found` };
38+
if (lifecycle.length > 0 && !lifecycle.includes("active")) return { page: empty() };
3939
const available = ids.slice(afterIndex + 1, beforeIndex);
4040
// The SDK continues backward whenever before_id is present, retaining after_id.
4141
const selected = before !== null ? available.slice(-limit) : available.slice(0, limit);

0 commit comments

Comments
 (0)