Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker/setup/gateways.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const cleanManagementKey = value => {
};
const allowPrivate = input => (input?.allowPrivateHost ?? input?.gateway?.allowPrivateHost) === true;
const requireHostConsent = result => {
if (result?.code === 'metadata-host') throw new GatewayError(result.message, 409, 'metadata-host', { field: 'url', hint: result.hint });
if (result?.code === 'private-host') throw new GatewayError(result.message, 409, 'private-host', { field: 'url', confirmHost: result.confirmHost, hint: result.hint });
};
const cleanAddress = (value, allowPrivateHost = false) => {
Expand Down
4 changes: 3 additions & 1 deletion docker/setup/gateways/adapters.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ const SPECS = Object.freeze({
config: (gateway, models, base, files, capabilities) => {
const id = providerId(gateway), key = name => `model_providers.${id}.${name}`;
// Single-quoted for T3's tokenizer, double-quoted inside so Codex reads TOML strings.
// Escape for TOML basic strings, so a quote or backslash in the URL cannot break out.
const tomlValue = value => String(value).replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const overrides = [['model_provider', id], [key('name'), gateway.id], [key('base_url'), `${gateway.url}/v1`], [key('env_key'), GATEWAY_KEY_ENV], [key('wire_api'), 'responses']];
if (hasCodexCatalog(gateway, models)) overrides.push(['model_catalog_json', files.config]);
// Codex needs the key; the shells it opens don't. Without this it also lands
Expand All @@ -214,7 +216,7 @@ const SPECS = Object.freeze({
binaryPath: base.binaryPath,
homePath: base.homePath,
shadowHomePath: base.shadowHomePath,
launchArgs: [base.launchArgs, ...overrides.map(([name, value]) => `-c '${name}="${value}"'`), keepFromShells].filter(Boolean).join(' '),
launchArgs: [base.launchArgs, ...overrides.map(([name, value]) => `-c '${name}="${tomlValue(value)}"'`), keepFromShells].filter(Boolean).join(' '),
customModels: t3Models('codex', gateway, models, capabilities),
};
},
Expand Down
19 changes: 19 additions & 0 deletions docker/setup/gateways/address.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ export function isMetadata(hostname) {
return Boolean(m) && Number(m[1]) === 169 && Number(m[2]) === 254 || ipv6(host) === 'fd00:ec2::254';
}

/** A resolved address in a metadata range. Never allowed, even with private-host consent. */
export function isMetadataIp(ip) {
const v = stripBrackets(String(ip ?? '').toLowerCase());
const m = /^(\d{1,3})\.(\d{1,3})\.\d{1,3}\.\d{1,3}$/.exec(v);
if (m) return Number(m[1]) === 169 && Number(m[2]) === 254;
const canonical = ipv6(v);
const mapped = /^::ffff:([a-f0-9]+):([a-f0-9]+)$/.exec(canonical ?? '');
if (mapped) {
const high = parseInt(mapped[1], 16), low = parseInt(mapped[2], 16);
return isMetadataIp([high >> 8, high & 255, low >> 8, low & 255].join('.'));
}
return canonical === 'fd00:ec2::254';
}

/** A Compose service name or LAN host: no dot, no colon, just letters, digits and dashes. */
export function isSingleLabel(hostname) {
const host = stripBrackets(String(hostname ?? '').toLowerCase());
Expand Down Expand Up @@ -93,6 +107,7 @@ export function parseAddress(input, options = {}) {
const allowPrivateHost = options?.allowPrivateHost === true;
const text = String(input ?? '').trim();
if (!text) return { ok: false, error: 'Enter your gateway’s address.' };
if (/["`\\]/.test(text)) return { ok: false, error: 'Remove " and \\ and ` from the address.' };
if (text.length > MAX_URL || /[\s\0]/.test(text)) return { ok: false, error: 'Enter a single address, without spaces.' };
let parsed;
try { parsed = new URL(SCHEME.test(text) ? text : assumedScheme(text) + text); }
Expand All @@ -108,6 +123,10 @@ export function parseAddress(input, options = {}) {
pathname = trimmed;
}
const hostname = stripBrackets(parsed.hostname);
if (isMetadata(hostname) || isMetadataIp(hostname)) {
return { ok: false, code: 'metadata-host', hostname, host: parsed.host,
error: `“${hostname}” is a cloud metadata address. A gateway can never use it.` };
}
if (!allowPrivateHost && needsPrivateConfirm(hostname)) {
return { ok: false, code: 'private-host', hostname, host: parsed.host,
error: `“${hostname}” looks like a private host. Confirm that you trust this address with your credentials.` };
Expand Down
3 changes: 2 additions & 1 deletion docker/setup/gateways/hub.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ const refusal = text => {
*/
export async function probeHub({ url, key, fetchImpl = globalThis.fetch, timeoutMs = TIMEOUT_MS, signal, allowPrivateHost = false, lookup = lookupGateway } = {}) {
const parsed = parseAddress(url, { allowPrivateHost });
if (!parsed.ok && parsed.code === 'metadata-host') return { ...failure('metadata-host', parsed.error, 'Use a gateway address instead.') };
if (!parsed.ok && parsed.code === 'private-host') return { ...failure('private-host', parsed.error, 'Confirm that you trust this address with your credentials, then try again.'), confirmHost: parsed.hostname };
if (!parsed.ok) return REFUSED.unreachable();
let fetchPinned;
try { fetchPinned = await gatewayFetch(parsed, { allowPrivateHost, lookup, signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs), fetchImpl }); }
catch (error) { return error.code === 'private-host' ? { ...failure('private-host', `“${parsed.hostname}” resolves to a private host.`, 'Confirm this host before sending a key.'), confirmHost: parsed.hostname } : REFUSED.unreachable(); }
catch (error) { if (error.code === 'metadata-host') return { ...failure('metadata-host', `“${parsed.hostname}” is a cloud metadata address. A gateway can never use it.`, 'Use a gateway address instead.') }; return error.code === 'private-host' ? { ...failure('private-host', `“${parsed.hostname}” resolves to a private host.`, 'Confirm this host before sending a key.'), confirmHost: parsed.hostname } : REFUSED.unreachable(); }
// Keyless first: a host that can't be reached never sees the management key.
try {
const probe = await fetchPinned(`${parsed.url}/`, { redirect: 'manual', headers: { accept: 'application/json', 'user-agent': 't3code-docker-setup' },
Expand Down
2 changes: 2 additions & 0 deletions docker/setup/gateways/probe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ export async function probeGateway({ url: input, key = '', fetchImpl = globalThi
const { url, notes, insecure, hostname } = address, headers = headersFor(key), keyless = headersFor(), limit = () => signal ? AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs);
const finish = (result) => ({ at, url, notes, insecure, ms: Math.max(0, now() - at), models: { listed: false, ids: [], capabilities: {} }, protocols: {}, ...result });
const privateRefusal = pinned => finish({ ok: false, code: 'private-host', confirmHost: hostname, host: address.host, message: `“${hostname}” looks like a private host${pinned ? ` (${pinned})` : ''}.`, hint: 'Tick “Private host” to confirm this address is yours, then test again.' });
const metadataRefusal = pinned => finish({ ok: false, code: 'metadata-host', host: address.host, message: `“${hostname}” is a cloud metadata address${pinned ? ` (${pinned})` : ''}. A gateway can never use it.`, hint: 'Use a gateway address instead.' });
// DNS is pinned once, before any request: no re-resolve per redirect or per route.
let fetchPinned;
try { fetchPinned = await gatewayFetch(address, { allowPrivateHost, lookup, signal: limit(), fetchImpl }); }
catch (error) {
if (error.code === 'metadata-host') return metadataRefusal(error.address);
if (error.code === 'private-host') return privateRefusal(error.address);
const code = connectionCode(error); return finish({ ok: false, code, ...connectionProblem(code, address) });
}
Expand Down
4 changes: 3 additions & 1 deletion docker/setup/gateways/transport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isIP } from 'node:net';
import http from 'node:http';
import https from 'node:https';
import { Readable } from 'node:stream';
import { isBlockedIp } from './address.mjs';
import { isBlockedIp, isMetadataIp } from './address.mjs';

export const lookupGateway = hostname => dnsLookup(hostname, { all: true });
const dnsError = () => Object.assign(new Error('The gateway address could not be resolved.'), { code: 'ENOTFOUND' });
Expand All @@ -32,6 +32,8 @@ async function resolved(hostname, lookup, signal) {

export async function gatewayFetch(address, { allowPrivateHost = false, lookup = lookupGateway, signal, fetchImpl = globalThis.fetch } = {}) {
const addresses = await resolved(address.hostname, lookup, signal);
const metadata = addresses.find(row => isMetadataIp(row.address));
if (metadata) throw Object.assign(new Error('The gateway resolves to a cloud metadata address.'), { code: 'metadata-host', address: metadata.address });
const blocked = addresses.find(row => isBlockedIp(row.address));
if (blocked && allowPrivateHost !== true) throw Object.assign(new Error('The gateway resolves to a private address.'), { code: 'private-host', address: blocked.address });
// Injected transports are for controlled adapters/tests. Production always
Expand Down
2 changes: 1 addition & 1 deletion scripts/gateways-native-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const smokes = {
assert.deepEqual(found?.keys, ["ANTHROPIC_BASE_URL"]);
await fs.writeFile(settings, '{}');
const baseline = JSON.parse(await runClaude(env, '', { model: 'gemini-smoke' }));
assert.equal(baseline.modelUsage['gemini-smoke'].contextWindow, 200000);
assert.equal(baseline.modelUsage['gemini-smoke'].contextWindow, 1000000);
const source = new URL(process.env.T3_SETUP_DIR ? 'file:///opt/t3-runtime/patches/gateway-context.ts' : '../docker/runtime/patches/gateway-context.ts', import.meta.url);
const helper = await import(`data:text/javascript,${encodeURIComponent(stripTypeScriptTypes(await fs.readFile(source, 'utf8')))}`);
const gw = gatewayFor(gateway.url);
Expand Down
6 changes: 5 additions & 1 deletion tests/gateway-transport.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ test('DNS resolution uses the same bounded probe deadline', async t => {
});

test('IPv6 link-local, metadata and mapped loopback forms require literal boolean consent', () => {
for (const address of ['::ffff:127.0.0.1', '::ffff:7f00:1', '0:0:0:0:0:ffff:a9fe:a9fe', 'fe80::1', 'febf::1', 'fd00:ec2::254', '::']) {
for (const address of ['::ffff:127.0.0.1', '::ffff:7f00:1', 'fe80::1', 'febf::1', '::']) {
assert.equal(isBlockedIp(address), true, address);
assert.equal(parseAddress(`http://[${address}]`).code, 'private-host', address);
assert.equal(parseAddress(`http://[${address}]`, { allowPrivateHost: 'false' }).code, 'private-host', address);
assert.equal(parseAddress(`http://[${address}]`, { allowPrivateHost: true }).ok, true, address);
}
assert.equal(isBlockedIp('2606:4700:4700::1111'), false);
for (const address of ['fd00:ec2::254', '0:0:0:0:0:ffff:a9fe:a9fe']) {
assert.equal(parseAddress(`http://[${address}]`).code, 'metadata-host', address);
assert.equal(parseAddress(`http://[${address}]`, { allowPrivateHost: true }).code, 'metadata-host', address);
}
});

test('a landing-page redirect does not reject working API paths or send a key to the target', async t => {
Expand Down
10 changes: 8 additions & 2 deletions tests/gateways-address.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ test('a pasted admin address loses its admin path, so a test can’t count as a
}
});

test('loopback, metadata and single-label names need a private-host confirm', () => {
for (const input of ['http://localhost:8317', 'http://127.0.0.1:8317', 'http://127.1.2.3:8317', 'http://0.0.0.0:8317', 'http://[::1]:8317', 'http://169.254.169.254', 'http://169.254.10.20:80', 'http://gateway:8317', 'cliproxy:8317', 'localhost:8317']) {
test('loopback and single-label names need a private-host confirm, metadata stays refused', () => {
for (const input of ['http://localhost:8317', 'http://127.0.0.1:8317', 'http://127.1.2.3:8317', 'http://0.0.0.0:8317', 'http://[::1]:8317', 'http://gateway:8317', 'cliproxy:8317', 'localhost:8317']) {
const rejected = parseAddress(input);
assert.equal(rejected.ok, false, input);
assert.equal(rejected.code, 'private-host', input);
assert.match(rejected.error, /Confirm that you trust/, input);
const allowed = parseAddress(input, { allowPrivateHost: true });
assert.equal(allowed.ok, true, input);
}
for (const input of ['http://169.254.169.254', 'http://169.254.10.20:80']) {
assert.equal(parseAddress(input).code, 'metadata-host', input);
const refused = parseAddress(input, { allowPrivateHost: true });
assert.equal(refused.ok, false, input);
assert.equal(refused.code, 'metadata-host', input);
}
assert.equal(parseAddress('https://openrouter.ai/api').ok, true);
assert.equal(parseAddress('http://192.168.1.20:4000').ok, true);
assert.equal(parseAddress('http://10.0.0.2:4000').ok, true);
Expand Down
54 changes: 54 additions & 0 deletions tests/gateways-metadata-codex.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { isMetadataIp, parseAddress } from '../docker/setup/gateways/address.mjs';
import { probeGateway } from '../docker/setup/gateways/probe.mjs';
import { probeHub } from '../docker/setup/gateways/hub.mjs';
import { desiredInstance } from '../docker/setup/gateways/adapters.mjs';
import { agentOf } from '../docker/setup/gateways/catalog.mjs';

const codex = agentOf('codex');

test('metadata stays refused even with private-host consent', () => {
for (const url of ['http://169.254.169.254/', 'http://169.254.10.20:80', 'http://[fd00:ec2::254]/']) {
const parsed = parseAddress(url, { allowPrivateHost: true });
assert.equal(parsed.ok, false, url);
assert.equal(parsed.code, 'metadata-host', url);
}
assert.equal(isMetadataIp('169.254.169.254'), true);
assert.equal(isMetadataIp('169.254.0.1'), true);
assert.equal(isMetadataIp('fd00:ec2::254'), true);
assert.equal(isMetadataIp('127.0.0.1'), false);
assert.equal(isMetadataIp('192.168.1.1'), false);
assert.equal(isMetadataIp('8.8.8.8'), false);
// Consent still opens loopback, RFC1918 and Compose names.
assert.equal(parseAddress('http://127.0.0.1:8317', { allowPrivateHost: true }).ok, true);
assert.equal(parseAddress('http://192.168.1.20:4000').ok, true);
assert.equal(parseAddress('http://gateway:8317', { allowPrivateHost: true }).ok, true);
});

test('a metadata probe sends no key, even with consent', async () => {
let called = 0;
const fetchImpl = async () => { called++; return new Response('{}', { status: 200 }); };
const direct = await probeGateway({ url: 'http://169.254.169.254/', key: 'sk-test', allowPrivateHost: true, fetchImpl });
assert.equal(direct.code, 'metadata-host');
assert.equal(called, 0);
const viaDns = await probeGateway({ url: 'https://gateway.example.com', key: 'sk-test', allowPrivateHost: true, fetchImpl, lookup: async () => '169.254.169.254' });
assert.equal(viaDns.code, 'metadata-host');
assert.equal(called, 0);
const hub = await probeHub({ url: 'http://169.254.169.254/', key: 'mgmt-test', allowPrivateHost: true, fetchImpl });
assert.equal(hub.code, 'metadata-host');
assert.equal(called, 0);
});

test('quote, backslash and backtick never become an address', () => {
for (const url of ['http://127.0.0.1:8317/a"b', 'http://127.0.0.1:8317/a\\b', 'http://127.0.0.1:8317/a`b']) {
const parsed = parseAddress(url, { allowPrivateHost: true });
assert.equal(parsed.ok, false, url);
}
});

test('Codex launch arguments escape quotes and backslashes for TOML', () => {
const gateway = { id: 'my-proxy', name: 'My Proxy', url: 'http://127.0.0.1:8317/a"b\\c', accent: '#6366f1', models: { ids: [], manual: [] }, agents: { codex: { on: true } } };
const { config } = desiredInstance(codex, gateway, { dir: '/d' });
assert.match(config.launchArgs, /a\\"b\\\\c\/v1/);
});
13 changes: 10 additions & 3 deletions tests/gateways-probe.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ test('other gateways and rejected keys never request the CLIProxyAPI catalog', a
test('a private host needs a confirm before any request goes out', async t => {
const seen = [];
const fetchImpl = async () => { seen.push(true); throw new Error('must not fetch'); };
for (const url of ['http://localhost:8317', 'http://127.0.0.1:8317', 'http://0.0.0.0:8317', 'http://[::1]:8317', 'http://169.254.169.254', 'http://gateway:8317']) {
for (const url of ['http://localhost:8317', 'http://127.0.0.1:8317', 'http://0.0.0.0:8317', 'http://[::1]:8317', 'http://gateway:8317']) {
const result = await probeGateway({ url, key: 'sk-test', fetchImpl });
assert.equal(result.code, 'private-host', url);
assert.match(result.message, /Private host|private host/);
assert.equal(result.confirmHost, result.confirmHost);
}
const metadata = await probeGateway({ url: 'http://169.254.169.254', key: 'sk-test', fetchImpl, allowPrivateHost: true });
assert.equal(metadata.code, 'metadata-host');
assert.equal(seen.length, 0, 'No request, keyed or not, without a confirm');
const server = await serve(gateway()); within(t, server);
const allowed = await probeGateway({ allowPrivateHost: true, url: server.url, key: 'sk-test' });
Expand All @@ -276,13 +278,18 @@ test('a private host needs a confirm before any request goes out', async t => {
test('a name that resolves to a blocked address needs the same confirm', async () => {
const seen = [];
const fetchImpl = async () => { seen.push(true); return new Response(JSON.stringify({ data: [] }), { status: 200 }); };
const blocked = await probeGateway({ url: 'https://gateway.example.com', key: 'sk-test', fetchImpl, lookup: async () => '169.254.169.254' });
const blocked = await probeGateway({ url: 'https://gateway.example.com', key: 'sk-test', fetchImpl, lookup: async () => '127.0.0.1' });
assert.equal(blocked.code, 'private-host');
assert.equal(blocked.confirmHost, 'gateway.example.com');
assert.equal(seen.length, 0);
const allowed = await probeGateway({ allowPrivateHost: true, url: 'https://gateway.example.com', key: 'sk-test', fetchImpl, lookup: async () => '169.254.169.254' });
const allowed = await probeGateway({ allowPrivateHost: true, url: 'https://gateway.example.com', key: 'sk-test', fetchImpl, lookup: async () => '127.0.0.1' });
assert.notEqual(allowed.code, 'private-host');
assert.ok(seen.length > 0, 'DNS is pinned once, then the keyed probe runs');
const metaSeen = [];
const metaFetch = async () => { metaSeen.push(true); return new Response(JSON.stringify({ data: [] }), { status: 200 }); };
const meta = await probeGateway({ allowPrivateHost: true, url: 'https://gateway.example.com', key: 'sk-test', fetchImpl: metaFetch, lookup: async () => '169.254.169.254' });
assert.equal(meta.code, 'metadata-host');
assert.equal(metaSeen.length, 0, 'No request to metadata, even with consent');
});

test('an unreachable host sees no key: the keyless check runs first', async t => {
Expand Down
Loading