From f2c262cf40798cd5e76706c5ac0d61c76c6041a3 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Fri, 20 Nov 2026 01:13:24 -0600 Subject: [PATCH 1/3] feat(model-proxy,bus,webui): settings-backend A3: the provider list comes from the cliproxy plugin's PROVIDERS (id, label, sign-in kind, the sign-in host for a subscription, the base URL for the key provider); the management client's oauthStart, the bus proxy page's names, login allow list, sign-in host check and key allow list read it, with the same refusals (proxy-client-invalid, provider-not-allowed); the setup status and the accounts answer carry providers as id, label and sign-in kind only; the Model setup page builds the provider select and its help text from them, and with no list, an empty one or a failed read it shows no select and says the providers could not be read; an architecture test holds that no provider id, sign-in host or key base URL is written out in client.mjs, proxy-page.mjs or display.js (#1873, lane settings-backend-a3) --- packages/bus/src/proxy-page.mjs | 44 +++++---- .../bus/tests/proxy-page-accounts.test.mjs | 5 +- .../bus/tests/proxy-page-providers.test.mjs | 91 +++++++++++++++++++ packages/model-proxy/src/admin-control.mjs | 5 +- packages/model-proxy/src/client.mjs | 12 ++- packages/model-proxy/tests/providers.test.mjs | 73 +++++++++++++++ .../plugins/model-provider/cliproxy/index.mjs | 12 +++ packages/webui/src/public/display.js | 45 ++++++--- .../webui/tests/proxy-architecture.test.mjs | 18 ++++ packages/webui/tests/proxy-browser.test.mjs | 41 ++++++++- .../webui/tests/proxy-setup-browser.test.mjs | 3 +- .../webui/tests/settings-s4-browser.test.mjs | 44 ++++++++- 12 files changed, 349 insertions(+), 44 deletions(-) create mode 100644 packages/bus/tests/proxy-page-providers.test.mjs create mode 100644 packages/model-proxy/tests/providers.test.mjs diff --git a/packages/bus/src/proxy-page.mjs b/packages/bus/src/proxy-page.mjs index 306fdd1b5..c7c90194d 100644 --- a/packages/bus/src/proxy-page.mjs +++ b/packages/bus/src/proxy-page.mjs @@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto'; import { isDeepStrictEqual } from 'node:util'; import { sha256, refuse } from '../../model-proxy/src/common.mjs'; import { PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs'; -import { ZAI_URL, MODEL_ID } from '../../plugins/model-provider/cliproxy/index.mjs'; +import { MODEL_ID, PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs'; export { PROXY_PAGE_VERBS, PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs'; const object = x => x !== null && typeof x === 'object' && !Array.isArray(x); function fields(args, names) { @@ -13,11 +13,14 @@ const priorityOk = n => Number.isInteger(n) && n >= -1000 && n <= 1000; const identifier = x => typeof x === 'string' && x.length > 0 && x.length <= 512; const stateOk = x => typeof x === 'string' && /^[a-zA-Z0-9_-]{1,512}$/.test(x); const keyId = value => sha256(value).slice(0, 12); -const providerName = provider => ({ claude: 'Claude', codex: 'Codex', 'z.ai': 'z.ai', other: 'Other' })[provider]; +// #1873 settings-backend-a3: provider names, the login allow list and the key allow list all come +// from the plugin's PROVIDERS. An account the list does not name reads Other. +const listed = (providers, id, signIn) => providers.find(p => p.id === id && p.signIn === signIn) ?? null; +const providerName = (providers, provider) => providers.find(p => p.id === provider)?.label ?? 'Other'; // Credential labels/emails and group names are arbitrary upstream strings. A stable // generated display name provides identification without reflecting a misplaced token. -function view(id, provider, status, priority) { - return { accountId: keyId(id), provider, displayName: `${providerName(provider)} account ${keyId(id)}`, +function view(providers, id, provider, status, priority) { + return { accountId: keyId(id), provider, displayName: `${providerName(providers, provider)} account ${keyId(id)}`, status, priority: priorityOk(priority) ? priority : 0 }; } // #1873 chat-fail F3: one plain line under an account the proxy reports as not working, with @@ -57,11 +60,12 @@ function accountReason(row, now) { return NO_REASON; } export class ProxyPage { - #client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #pending = new Map(); #writes = Promise.resolve(); - constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null }) { + #client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #providers; #pending = new Map(); #writes = Promise.resolve(); + // providers: the plugin's PROVIDERS; a test passes a copy with one taken out. + constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null, providers = PROVIDERS }) { if (!client || !/^[a-zA-Z0-9_-]{1,64}$/.test(instanceTag) || typeof keyRecords !== 'function' || typeof now !== 'function' - || typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0) throw new TypeError('invalid proxy page settings'); - this.#lock = lock; this.#org = org; + || typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0 || !Array.isArray(providers)) throw new TypeError('invalid proxy page settings'); + this.#lock = lock; this.#org = org; this.#providers = providers; this.#client = client; this.#tag = instanceTag; this.#records = keyRecords; this.#now = now; this.#timeout = loginTimeoutSeconds * 1000; this.#onChange = onChange; } // Serializes read-modify-write provider config within the bus. P2 owns the @@ -84,10 +88,10 @@ export class ProxyPage { const rows = []; for (const row of response.files) { if (!identifier(row?.id) || !identifier(row?.name)) continue; - const provider = ['claude', 'codex'].includes(row.provider) ? row.provider : 'other'; + const provider = listed(this.#providers, row.provider, 'oauth') ? row.provider : 'other'; const status = row.disabled === true ? 'disabled' : row.unavailable === true ? 'unavailable' : ['ready', 'active', 'error', 'disabled', 'unavailable'].includes(row.status) ? row.status : 'unknown'; - rows.push({ ...view(row.id, provider, status, row.priority), source: 'oauth', name: row.name, + rows.push({ ...view(this.#providers, row.id, provider, status, row.priority), source: 'oauth', name: row.name, ...(['unavailable', 'error'].includes(status) ? { reason: accountReason(row, this.#now()) } : {}) }); } for (const [groupIndex, group] of groups.entries()) { @@ -95,18 +99,18 @@ export class ProxyPage { for (const [keyIndex, key] of group.keys.entries()) { // auth_index is generated by the proxy. Never fall back to hashing api-key. if (!identifier(key?.auth_index)) continue; - const provider = group['base-url'] === ZAI_URL ? 'z.ai' : 'other'; + const provider = this.#providers.find(p => p.signIn === 'apikey' && p.baseUrl === group['base-url'])?.id ?? 'other'; const status = key.disabled === true || group.disabled === true ? 'disabled' : 'active'; const configured = group.models ?? []; if (!Array.isArray(configured) || configured.length > 32 || configured.some(m => !m || typeof (m.alias || m.name) !== 'string' || !MODEL_ID.test(m.alias || m.name))) refuse('proxy-down'); - rows.push({ ...view(key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) }); + rows.push({ ...view(this.#providers, key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) }); } } return { rows, groups }; } #public(row) { const { accountId, provider, displayName, status, priority } = row; - return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}), + return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && listed(this.#providers, provider, 'apikey') ? { models: row.models } : {}), ...(typeof row.reason === 'string' ? { reason: row.reason } : {}) }; } #session(state, scope, { reportTimeout = false } = {}) { @@ -131,17 +135,18 @@ export class ProxyPage { async #request(verb, args, scope) { if (verb === 'proxy.accounts') { fields(args, []); - return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)) }; + // The provider list rides along for the page's select: id, label and sign-in kind only. + return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)), providers: publicProviders(this.#providers) }; } if (verb === 'proxy.login.start') { fields(args, ['provider']); - if (!['claude', 'codex'].includes(args.provider)) refuse('provider-not-allowed'); + const oauth = listed(this.#providers, args.provider, 'oauth'); + if (!oauth) refuse('provider-not-allowed'); for (const [state, value] of this.#pending) if (value.expires <= this.#now()) this.#pending.delete(state); if (this.#pending.size >= 128) refuse('proxy-down'); const answer = await (this.#lock ? this.#write(() => this.#client.oauthStart(args.provider)) : this.#client.oauthStart(args.provider)); let url; try { url = new URL(answer.url); } catch { refuse('proxy-down'); } - const host = args.provider === 'claude' ? 'claude.ai' : 'auth.openai.com'; - if (url.protocol !== 'https:' || url.hostname !== host || url.username || url.password || url.port + if (url.protocol !== 'https:' || typeof oauth.signInHost !== 'string' || url.hostname !== oauth.signInHost || url.username || url.password || url.port || url.href.length > 8192 || !stateOk(answer.state) || url.searchParams.getAll('state').length !== 1 || url.searchParams.get('state') !== answer.state || this.#pending.has(answer.state)) refuse('proxy-down'); this.#pending.set(answer.state, { business: scope.business, human: scope.human, expires: this.#now() + this.#timeout }); @@ -186,12 +191,13 @@ export class ProxyPage { } if (verb === 'proxy.apikey.add') { fields(args, ['provider', 'key']); - if (args.provider !== 'z.ai') refuse('provider-not-allowed'); + const keyed = listed(this.#providers, args.provider, 'apikey'); + if (!keyed || typeof keyed.baseUrl !== 'string') refuse('provider-not-allowed'); if (typeof args.key !== 'string' || !/^[\x21-\x7e]{1,4096}$/.test(args.key)) refuse('apikey-invalid'); return this.#write(async () => { const groups = await this.#client.getUpstreamGroups(); if (!Array.isArray(groups)) refuse('proxy-down'); - const group = { name: `mosaic-zai-${randomBytes(6).toString('hex')}`, 'base-url': ZAI_URL, + const group = { name: `mosaic-${keyed.id.replace(/[^a-z0-9]/g, '')}-${randomBytes(6).toString('hex')}`, 'base-url': keyed.baseUrl, priority: 0, keys: [{ 'api-key': args.key }] }; if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), groups)) refuse('proxy-down'); this.#beforeMutation('proxy.apikey.add'); diff --git a/packages/bus/tests/proxy-page-accounts.test.mjs b/packages/bus/tests/proxy-page-accounts.test.mjs index df9a6558f..fcefbf292 100644 --- a/packages/bus/tests/proxy-page-accounts.test.mjs +++ b/packages/bus/tests/proxy-page-accounts.test.mjs @@ -4,6 +4,7 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; import { fakeManagement } from './proxy-page-fixture.mjs'; +import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs'; const id = value => createHash('sha256').update(value).digest('hex').slice(0, 12); const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: 'p4-private-label', email: 'p4-private-email', token: 'p4-private-token', status: 'ready', priority: 3, ...extra }); @@ -11,7 +12,9 @@ test('accounts have exactly the allowlisted public fields, id from credential id const s = await fakeManagement(t, { files: [oauth()] }); const result = await s.call('proxy.accounts'); assert.deepEqual(result, { accounts: [{ accountId: id('oauth-one.json'), provider: 'claude', - displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }] }); + displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }], + // settings-backend-a3: the provider list rides along, id, label and sign-in kind only. + providers: PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })) }); for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token']) { assert.equal(JSON.stringify(result).includes(value), false); assert.notEqual(result.accounts[0].accountId, id(value)); diff --git a/packages/bus/tests/proxy-page-providers.test.mjs b/packages/bus/tests/proxy-page-providers.test.mjs new file mode 100644 index 000000000..16c296452 --- /dev/null +++ b/packages/bus/tests/proxy-page-providers.test.mjs @@ -0,0 +1,91 @@ +// #1873 settings-backend-a3: the proxy page's provider names, login allow list, sign-in host and key +// allow list all come from the plugin's PROVIDERS. A provider a test copy of the list leaves out is +// refused at each door with the same code as before; one a copy adds is let through. The accounts +// answer carries the list as id, label and sign-in kind only. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { ProxyPage } from '../src/proxy-page.mjs'; +import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs'; +const owner = { business: 'demo', human: 'owner' }; +function setup(options = {}) { + const calls = [], state = { files: [], groups: [] }; + const client = Object.fromEntries(['getCredentials', 'oauthStart', 'oauthCallback', 'oauthStatus', 'oauthCancel', 'getUpstreamGroups', 'putUpstreamGroups'].map(name => [name, async (...args) => { + calls.push([name, ...args]); + if (name === 'oauthStart') return { status: 'ok', state: `s-${calls.length}`, url: `https://${options.hosts?.[args[0]] ?? 'nowhere.invalid'}/oauth/authorize?state=s-${calls.length}` }; + if (name === 'getCredentials') return { files: state.files }; + if (name === 'getUpstreamGroups') return structuredClone(state.groups); + if (name === 'putUpstreamGroups') { state.groups = args[0].map((g, i) => ({ ...g, keys: g.keys.map((k, j) => ({ ...k, auth_index: `idx-${i}-${j}` })) })); return { status: 'ok' }; } + return { status: 'ok' }; + }])); + const p = new ProxyPage({ client, instanceTag: 'fixture', ...(options.providers ? { providers: options.providers } : {}) }); + return { calls, state, call: (verb, args = {}) => p.request(verb, args, owner) }; +} +const hosts = Object.fromEntries(PROVIDERS.filter(p => p.signIn === 'oauth').map(p => [p.id, p.signInHost])); +const door = p => p.signIn === 'oauth' ? ['proxy.login.start', { provider: p.id }] : ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }]; +const otherDoor = p => p.signIn === 'oauth' ? ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }] : ['proxy.login.start', { provider: p.id }]; + +test('a3: each provider in PROVIDERS passes its own door and is refused at the other with provider-not-allowed', async () => { + assert.ok(PROVIDERS.some(p => p.signIn === 'oauth') && PROVIDERS.some(p => p.signIn === 'apikey')); + for (const p of PROVIDERS) { + const s = setup({ hosts }); + const answer = await s.call(...door(p)); + if (p.signIn === 'oauth') assert.equal(new URL(answer.url).hostname, p.signInHost, p.id); + else { assert.deepEqual(answer, { added: true }); assert.equal(s.state.groups.at(-1)['base-url'], p.baseUrl); } + const before = s.calls.length; + await assert.rejects(s.call(...otherDoor(p)), { code: 'provider-not-allowed' }, p.id); + assert.equal(s.calls.length, before, `${p.id}: a refusal reaches no proxy call`); + } + for (const [verb, args] of [['proxy.login.start', { provider: 'other' }], ['proxy.apikey.add', { provider: 'other', key: 'fixture-key' }]]) { + const s = setup({ hosts }); + await assert.rejects(s.call(verb, args), { code: 'provider-not-allowed' }); assert.equal(s.calls.length, 0); + } +}); + +test('a3: a provider taken out of a test copy of the list is refused at its door, reads Other and leaves the providers answer', async () => { + for (const gone of PROVIDERS) { + const s = setup({ hosts, providers: PROVIDERS.filter(p => p !== gone) }); + await assert.rejects(s.call(...door(gone)), { code: 'provider-not-allowed' }, gone.id); + assert.equal(s.calls.length, 0, `${gone.id}: refused before any proxy call`); + if (gone.signIn === 'oauth') s.state.files = [{ id: 'one.json', name: 'one.json', provider: gone.id, status: 'ready' }]; + else s.state.groups = [{ name: 'g', 'base-url': gone.baseUrl, keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }]; + const answer = await s.call('proxy.accounts'); + assert.equal(answer.accounts.length, 1); + assert.equal(answer.accounts[0].provider, 'other', gone.id); + assert.match(answer.accounts[0].displayName, /^Other account /); + assert.equal(Object.hasOwn(answer.accounts[0], 'models'), false, `${gone.id}: an unlisted group shows no models`); + assert.deepEqual(answer.providers.map(p => p.id), PROVIDERS.filter(p => p !== gone).map(p => p.id)); + } +}); + +test('a3: a provider a test copy adds is let through: key group from its base URL, sign-in on its host, name from its label', async () => { + const extra = [{ id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: 'https://api.fixture.invalid/v1' }, + { id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }]; + const s = setup({ providers: [...PROVIDERS, ...extra], hosts: { ...hosts, 'fixture-sso': 'sso.fixture.invalid' } }); + assert.deepEqual(await s.call('proxy.apikey.add', { provider: 'fixture.ai', key: 'fixture-key' }), { added: true }); + assert.equal(s.state.groups[0]['base-url'], 'https://api.fixture.invalid/v1'); + assert.match(s.state.groups[0].name, /^mosaic-fixtureai-[0-9a-f]{12}$/); + assert.equal(new URL((await s.call('proxy.login.start', { provider: 'fixture-sso' })).url).hostname, 'sso.fixture.invalid'); + s.state.files = [{ id: 'sso.json', name: 'sso.json', provider: 'fixture-sso', status: 'ready' }]; + const { accounts } = await s.call('proxy.accounts'); + assert.deepEqual(accounts.map(a => [a.provider, a.displayName.replace(/ [0-9a-f]{12}$/, '')]), [['fixture-sso', 'Fixture SSO account'], ['fixture.ai', 'Fixture AI account']]); + assert.deepEqual(accounts[1].models, []); +}); + +test('a3: the sign-in address must be on the host the list names for that provider', async () => { + const codex = PROVIDERS.find(p => p.id === 'codex'); + const moved = PROVIDERS.map(p => p === codex ? { ...p, signInHost: 'elsewhere.invalid' } : p); + const s = setup({ hosts, providers: moved }); + await assert.rejects(s.call('proxy.login.start', { provider: 'codex' }), { code: 'proxy-down' }); + const t = setup({ hosts: { ...hosts, codex: 'elsewhere.invalid' }, providers: moved }); + assert.equal(new URL((await t.call('proxy.login.start', { provider: 'codex' })).url).hostname, 'elsewhere.invalid'); +}); + +test('a3: the accounts answer carries the providers as id, label and sign-in kind only, no URL, host or key', async () => { + const s = setup({ hosts }); + await s.call('proxy.apikey.add', { provider: 'z.ai', key: 'a3-canary-key-value' }); + const answer = await s.call('proxy.accounts'); + assert.deepEqual(answer.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn }))); + for (const p of answer.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']); + const text = JSON.stringify(answer.providers); + for (const word of ['a3-canary-key-value', 'https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word); +}); diff --git a/packages/model-proxy/src/admin-control.mjs b/packages/model-proxy/src/admin-control.mjs index e3c6ccd65..1f0d242f1 100644 --- a/packages/model-proxy/src/admin-control.mjs +++ b/packages/model-proxy/src/admin-control.mjs @@ -11,6 +11,7 @@ import { releaseState, initState, fenceState, initLockStatus, clearInitLock, app import { secretValues } from './redact.mjs'; import { discoverModules, hasModule } from '../../plugins/src/loader.mjs'; import { readPluginLock } from '../../plugins/src/pinned.mjs'; +import { PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs'; export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback); const failure = code => Object.assign(new Error(code), { code }); @@ -79,7 +80,7 @@ const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9] // reads it ({document, revision}), read only. `harnesses()` lists the installed harness programs. // `rootAgents()` reads the root agent file (loadRootAgents); with none given, or one that throws, // no organization that has the program on is written (unreadable). -export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) { +export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN, providers = PROVIDERS }) { const port = () => { try { return initState(configDir, dataRoot).settings?.port ?? null; } catch { return null; } }; const control = unit ?? createUnitControl({ instance, unitDir, port, env }); let job = { state: 'idle', step: null, receipt: null, error: null }; @@ -273,6 +274,8 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir initLock: lock, fetch: fetchJob, restart: restarted ? { confirmed: true, observedAt: restarted.observedAt, pid: restarted.after.pid } : null, + // #1873 settings-backend-a3: the page's provider select, from the plugin. Id, label and sign-in kind only. + providers: publicProviders(providers), busy, receipts: recentReceipts(dataRoot), organizations, diff --git a/packages/model-proxy/src/client.mjs b/packages/model-proxy/src/client.mjs index f3cdbf996..fed4d319c 100644 --- a/packages/model-proxy/src/client.mjs +++ b/packages/model-proxy/src/client.mjs @@ -3,17 +3,19 @@ import http from 'node:http'; import { ProxyError, refuse, sha256, regularBytes, validManagementKey } from './common.mjs'; import { lstatSync } from 'node:fs'; import { join } from 'node:path'; +import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs'; const KEY_PATH = '/v8/management/config/access/api-keys'; const validKeys = value => Array.isArray(value) && value.length <= 100000 && value.every(k => typeof k === 'string' && k.length > 0 && k.length <= 4096 && !/[\r\n\x00]/.test(k)); export class ManagementClient { - #base; #key; #receipt; #timeout; - constructor({ baseUrl, key, receipt = () => {}, timeout = 5000 }) { + #base; #key; #receipt; #timeout; #providers; + // providers: the plugin's PROVIDERS; a test passes a copy with one taken out. + constructor({ baseUrl, key, receipt = () => {}, timeout = 5000, providers = PROVIDERS }) { let u; try { u = new URL(baseUrl); } catch { refuse('proxy-client-invalid'); } if (u.protocol !== 'http:' || u.hostname !== '127.0.0.1' || !u.port || Number(u.port) < 1024 || u.port === '8317' || u.username || u.password || u.pathname !== '/' || u.search || u.hash || !validManagementKey(key) - || typeof receipt !== 'function' || !Number.isSafeInteger(timeout) || timeout <= 0) refuse('proxy-client-invalid'); - this.#base = u.origin; this.#key = key; this.#receipt = receipt; this.#timeout = timeout; + || typeof receipt !== 'function' || !Number.isSafeInteger(timeout) || timeout <= 0 || !Array.isArray(providers)) refuse('proxy-client-invalid'); + this.#base = u.origin; this.#key = key; this.#receipt = receipt; this.#timeout = timeout; this.#providers = providers; } #request(method, endpoint, payload, onStatus) { // node:http addresses the fixed numeric loopback directly. An ambient fetch @@ -107,7 +109,7 @@ export class ManagementClient { } patchCredential(name, priority) { return this.#page('PATCH', '/v8/management/credentials/fields', { name, priority }, 200); } oauthStart(provider) { - if (!['claude', 'codex'].includes(provider)) refuse('proxy-client-invalid'); + if (!this.#providers.some(p => p.id === provider && p.signIn === 'oauth')) refuse('proxy-client-invalid'); return this.#page('GET', `/v8/management/oauth/auth-url?provider=${provider}`); } oauthCallback(redirect_url) { return this.#page('POST', '/v8/management/oauth/callback', { redirect_url }); } diff --git a/packages/model-proxy/tests/providers.test.mjs b/packages/model-proxy/tests/providers.test.mjs new file mode 100644 index 000000000..7078177df --- /dev/null +++ b/packages/model-proxy/tests/providers.test.mjs @@ -0,0 +1,73 @@ +// #1873 settings-backend-a3: the management client's sign-in allow list and the setup status's +// provider field both come from the plugin's PROVIDERS. oauthStart refuses the key provider and any +// provider a test copy of the list leaves out, before any request. The status field carries id, +// label and sign-in kind only: no base URL, no sign-in host, no key text. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import http from 'node:http'; +import { join } from 'node:path'; +import { tmpdir } from 'node:os'; +import { ManagementClient } from '../src/client.mjs'; +import { createProxyAdmin } from '../src/admin-control.mjs'; +import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs'; +const code = name => error => error.code === name; + +async function server(t) { + const asked = []; + const s = http.createServer((q, r) => { asked.push(new URL(q.url, 'http://x').searchParams.get('provider')); r.setHeader('content-type', 'application/json'); r.end(JSON.stringify({ status: 'ok', state: 's', url: 'https://claude.ai/x?state=s' })); }); + await new Promise(r => s.listen(0, '127.0.0.1', r)); t.after(() => new Promise(r => s.close(r))); + return { asked, baseUrl: `http://127.0.0.1:${s.address().port}` }; +} + +test('a3: oauthStart asks the proxy for each sign-in provider in PROVIDERS and refuses the key provider before any request', async t => { + const { asked, baseUrl } = await server(t); + const c = new ManagementClient({ baseUrl, key: 'a3-management' }); + for (const p of PROVIDERS.filter(p => p.signIn === 'oauth')) await c.oauthStart(p.id); + assert.deepEqual(asked, PROVIDERS.filter(p => p.signIn === 'oauth').map(p => p.id)); + const keyed = PROVIDERS.filter(p => p.signIn === 'apikey'); + assert.ok(keyed.length > 0); + for (const p of keyed) await assert.rejects(async () => c.oauthStart(p.id), code('proxy-client-invalid'), p.id); + for (const id of ['other', '', 'claude&x=1', undefined]) await assert.rejects(async () => c.oauthStart(id), code('proxy-client-invalid')); + assert.equal(asked.length, PROVIDERS.filter(p => p.signIn === 'oauth').length, 'no refused provider reached the proxy'); +}); + +test('a3: oauthStart refuses a sign-in provider a test copy of the list leaves out, and asks for one it adds', async t => { + const { asked, baseUrl } = await server(t); + for (const gone of PROVIDERS.filter(p => p.signIn === 'oauth')) { + const c = new ManagementClient({ baseUrl, key: 'a3-management', providers: PROVIDERS.filter(p => p !== gone) }); + await assert.rejects(async () => c.oauthStart(gone.id), code('proxy-client-invalid'), gone.id); + } + assert.deepEqual(asked, []); + const c = new ManagementClient({ baseUrl, key: 'a3-management', providers: [...PROVIDERS, { id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }] }); + await c.oauthStart('fixture-sso'); + assert.deepEqual(asked, ['fixture-sso']); + assert.throws(() => new ManagementClient({ baseUrl, key: 'a3-management', providers: null }), code('proxy-client-invalid')); +}); + +function admin(t, extra = {}) { + const root = fs.mkdtempSync(join(tmpdir(), 'a3-')); + t.after(() => fs.rmSync(root, { recursive: true, force: true })); + const configDir = join(root, 'config'), dataRoot = join(root, 'data'); + fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 }); + const unit = { unit: 'mosaic-proxy-a3.service', status: async () => ({ unit: 'mosaic-proxy-a3.service', installed: false, active: 'inactive', healthy: false }) }; + return createProxyAdmin({ dataRoot, configDir, unit, buses: async () => ({ stopped: true, running: [] }), fetch: async () => ({}), now: () => new Date(0), + orgs: async () => [], harnesses: () => [], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }), ...extra }); +} + +test('a3: the status lists the providers as id, label and sign-in kind only, in the plugin\'s order', async t => { + const status = await admin(t).status(); + assert.deepEqual(status.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn }))); + for (const p of status.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']); + const text = JSON.stringify(status.providers); + for (const word of ['https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word); +}); + +test('a3: the status field holds no key text, even from a list entry that carries a key, and a provider left out is gone', async t => { + const canary = 'a3-status-canary-key-0123456789'; + const list = [...PROVIDERS.filter(p => p.id !== 'codex'), { id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: `https://${canary}.invalid/`, key: canary, apiKey: canary }]; + const status = await admin(t, { providers: list }).status(); + assert.deepEqual(status.providers.map(p => p.id), ['claude', 'z.ai', 'fixture.ai']); + assert.equal(JSON.stringify(status.providers).includes(canary), false); + assert.equal(JSON.stringify(status).includes(canary), false); +}); diff --git a/packages/plugins/model-provider/cliproxy/index.mjs b/packages/plugins/model-provider/cliproxy/index.mjs index 68cec91a6..e191db620 100644 --- a/packages/plugins/model-provider/cliproxy/index.mjs +++ b/packages/plugins/model-provider/cliproxy/index.mjs @@ -1,6 +1,18 @@ // Provider configuration and transport declarations owned by the proxy module. export const interfaceVersion = 1; export const ZAI_URL = 'https://api.z.ai/api/anthropic'; +// #1873 settings-backend-a3: the one list of providers an account can be added for. The bus proxy +// page, the management client and the Console's provider select all read it; a provider not here +// is refused. `id` is the value the page sends and an account row carries. An OAuth provider names +// the host its sign-in address must be on; the key provider names the base URL its key group uses. +// The setup status and the accounts read pass on only id, label and sign-in kind. +export const PROVIDERS = Object.freeze([ + Object.freeze({ id: 'claude', label: 'Claude', signIn: 'oauth', signInHost: 'claude.ai' }), + Object.freeze({ id: 'codex', label: 'ChatGPT / Codex', signIn: 'oauth', signInHost: 'auth.openai.com' }), + Object.freeze({ id: 'z.ai', label: 'z.ai', signIn: 'apikey', baseUrl: ZAI_URL }), +]); +// What a page may see of the list: no URL, no host, nothing else. +export const publicProviders = (list = PROVIDERS) => list.map(({ id, label, signIn }) => ({ id, label, signIn })); export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/; const MODEL = MODEL_ID; const MAX_MODELS = 10000; diff --git a/packages/webui/src/public/display.js b/packages/webui/src/public/display.js index 1cd772693..d91b3ea5b 100644 --- a/packages/webui/src/public/display.js +++ b/packages/webui/src/public/display.js @@ -603,13 +603,13 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo + '
Account order

Higher priority is used first. Credential values are never shown.

    ' + '
    Models offered
    ' + '
    Add an account
    ' - + '
    ' - + '' - + '

    Claude and ChatGPT / Codex: you sign in on the provider\'s site in a new tab. Nothing you type there comes to this page. z.ai: you paste an API key from your z.ai account.

    ' - + '
    ' - + '

    ' + '
    ' + '