|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// An authored connector entry (`DeclarativeConnectorEntrySchema`, behind |
| 4 | +// `defineStack({ connectors })`, `os validate` / `os compile`, the artifact load |
| 5 | +// and `PUT /meta/connector/:name`) keeps no credential part in a URL: under |
| 6 | +// `providerConfig` (any key, any depth, array elements included) and under |
| 7 | +// `auth` (`tokenUrl`, `authorizationUrl`, `resource`), a URL holding a userinfo |
| 8 | +// or a credential query parameter is refused, one issue per URL, located at |
| 9 | +// the URL. Every refusal has a CONTROL accept beside it, so a refusal that fired |
| 10 | +// on everything could not pass, and the last block pins that the write door |
| 11 | +// refuses exactly what the read redactor rewrites. |
| 12 | +// |
| 13 | +// Every value below is an obviously-fake placeholder, and the refusal must |
| 14 | +// never echo it. |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { ConnectorSchema, DeclarativeConnectorEntrySchema } from './connector.zod'; |
| 18 | +import { redactConnectorItem } from './connector-credential-redaction'; |
| 19 | +import { ObjectStackDefinitionSchema } from '../stack.zod'; |
| 20 | + |
| 21 | +const PLACEHOLDER = 'TEST_PLACEHOLDER_NOT_A_SECRET'; |
| 22 | + |
| 23 | +const rest = (baseUrl: string, auth: Record<string, unknown> = { type: 'bearer', credentialRef: 'VENDOR_TOKEN' }) => ({ |
| 24 | + name: 'vendor', |
| 25 | + label: 'Vendor', |
| 26 | + type: 'api', |
| 27 | + provider: 'rest', |
| 28 | + providerConfig: { baseUrl }, |
| 29 | + auth, |
| 30 | +}); |
| 31 | + |
| 32 | +const mcp = (url: string) => ({ |
| 33 | + name: 'tools', |
| 34 | + label: 'Tools', |
| 35 | + type: 'api', |
| 36 | + provider: 'mcp', |
| 37 | + providerConfig: { transport: { kind: 'http', url } }, |
| 38 | +}); |
| 39 | + |
| 40 | +const openapi = (spec: string) => ({ |
| 41 | + name: 'billing', |
| 42 | + label: 'Billing', |
| 43 | + type: 'api', |
| 44 | + provider: 'openapi', |
| 45 | + providerConfig: { spec }, |
| 46 | + auth: { type: 'bearer', credentialRef: 'BILLING_TOKEN' }, |
| 47 | +}); |
| 48 | + |
| 49 | +const oauth2 = (over: Record<string, unknown>) => rest('https://api.vendor.example.com', { |
| 50 | + type: 'oauth2', |
| 51 | + grant: 'authorization_code', |
| 52 | + authorizationUrl: 'https://auth.vendor.example.com/authorize', |
| 53 | + tokenUrl: 'https://auth.vendor.example.com/token', |
| 54 | + clientId: 'client-1', |
| 55 | + credentialRef: 'VENDOR_CLIENT_SECRET', |
| 56 | + ...over, |
| 57 | +}); |
| 58 | + |
| 59 | +/** Every issue of a refused parse, as `[code, path]`, and the messages. */ |
| 60 | +function refusals(input: unknown): { located: Array<[string, string]>; messages: string[] } { |
| 61 | + const result = DeclarativeConnectorEntrySchema.safeParse(input); |
| 62 | + expect(result.success, 'the entry must be refused').toBe(false); |
| 63 | + const issues = result.error!.issues; |
| 64 | + return { |
| 65 | + located: issues.map((issue) => [issue.code, issue.path.join('.')]), |
| 66 | + messages: issues.map((issue) => issue.message), |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +const accepts = (input: unknown): void => { |
| 71 | + const result = DeclarativeConnectorEntrySchema.safeParse(input); |
| 72 | + expect(result.success, JSON.stringify(result.success ? null : result.error.issues)).toBe(true); |
| 73 | +}; |
| 74 | + |
| 75 | +describe('an authored connector URL holds no credential part', () => { |
| 76 | + describe('CONTROL: a URL with no credential part is accepted at every position', () => { |
| 77 | + it.each([ |
| 78 | + ['a rest baseUrl', rest('https://api.vendor.example.com/v1')], |
| 79 | + ['a baseUrl with an ordinary query', rest('https://api.vendor.example.com/v1?region=eu&page=2')], |
| 80 | + ['a baseUrl whose credential parameter is empty', rest('https://api.vendor.example.com/v1?token=&api_key=')], |
| 81 | + ['a baseUrl whose credential name sits in the fragment', rest(`https://api.vendor.example.com/v1#token=${PLACEHOLDER}`)], |
| 82 | + ['an mcp http transport url', mcp('https://mcp.vendor.example.com/mcp')], |
| 83 | + ['an openapi spec URL', openapi('https://api.vendor.example.com/openapi.json')], |
| 84 | + ['an openapi spec that is a path, not a URL', openapi('./specs/billing.yaml')], |
| 85 | + ['oauth2 endpoints and resource', oauth2({ resource: 'https://api.vendor.example.com/' })], |
| 86 | + ['a credentialRef-only instance', rest('https://api.vendor.example.com', { type: 'api-key', credentialRef: 'VENDOR_KEY', paramName: 'key' })], |
| 87 | + ] as const)('%s', (_label, entry) => accepts(entry)); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('REFUSED: a userinfo or a credential query parameter, at each URL position', () => { |
| 91 | + it.each([ |
| 92 | + ['a userinfo password in the baseUrl', rest(`https://user:${PLACEHOLDER}@api.vendor.example.com/v1`), 'providerConfig.baseUrl'], |
| 93 | + ['a userinfo token in the baseUrl', rest(`https://${PLACEHOLDER}@api.vendor.example.com/v1`), 'providerConfig.baseUrl'], |
| 94 | + ['`api_key` in the baseUrl query', rest(`https://api.vendor.example.com/v1?api_key=${PLACEHOLDER}`), 'providerConfig.baseUrl'], |
| 95 | + ['`token` in the baseUrl query', rest(`https://api.vendor.example.com/v1?region=eu&token=${PLACEHOLDER}`), 'providerConfig.baseUrl'], |
| 96 | + ['`access_token` in the baseUrl query', rest(`https://api.vendor.example.com/v1?access_token=${PLACEHOLDER}`), 'providerConfig.baseUrl'], |
| 97 | + ['a credential name spelled in another case', rest(`https://api.vendor.example.com/v1?Access_Token=${PLACEHOLDER}`), 'providerConfig.baseUrl'], |
| 98 | + ['a percent-encoded credential name', rest(`https://api.vendor.example.com/v1?api%5Fkey=${PLACEHOLDER}`), 'providerConfig.baseUrl'], |
| 99 | + [ |
| 100 | + 'the instance\'s own `auth.paramName` in the baseUrl query', |
| 101 | + rest(`https://api.vendor.example.com/v1?vendor_key=${PLACEHOLDER}`, { type: 'api-key', credentialRef: 'VENDOR_KEY', paramName: 'vendor_key' }), |
| 102 | + 'providerConfig.baseUrl', |
| 103 | + ], |
| 104 | + ['a userinfo password in an mcp transport url', mcp(`https://user:${PLACEHOLDER}@mcp.vendor.example.com/mcp`), 'providerConfig.transport.url'], |
| 105 | + ['`token` in an openapi spec URL', openapi(`https://api.vendor.example.com/openapi.json?token=${PLACEHOLDER}`), 'providerConfig.spec'], |
| 106 | + ['a userinfo password in `auth.tokenUrl`', oauth2({ tokenUrl: `https://c:${PLACEHOLDER}@auth.vendor.example.com/token` }), 'auth.tokenUrl'], |
| 107 | + ['`client_secret` in `auth.tokenUrl`', oauth2({ tokenUrl: `https://auth.vendor.example.com/token?client_secret=${PLACEHOLDER}` }), 'auth.tokenUrl'], |
| 108 | + ['a userinfo in `auth.authorizationUrl`', oauth2({ authorizationUrl: `https://c:${PLACEHOLDER}@auth.vendor.example.com/authorize` }), 'auth.authorizationUrl'], |
| 109 | + ['`key` in `auth.resource`', oauth2({ resource: `https://api.vendor.example.com/?key=${PLACEHOLDER}` }), 'auth.resource'], |
| 110 | + ] as const)('%s', (_label, entry, at) => { |
| 111 | + const { located, messages } = refusals(entry); |
| 112 | + expect(located).toEqual([['custom', at]]); |
| 113 | + expect(messages[0]).toContain(`\`${at}\``); |
| 114 | + expect(messages[0]).toContain('auth: { type, credentialRef }'); |
| 115 | + expect(messages[0]).not.toContain(PLACEHOLDER); |
| 116 | + }); |
| 117 | + |
| 118 | + it('a URL at any depth under `providerConfig`, an array element and a dotted key included', () => { |
| 119 | + const entry = { |
| 120 | + ...rest('https://api.vendor.example.com'), |
| 121 | + providerConfig: { |
| 122 | + baseUrl: 'https://api.vendor.example.com', |
| 123 | + mirrors: ['https://eu.vendor.example.com', `https://us.vendor.example.com?sig=${PLACEHOLDER}`], |
| 124 | + 'x.callback': `https://hooks.vendor.example.com?secret=${PLACEHOLDER}`, |
| 125 | + nested: { deeper: { url: `https://u:${PLACEHOLDER}@deep.vendor.example.com` } }, |
| 126 | + }, |
| 127 | + }; |
| 128 | + expect(refusals(entry).located).toEqual([ |
| 129 | + ['custom', 'providerConfig.mirrors.1'], |
| 130 | + ['custom', 'providerConfig.x.callback'], |
| 131 | + ['custom', 'providerConfig.nested.deeper.url'], |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it('one issue per URL, naming every credential part it holds', () => { |
| 136 | + const entry = oauth2({ tokenUrl: `https://c:${PLACEHOLDER}@auth.vendor.example.com/token?token=${PLACEHOLDER}&key=${PLACEHOLDER}` }); |
| 137 | + const { located, messages } = refusals(entry); |
| 138 | + expect(located).toEqual([['custom', 'auth.tokenUrl']]); |
| 139 | + expect(messages[0]).toContain('a userinfo'); |
| 140 | + expect(messages[0]).toContain('the `token` query parameter'); |
| 141 | + expect(messages[0]).toContain('the `key` query parameter'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('inside a stack, the issue is located under the entry', () => { |
| 145 | + const result = ObjectStackDefinitionSchema.safeParse({ |
| 146 | + manifest: { id: 'com.example.app', name: 'app', version: '1.0.0', type: 'app' }, |
| 147 | + connectors: [rest(`https://api.vendor.example.com?token=${PLACEHOLDER}`)], |
| 148 | + }); |
| 149 | + expect(result.success).toBe(false); |
| 150 | + const paths = result.error!.issues.map((issue) => issue.path.join('.')); |
| 151 | + expect(paths).toContain('connectors.0.providerConfig.baseUrl'); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('CONTROL: a URL inside a header or environment map is left to the map\'s own rules (the read withholds the map whole)', () => { |
| 156 | + accepts({ |
| 157 | + ...rest('https://api.vendor.example.com'), |
| 158 | + providerConfig: { baseUrl: 'https://api.vendor.example.com', headers: { Referer: `https://u:${PLACEHOLDER}@app.example.com` } }, |
| 159 | + }); |
| 160 | + accepts({ |
| 161 | + name: 'local_tools', label: 'Local tools', type: 'api', provider: 'mcp', |
| 162 | + providerConfig: { transport: { kind: 'stdio', command: 'tools', env: { UPSTREAM_URL: `https://u:${PLACEHOLDER}@db.example.com` } } }, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('CONTROL: the open runtime carrier `ConnectorSchema` keeps its posture', () => { |
| 167 | + expect(ConnectorSchema.safeParse(rest(`https://api.vendor.example.com?token=${PLACEHOLDER}`)).success).toBe(true); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('the write door refuses exactly what the read redactor rewrites', () => { |
| 171 | + const urls = [ |
| 172 | + 'https://api.vendor.example.com/v1', |
| 173 | + 'https://api.vendor.example.com/v1?page=2', |
| 174 | + 'https://api.vendor.example.com/v1?token=', |
| 175 | + `https://user:${PLACEHOLDER}@api.vendor.example.com/v1`, |
| 176 | + `https://${PLACEHOLDER}@api.vendor.example.com/v1`, |
| 177 | + `https://api.vendor.example.com/v1?apikey=${PLACEHOLDER}`, |
| 178 | + `https://api.vendor.example.com/v1?page=2&signature=${PLACEHOLDER}#token=x`, |
| 179 | + `https://api.vendor.example.com/v1#api_key=${PLACEHOLDER}`, |
| 180 | + ]; |
| 181 | + it.each(urls)('%s', (url) => { |
| 182 | + for (const entry of [rest(url), oauth2({ tokenUrl: url })]) { |
| 183 | + const refused = !DeclarativeConnectorEntrySchema.safeParse(entry).success; |
| 184 | + const rewritten = redactConnectorItem(entry as Record<string, unknown>).redactedKeys.length > 0; |
| 185 | + expect(refused, `${url}: refused ⇔ rewritten`).toBe(rewritten); |
| 186 | + } |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments