Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { warnIfBindingsExposeSecrets } from '../src/validate-bindings'
3import type { SecretParameterInfo } from '../src/validate-bindings'
4
5describe('warnIfBindingsExposeSecrets — top-level detection', () => {
6 it('warns when input schema has a secret-like parameter name', () => {
7 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
8
9 warnIfBindingsExposeSecrets([
10 {
11 name: 'callApi',
12 inputSchema: {
13 type: 'object',
14 properties: {
15 url: { type: 'string' },
16 apiKey: { type: 'string' },
17 },
18 },
19 },
20 ])
21
22 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('apiKey'))
23 warnSpy.mockRestore()
24 })
25
26 it('does not warn for safe parameter names', () => {
27 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
28
29 warnIfBindingsExposeSecrets([
30 {
31 name: 'search',
32 inputSchema: {
33 type: 'object',
34 properties: {
35 query: { type: 'string' },
36 limit: { type: 'number' },
37 },
38 },
39 },
40 ])
41
42 expect(warnSpy).not.toHaveBeenCalled()
43 warnSpy.mockRestore()
44 })
45
46 it('detects multiple secret patterns in a single tool', () => {
47 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
48
49 warnIfBindingsExposeSecrets([
50 {
51 name: 'auth',
52 inputSchema: {
53 type: 'object',
54 properties: {
55 token: { type: 'string' },
56 password: { type: 'string' },
57 username: { type: 'string' },
58 },
59 },
60 },
61 ])
62
63 const calls = warnSpy.mock.calls.map((c) => String(c[0]))
64 expect(calls.some((c) => c.includes('token'))).toBe(true)
65 expect(calls.some((c) => c.includes('password'))).toBe(true)
66 expect(calls.some((c) => c.includes('username'))).toBe(false)
67 warnSpy.mockRestore()
68 })
69
70 it('handles tools with no inputSchema', () => {
71 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
72
73 warnIfBindingsExposeSecrets([{ name: 'simple' }])
74
75 expect(warnSpy).not.toHaveBeenCalled()
76 warnSpy.mockRestore()
77 })
78})
79
80describe('warnIfBindingsExposeSecrets — name patterns', () => {
81 const secretNames = [
82 'apiKey',
83 'api_key',
84 'api-key',
85 'APIKey',
86 'accessToken',
87 'access_token',
88 'bearerToken',
89 'refreshToken',
90 'sessionToken',
91 'clientSecret',
92 'client_secret',
93 'webhookSecret',
94 'authorization',
95 'Authorization',
96 'password',
97 'Password',
98 'passcode',
99 'pwd',
100 'jwt',
101 'privateKey',
102 'private_key',
103 'openaiApiKey',
104 'openai_api_key',
105 'githubToken',
106 'x-api-key',
107 ]
108
109 it.each(secretNames)('warns on parameter named "%s"', (paramName) => {
110 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
111
112 warnIfBindingsExposeSecrets([
113 {
114 name: 'tool',
115 inputSchema: {
116 type: 'object',
117 properties: { [paramName]: { type: 'string' } },
118 },
119 },
120 ])
121
122 expect(warnSpy).toHaveBeenCalled()
123 warnSpy.mockRestore()
124 })
125
126 const safeNames = [
127 'username',
128 'email',
129 'query',
130 'limit',
131 'offset',
132 'url',
133 'path',
134 'id',
135 'userId',
136 'foreignKey',
137 'sortKey',
138 'partitionKey',
139 'tokenizer',
140 'tokenization',
141 'tokens',
142 'keyboardLayout',
143 ]
144
145 it.each(safeNames)('does NOT warn on safe parameter "%s"', (paramName) => {
146 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
147
148 warnIfBindingsExposeSecrets([
149 {
150 name: 'tool',
151 inputSchema: {
152 type: 'object',
153 properties: { [paramName]: { type: 'string' } },
154 },
155 },
156 ])
157
158 expect(warnSpy).not.toHaveBeenCalled()
159 warnSpy.mockRestore()
160 })
161})
162
163describe('warnIfBindingsExposeSecrets — nested schema recursion', () => {
164 it('detects secrets nested inside an object property', () => {
165 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
166
167 warnIfBindingsExposeSecrets([
168 {
169 name: 'nested',
170 inputSchema: {
171 type: 'object',
172 properties: {
173 auth: {
174 type: 'object',
175 properties: {
176 token: { type: 'string' },
177 },
178 },
179 },
180 },
181 },
182 ])
183
184 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('auth.token'))
185 warnSpy.mockRestore()
186 })
187
188 it('detects secrets inside array items', () => {
189 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
190
191 warnIfBindingsExposeSecrets([
192 {
193 name: 'arr',
194 inputSchema: {
195 type: 'object',
196 properties: {
197 headers: {
198 type: 'array',
199 items: {
200 type: 'object',
201 properties: {
202 apiKey: { type: 'string' },
203 },
204 },
205 },
206 },
207 },
208 },
209 ])
210
211 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('apiKey'))
212 warnSpy.mockRestore()
213 })
214
215 it('detects secrets inside anyOf/oneOf/allOf branches', () => {
216 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
217
218 warnIfBindingsExposeSecrets([
219 {
220 name: 'unioned',
221 inputSchema: {
222 type: 'object',
223 properties: {
224 credentials: {
225 anyOf: [
226 {
227 type: 'object',
228 properties: { password: { type: 'string' } },
229 },
230 {
231 type: 'object',
232 properties: { jwt: { type: 'string' } },
233 },
234 ],
235 },
236 },
237 },
238 },
239 ])
240
241 const calls = warnSpy.mock.calls.map((c) => String(c[0]))
242 expect(calls.some((c) => c.includes('password'))).toBe(true)
243 expect(calls.some((c) => c.includes('jwt'))).toBe(true)
244 warnSpy.mockRestore()
245 })
246
247 it('resolves and scans $ref targets in $defs', () => {
248 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
249
250 warnIfBindingsExposeSecrets([
251 {
252 name: 'refs',
253 inputSchema: {
254 $defs: {
255 Creds: {
256 type: 'object',
257 properties: {
258 accessToken: { type: 'string' },
259 },
260 },
261 },
262 type: 'object',
263 properties: {
264 creds: { $ref: '#/$defs/Creds' },
265 },
266 },
267 },
268 ])
269
270 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('accessToken'))
271 warnSpy.mockRestore()
272 })
273
274 it('scans additionalProperties schemas', () => {
275 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
276
277 warnIfBindingsExposeSecrets([
278 {
279 name: 'dict',
280 inputSchema: {
281 type: 'object',
282 additionalProperties: {
283 type: 'object',
284 properties: {
285 secret: { type: 'string' },
286 },
287 },
288 },
289 },
290 ])
291
292 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('secret'))
293 warnSpy.mockRestore()
294 })
295
296 it('does not loop forever on schemas that self-reference via cycles', () => {
297 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
298
299 const cyclic: Record<string, unknown> = { type: 'object', properties: {} }
300 ;(cyclic.properties as Record<string, unknown>).self = cyclic
301 ;(cyclic.properties as Record<string, unknown>).secret = { type: 'string' }
302
303 warnIfBindingsExposeSecrets([
304 { name: 'cycle', inputSchema: cyclic as Record<string, unknown> },
305 ])
306
307 expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('secret'))
308 warnSpy.mockRestore()
309 })
310})
311
312describe('warnIfBindingsExposeSecrets — handler variants', () => {
313 it('handler: "ignore" suppresses warnings', () => {
314 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
315
316 warnIfBindingsExposeSecrets(
317 [
318 {
319 name: 't',
320 inputSchema: {
321 type: 'object',
322 properties: { apiKey: { type: 'string' } },
323 },
324 },
325 ],
326 { handler: 'ignore' },
327 )
328
329 expect(warnSpy).not.toHaveBeenCalled()
330 warnSpy.mockRestore()
331 })
332
333 it('handler: "throw" throws on the first match', () => {
334 expect(() =>
335 warnIfBindingsExposeSecrets(
336 [
337 {
338 name: 't',
339 inputSchema: {
340 type: 'object',
341 properties: { apiKey: { type: 'string' } },
342 },
343 },
344 ],
345 { handler: 'throw' },
346 ),
347 ).toThrow(/apiKey/)
348 })
349
350 it('handler: function receives each match', () => {
351 const matches: Array<SecretParameterInfo> = []
352
353 warnIfBindingsExposeSecrets(
354 [
355 {
356 name: 'callApi',
357 inputSchema: {
358 type: 'object',
359 properties: {
360 apiKey: { type: 'string' },
361 nested: {
362 type: 'object',
363 properties: { token: { type: 'string' } },
364 },
365 },
366 },
367 },
368 ],
369 { handler: (info) => matches.push(info) },
370 )
371
372 expect(matches).toHaveLength(2)
373 expect(matches[0]).toMatchObject({
374 toolName: 'callApi',
375 paramName: 'apiKey',
376 paramPath: ['apiKey'],
377 })
378 expect(matches[1]).toMatchObject({
379 toolName: 'callApi',
380 paramName: 'token',
381 paramPath: ['nested', 'token'],
382 })
383 })
384})
385
386describe('warnIfBindingsExposeSecrets — dedup', () => {
387 it('does not surface the same (tool, paramPath) twice across calls when a shared cache is passed', () => {
388 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
389 const cache = new Set<string>()
390
391 const tool = {
392 name: 'api',
393 inputSchema: {
394 type: 'object',
395 properties: { apiKey: { type: 'string' } },
396 },
397 }
398
399 warnIfBindingsExposeSecrets([tool], { dedupCache: cache })
400 warnIfBindingsExposeSecrets([tool], { dedupCache: cache })
401
402 expect(warnSpy).toHaveBeenCalledTimes(1)
403 warnSpy.mockRestore()
404 })
405
406 it('still surfaces a different paramPath on the second call', () => {
407 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
408 const cache = new Set<string>()
409
410 warnIfBindingsExposeSecrets(
411 [
412 {
413 name: 'api',
414 inputSchema: {
415 type: 'object',
416 properties: { apiKey: { type: 'string' } },
417 },
418 },
419 ],
420 { dedupCache: cache },
421 )
422 warnIfBindingsExposeSecrets(
423 [
424 {
425 name: 'api',
426 inputSchema: {
427 type: 'object',
428 properties: { secret: { type: 'string' } },
429 },
430 },
431 ],
432 { dedupCache: cache },
433 )
434
435 expect(warnSpy).toHaveBeenCalledTimes(2)
436 warnSpy.mockRestore()
437 })
438})