{{ message }}
Repository navigation
Expand file tree
/
Copy pathcheck-tool-request-boundary.test.ts
More file actions
408 lines (353 loc) · 11.3 KB
/
Copy pathcheck-tool-request-boundary.test.ts
File metadata and controls
408 lines (353 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import { describe, expect, it } from 'vitest'
import { auditToolSelfHops, mayAccessToolRequest } from './check-tool-request-boundary'
const ENCODED_ID_TEMPLATE = '$' + '{encodeURIComponent(params.id)}'
const GET_BASE_URL_TEMPLATE = '$' + '{getBaseUrl()}'
const _PARAMS_HOST_TEMPLATE = '$' + '{params.host}'
function auditRequest(request: string) {
return auditToolSelfHops(`
const tool = {
id: 'test_tool',
request: { ${request} },
}
`)
}
describe('tool self-hop audit', () => {
it('rejects the retired direct execution property', () => {
const audit = auditToolSelfHops(`
const tool = {
id: 'test_tool',
directExecution: async () => ({ success: true, output: {} }),
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({ reason: 'retired-direct-execution' }),
])
})
it('allows ordinary operation implementations', () => {
const audit = auditToolSelfHops(`
const tool = {
id: 'test_tool',
operation: { input: (params) => params },
}
`)
expect(audit.violations).toEqual([])
})
it('allows an absolute external provider URL', () => {
const audit = auditRequest(
"url: 'https://api.example.com/v1/items', method: 'GET', headers: () => ({})"
)
expect(audit).toEqual({
violations: [],
detectedSelfHops: 0,
legacyInternalPolicies: 0,
})
})
it('rejects a literal same-origin API route', () => {
const audit = auditRequest("url: '/api/tools/test', method: 'POST'")
expect(audit.detectedSelfHops).toBe(1)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'same-origin-tool-request',
}),
])
})
it('rejects a dynamic same-origin API route', () => {
const audit = auditRequest(`url: (params) => \`/api/tools/${ENCODED_ID_TEMPLATE}\``)
expect(audit.detectedSelfHops).toBe(1)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a same-origin path referenced through a constant', () => {
const audit = auditToolSelfHops(`
const INTERNAL_URL = '/api/tools/test'
const tool = {
id: 'test_tool',
request: { url: INTERNAL_URL, method: 'POST' },
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a same-origin request inherited through a tool-level spread', () => {
const audit = auditToolSelfHops(`
const base = { request: { url: '/api/tools/test', method: 'POST' } }
const tool = { id: 'test_tool', ...base }
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'same-origin-tool-request',
}),
])
})
it('uses a direct external URL that overrides an internal request spread', () => {
const audit = auditToolSelfHops(`
const internalRequest = { url: '/api/tools/test', method: 'POST' }
const tool = {
id: 'test_tool',
request: {
...internalRequest,
url: 'https://api.example.com/v1/items',
},
}
`)
expect(audit.violations).toEqual([])
})
it('rejects an unresolved spread that can override a known request', () => {
const audit = auditToolSelfHops(`
const known = { url: 'https://api.example.com/v1/items', method: 'POST' }
const tool = { id: 'test_tool', request: { ...known, ...unknownRequest } }
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('rejects a same-origin path returned through a helper', () => {
const audit = auditToolSelfHops(`
function buildInternalUrl(id) {
return '/api/tools/' + id
}
const tool = {
id: 'test_tool',
request: { url: (params) => buildInternalUrl(params.id), method: 'POST' },
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a same-origin URL constructor', () => {
const audit = auditToolSelfHops(`
import { getInternalApiBaseUrl } from '@/lib/core/utils/urls'
const tool = {
id: 'test_tool',
request: {
url: () => {
const url = new URL('/api/tools/test', getInternalApiBaseUrl())
return url.toString()
},
method: 'POST',
},
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a same-origin path concatenated with the Sim origin', () => {
const audit = auditToolSelfHops(`
import { getBaseUrl } from '@/lib/core/utils/urls'
const tool = {
id: 'test_tool',
request: { url: () => getBaseUrl() + '/api/tools/test', method: 'POST' },
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a helper-returned path resolved against the Sim origin', () => {
const audit = auditToolSelfHops(`
import { getBaseUrl } from '@/lib/core/utils/urls'
function buildPath() {
return '/api/tools/test'
}
const tool = {
id: 'test_tool',
request: { url: () => new URL(buildPath(), getBaseUrl()).toString(), method: 'POST' },
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a one-argument URL built from the Sim origin', () => {
const audit = auditToolSelfHops(`
import { getBaseUrl } from '@/lib/core/utils/urls'
const tool = {
id: 'test_tool',
request: {
url: () => new URL(\`${GET_BASE_URL_TEMPLATE}/api/tools/test\`).toString(),
method: 'POST',
},
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects a same-origin URL returned by an imported helper', () => {
const audit = auditToolSelfHops(`
import { buildWorkflowMcpServerUrl } from '@/lib/mcp/urls'
const tool = {
id: 'test_tool',
request: { url: (params) => buildWorkflowMcpServerUrl(params.id), method: 'POST' },
}
`)
expect(audit.violations[0]?.reason).toBe('same-origin-tool-request')
})
it('rejects the obsolete request.internal escape hatch', () => {
const audit = auditRequest('internal: true, url: (params) => buildInternalRoute(params.id)')
expect(audit.legacyInternalPolicies).toBe(1)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'legacy-internal-policy',
}),
])
})
it('rejects same-origin opt-in on an integration tool', () => {
const audit = auditToolSelfHops(`
const tool = {
id: 'test_tool',
request: {
allowSameOrigin: true,
url: (params) => params.url,
method: 'POST',
headers: () => ({}),
},
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unapproved-same-origin-policy',
}),
])
})
it.each(['http_request', 'webhook_request'])(
'allows the intentional same-origin policy on %s',
(toolId) => {
const audit = auditToolSelfHops(`
const tool = {
id: '${toolId}',
request: {
allowSameOrigin: true,
url: (params) => params.url,
method: 'POST',
headers: () => ({}),
},
}
`)
expect(audit.violations).toEqual([])
}
)
it('fails closed on a computed request property key', () => {
const audit = auditToolSelfHops(`
const tool = {
id: 'test_tool',
[runtimeRequestKey]: {
url: 'https://provider.example.com',
method: 'POST',
headers: () => ({}),
},
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('does not mistake a provider-relative path argument for a Sim API route', () => {
const audit = auditToolSelfHops(`
function providerUrl(path, host) {
return new URL(path, host).toString()
}
const tool = {
id: 'test_tool',
request: { url: (params) => providerUrl('/api/messages', params.host), method: 'GET' },
}
`)
expect(audit.violations).toEqual([])
})
it('allows a protocol-relative provider URL resolved against the Sim origin', () => {
const audit = auditToolSelfHops(`
import { getBaseUrl } from '@/lib/core/utils/urls'
const tool = {
id: 'test_tool',
request: {
url: () => new URL('//provider.example.com/api/messages', getBaseUrl()).toString(),
method: 'POST',
},
}
`)
expect(audit.violations).toEqual([])
})
it('fails closed when a dynamic suffix follows the Sim origin', () => {
const audit = auditToolSelfHops(`
import { getBaseUrl } from '@/lib/core/utils/urls'
const tool = {
id: 'test_tool',
request: {
url: (params) => new URL(
'/api/messages',
\`\${getBaseUrl()}\${params.providerDomain}\`
).toString(),
method: 'POST',
},
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('rejects a tool request object that cannot be statically resolved', () => {
const audit = auditToolSelfHops(`
const tool = { id: 'test_tool', request: unknownRequestFactory() }
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('rejects a request URL returned by an uninspectable helper', () => {
const audit = auditToolSelfHops(`
const tool = {
id: 'test_tool',
request: { url: () => unknownUrlHelper(), method: 'GET' },
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('rejects a request when any conditional branch cannot be resolved', () => {
const audit = auditToolSelfHops(`
const external = { url: 'https://api.example.com/v1/items', method: 'GET' }
const tool = {
id: 'test_tool',
request: flag ? external : unknownRequestFactory(),
}
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
it('does not use a nested function return to resolve an outer request factory', () => {
const audit = auditToolSelfHops(`
function buildRequest() {
function decoy() {
return { url: 'https://api.example.com/v1/items', method: 'GET' }
}
return unknownRequestFactory()
}
const tool = { id: 'test_tool', request: buildRequest() }
`)
expect(audit.violations).toEqual([
expect.objectContaining({
toolId: 'test_tool',
reason: 'unresolved-request-policy',
}),
])
})
})
describe('tool request access candidate scan', () => {
it('finds direct request member access', () => {
expect(mayAccessToolRequest('const url = tool.request.url')).toBe(true)
})
})
You can’t perform that action at this time.
