diff --git a/bin/clef-proxy.mjs b/bin/clef-proxy.mjs index e0c13f901..18b288cd4 100755 --- a/bin/clef-proxy.mjs +++ b/bin/clef-proxy.mjs @@ -96,6 +96,17 @@ const getCredentials = (authHeader) => { return { accountId, gatewayId, token }; }; +// The resolved model is interpolated raw into the gateway run URL, so it +// must be confined to the Clef family in the cloudflare namespace with +// plain name characters only (#2410): no '/', no dot segments, no '?', +// '#', whitespace, or control characters are representable in this +// grammar, so a hostile model cannot rewrite the upstream path/query or +// spend the proxy's token on a non-Clef model. This is a deterministic +// grammar check, deliberately NOT a gate on the live model list (that +// list is async/TTL'd display machinery for /v1/models and must not +// become a data-path dependency). +const CLEF_MODEL_GRAMMAR = /^@cf\/cloudflare\/clef[A-Za-z0-9._-]*$/; + const resolveModel = (model) => { if (!model || typeof model !== 'string') return '@cf/cloudflare/clef-flash'; if (model.startsWith('@cf/')) return model; @@ -228,6 +239,11 @@ const server = http.createServer(async (req, res) => { } const model = resolveModel(body.model); + if (!CLEF_MODEL_GRAMMAR.test(model)) { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ error: `Refused model '${model}': only Clef-family models (@cf/cloudflare/clef*) are served by this proxy` })); + return; + } const cfUrl = `${GATEWAY_BASE}/${accountId}/${gatewayId}/workers-ai/run/${model}`; const payload = { diff --git a/bin/clef-proxy.test.mjs b/bin/clef-proxy.test.mjs index 219a4c0f3..923008921 100644 --- a/bin/clef-proxy.test.mjs +++ b/bin/clef-proxy.test.mjs @@ -230,3 +230,66 @@ test('clef-proxy config cache contract (#782)', async (t) => { assert.equal(seenAuth.length, callsBefore); }); }); + +// Model grammar (#2410): the resolved model is interpolated raw into the +// gateway run URL, so anything outside the Clef family grammar +// (^@cf/cloudflare/clef[A-Za-z0-9._-]*$) must be refused with a 400 +// BEFORE any upstream fetch, and Clef spellings must reach the stub at +// the exact expected run path. +test('clef-proxy model grammar (#2410)', async (t) => { + const stubPaths = []; + const stub = http.createServer((req, res) => { + stubPaths.push(req.url); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ success: true, result: { answers: { q1: 'answer one' } } })); + }); + const stubPort = await freePort(); + await new Promise((resolve) => stub.listen(stubPort, '127.0.0.1', resolve)); + t.after(() => { stub.close(); }); + + const { child, base } = await startProxy({ + CLEF_PROXY_GATEWAY_BASE: `http://127.0.0.1:${stubPort}/v1`, + NO_PROXY: '127.0.0.1,localhost', + no_proxy: '127.0.0.1,localhost' + }); + t.after(() => { child.kill('SIGTERM'); }); + + const ask = (model) => fetch(`${base}/v1/systemone`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' }, + body: JSON.stringify({ state: {}, questions: { q1: 'question one' }, ...(model === undefined ? {} : { model }) }) + }); + + const hostile = [ + '@cf/meta/llama-3.1-8b-instruct', + '@cf/cloudflare/../../ai/models/search', + 'clef/../../../../client/v4/accounts/OTHER/ai/run/x', + '@cf/cloudflare/clef?per_page=1#frag', + 'my-clef-thing' + ]; + for (const model of hostile) { + await t.test(`hostile model is refused 400 with no upstream call: ${model}`, async () => { + const callsBefore = stubPaths.length; + const res = await ask(model); + assert.equal(res.status, 400); + const data = await res.json(); + assert.match(data.error, /Refused model/); + assert.equal(stubPaths.length, callsBefore); + }); + } + + const controls = [ + [undefined, '@cf/cloudflare/clef-flash'], + ['clef', '@cf/cloudflare/clef'], + ['clef-flash', '@cf/cloudflare/clef-flash'], + ['@cf/cloudflare/clef', '@cf/cloudflare/clef'], + ['@cf/cloudflare/clef-omni', '@cf/cloudflare/clef-omni'] + ]; + for (const [model, resolved] of controls) { + await t.test(`control model reaches the stub at the exact run path: ${model}`, async () => { + const res = await ask(model); + assert.equal(res.status, 200); + assert.ok(stubPaths.at(-1).endsWith(`/workers-ai/run/${resolved}`), stubPaths.at(-1)); + }); + } +});