import assert from 'node:assert/strict'; import test from 'node:test'; import { loadConfig } from './config.mjs'; const env = { MCP_PUBLIC_URL: 'https://sample.test/mcp', MCP_ISSUER_URL: 'https://issuer.test', MCP_JWKS_URL: 'https://issuer.test/jwks', MCP_INTROSPECTION_URL: 'https://issuer.test/introspect', MCP_INTROSPECTION_CLIENT_ID: 'public-reference', MCP_DATABASE_URL_FILE: '/run/secrets/database', MCP_INTROSPECTION_CLIENT_SECRET_FILE: '/run/secrets/introspection', SOURCE_REVISION: 'a'.repeat(40), }; test('template fails closed on invalid identity, limits and secret references', () => { const read = () => 'synthetic-value'; assert.equal(loadConfig(env, read).concurrency, 8); for (const change of [ { MCP_PUBLIC_URL: 'http://sample.test/mcp' }, { MCP_JWKS_URL: 'https://other.test/jwks' }, { SOURCE_REVISION: 'main' }, { MCP_WRITES_PER_MINUTE: '0' }, ]) assert.throws(() => loadConfig({ ...env, ...change }, read)); assert.throws(() => loadConfig(env, () => '')); }); test('incomplete chunked JSON is rejected at the body deadline', async () => { const { boundedBody } = await import('./http.mjs'); const body = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode('{"jsonrpc":"2.0"}')); }, }); const request = new Request('https://sample.test/mcp', { method: 'POST', body, duplex: 'half' }); await assert.rejects(() => boundedBody(request, 32768, 20), { status: 408 }); }); test('owner mode requires its fixed workspace, endpoints and least-privilege scope contract', () => { const owner = { ...env, MCP_AUTH_MODE: 'owner-signed', MCP_WORKSPACE: 'sample364', MCP_INTROSPECTION_URL: 'https://issuer.test/token/introspection', }; const loaded = loadConfig(owner, () => 'synthetic-value'); assert.equal(loaded.readScope, 'observe:connections:read'); assert.equal(loaded.writeScope, 'observe:connections:write'); for (const change of [ { MCP_AUTH_MODE: 'automatic' }, { MCP_WORKSPACE: '' }, { MCP_WORKSPACE: 'x'.repeat(81) }, { MCP_INTROSPECTION_URL: 'https://issuer.test/introspect' }, { MCP_READ_SCOPE: 'sample:read' }, { MCP_WRITE_SCOPE: 'sample:drafts:write' }, ]) assert.throws(() => loadConfig({ ...owner, ...change }, () => 'synthetic-value')); }); test('owner opaque credentials require fresh signed introspection with both audience bindings', async () => { const { generateKeyPair, exportJWK, SignJWT } = await import('jose'); const { createPrincipalVerifier } = await import('./auth.mjs'); const key = await generateKeyPair('ES256'); const unrelated = await generateKeyPair('ES256'); const jwks = { keys: [{ ...(await exportJWK(key.publicKey)), kid: 'synthetic', alg: 'ES256' }] }; const config = { issuer: 'https://issuer.test', resource: 'https://sample.test/mcp', jwks: 'https://issuer.test/jwks', introspection: 'https://issuer.test/token/introspection', clientId: 'sample-introspection', secret: 'generated-fixture-reference', authMode: 'owner-signed', workspace: 'sample364', readScope: 'observe:connections:read', writeScope: 'observe:connections:write', }; const now = Math.floor(Date.now() / 1000); const good = { active: true, iss: config.issuer, aud: config.resource, sub: 'synthetic-owner', iat: now, exp: now + 300, token_type: 'Bearer', observe_workspace: config.workspace, scope: config.readScope + ' ' + config.writeScope, }; let active = good, outer = {}, media = 'application/token-introspection+jwt', calls = 0; let signing = key.privateKey; const fetcher = async (url, options) => { assert.equal(options.redirect, 'error'); if (String(url) === config.jwks) return Response.json(jwks); assert.equal(String(url), config.introspection); assert.equal(options.headers.Accept, 'application/token-introspection+jwt'); assert.equal(new URLSearchParams(options.body).get('token'), 'opaque-synthetic-token'); calls++; const token = await new SignJWT({ iss: config.issuer, aud: config.clientId, iat: now, token_introspection: active, ...outer, }) .setProtectedHeader({ alg: 'ES256', kid: 'synthetic', typ: outer.typ ?? 'token-introspection+jwt' }) .sign(signing); return new Response(media === 'application/json' ? JSON.stringify(active) : token, { headers: { 'content-type': media }, }); }; const verify = createPrincipalVerifier(config, fetcher); assert.deepEqual(await verify('Bearer opaque-synthetic-token', true), { account: good.sub, tenant: config.workspace, scopes: [config.readScope, config.writeScope], }); active = { ...good, scope: config.readScope }; await assert.rejects(() => verify('Bearer opaque-synthetic-token', true), { status: 403 }); active = { active: false }; await assert.rejects(() => verify('Bearer opaque-synthetic-token'), { status: 401 }); assert.equal(calls, 3, 'revocation and scopes must be introspected again, never cached'); active = good; for (const value of [ { aud: config.resource }, { iss: 'https://other.test' }, { iat: now - 61 }, { iat: now + 60 }, { typ: 'at+jwt' }, ]) { outer = value; await assert.rejects(() => verify('Bearer opaque-synthetic-token'), { status: 401 }); } outer = {}; for (const value of [ { aud: 'https://other.test/mcp' }, { aud: [config.resource, 'https://other.test/mcp'] }, { iss: 'https://other.test' }, { observe_workspace: 'another-workspace' }, { sub: '' }, { exp: now - 1 }, { exp: now + 301 }, { iat: now + 60 }, { nbf: now + 60 }, { token_type: 'DPoP' }, { scope: config.readScope + ' operator:admin' }, ]) { active = { ...good, ...value }; await assert.rejects(() => verify('Bearer opaque-synthetic-token'), { status: 401 }); } active = good; media = 'application/json'; await assert.rejects(() => verify('Bearer opaque-synthetic-token'), { status: 401 }); media = 'application/token-introspection+jwt'; signing = unrelated.privateKey; await assert.rejects(() => verify('Bearer opaque-synthetic-token'), { status: 401 }); await assert.rejects(() => verify(undefined), { status: 401 }); await assert.rejects(() => verify('Bearer ' + 'a'.repeat(4097)), { status: 401 }); // The legacy mode still requires an access-token signature before introspection. await assert.rejects( () => createPrincipalVerifier({ ...config, authMode: 'jwt-json' }, fetcher)('Bearer opaque-synthetic-token'), { status: 401 }, ); });