From e6c8e1981ae344f3be36faf08a047bc7a285b0a2 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 04:17:13 -0600 Subject: [PATCH] fix(catalog-sync): validate proxy model response shape (#2827) --- bin/provider-catalog-sync.mjs | 25 ++++++++++++++-- bin/provider-catalog-sync.test.mjs | 47 ++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/bin/provider-catalog-sync.mjs b/bin/provider-catalog-sync.mjs index f0ba925a4f..77c8923527 100644 --- a/bin/provider-catalog-sync.mjs +++ b/bin/provider-catalog-sync.mjs @@ -440,14 +440,35 @@ async function fetchJson(url, timeoutMs = 15000) { return res.json(); } +function modelIdsFromProxyResponse(data) { + if (!data || typeof data !== 'object' || Array.isArray(data)) { + throw new Error('response body must be a non-null object'); + } + if (!Object.prototype.hasOwnProperty.call(data, 'data')) { + throw new Error('response body is missing data'); + } + if (!Array.isArray(data.data)) { + throw new Error('response data must be an array'); + } + return data.data.map((model, index) => { + if (!model || typeof model !== 'object' || Array.isArray(model)) { + throw new Error(`response data[${index}] must be an object`); + } + if (typeof model.id !== 'string' || model.id.trim().length === 0) { + throw new Error(`response data[${index}].id must be a non-empty string`); + } + return model.id; + }); +} + async function enumerateProxy(port, sourceDir) { if (sourceDir) { const file = path.join(sourceDir, `${port}.json`); const data = JSON.parse(fs.readFileSync(file, 'utf8')); - return (data.data ?? []).map((m) => m.id); + return modelIdsFromProxyResponse(data); } const data = await fetchJson(`http://127.0.0.1:${port}/v1/models`, 10000); - return (data.data ?? []).map((m) => m.id); + return modelIdsFromProxyResponse(data); } async function loadModelsDev(spec) { diff --git a/bin/provider-catalog-sync.test.mjs b/bin/provider-catalog-sync.test.mjs index 5fd6916654..b3134e24ab 100644 --- a/bin/provider-catalog-sync.test.mjs +++ b/bin/provider-catalog-sync.test.mjs @@ -1,5 +1,8 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; import { parseJsonc, findModelsSpan, @@ -12,6 +15,7 @@ import { catalogSyncBranch, assertNotDefaultBranch, openPrCommands, + runSync, } from './provider-catalog-sync.mjs'; const FIXTURE = `{ @@ -42,6 +46,49 @@ const FIXTURE = `{ } `; +const runSnapshot = async (snapshot) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'provider-catalog-sync-')); + const sourceDir = path.join(dir, 'sources'); + fs.mkdirSync(sourceDir); + const configPath = path.join(dir, 'config.jsonc'); + fs.writeFileSync(configPath, JSON.stringify({ + provider: { + 'claude-code': { models: { 'model-a': {}, 'model-b': {} } }, + }, + })); + fs.writeFileSync(path.join(sourceDir, '18749.json'), JSON.stringify(snapshot)); + try { + return await runSync({ configPath, modelsDevSpec: null, sourceDir }); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +}; + +for (const [name, snapshot] of [ + ['missing data', { unexpected: [] }], + ['non-array data', { data: 'oops' }], + ['entry without an id', { data: [{ name: 'no-id' }] }], + ['null entry', { data: [null] }], +]) { + test(`runSync marks proxy snapshot with ${name} as not-enumerable`, async () => { + const { report } = await runSnapshot(snapshot); + const entry = report.providers['claude-code']; + assert.equal(entry.status, 'not-enumerable'); + assert.match(entry.note, /proxy :18749 enumeration failed:/); + assert.equal(entry.live_count, undefined); + assert.equal(entry.vanishings_basis, undefined); + }); +} + +test('runSync preserves the valid proxy snapshot report', async () => { + const { report } = await runSnapshot({ data: [{ id: 'model-a' }, { id: 'model-b' }] }); + const entry = report.providers['claude-code']; + assert.equal(entry.live_count, 2); + assert.deepEqual(entry.additions_validated, []); + assert.deepEqual(entry.vanishings, []); + assert.equal(entry.vanishings_basis, 'live'); +}); + test('parseJsonc strips comments and trailing commas', () => { const cfg = parseJsonc(FIXTURE); assert.deepEqual(Object.keys(cfg.provider), ['proxy-a', 'name-only-b']);