-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathindex.spec.js
More file actions
558 lines (484 loc) · 17.4 KB
/
index.spec.js
File metadata and controls
558 lines (484 loc) · 17.4 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
'use strict'
const assert = require('node:assert/strict')
const { rejects } = require('node:assert/strict')
const msgpack = require('@msgpack/msgpack')
const { afterEach, beforeEach, describe, it } = require('mocha')
const sinon = require('sinon')
const NoopAIGuard = require('../../src/aiguard/noop')
const AIGuard = require('../../src/aiguard/sdk')
const agent = require('../plugins/agent')
const { assertObjectContains } = require('../../../../integration-tests/helpers')
const tracerVersion = require('../../../../package.json').version
const telemetryMetrics = require('../../src/telemetry/metrics')
const appsecNamespace = telemetryMetrics.manager.namespace('appsec')
const { USER_KEEP } = require('../../../../ext/priority')
const { SAMPLING_MECHANISM_AI_GUARD, DECISION_MAKER_KEY } = require('../../src/constants')
describe('AIGuard SDK', () => {
const config = {
flushInterval: 0,
service: 'ai_guard_demo',
env: 'test',
apiKey: 'API_KEY',
appKey: 'APP_KEY',
protocolVersion: '0.4',
experimental: {
aiguard: {
enabled: true,
endpoint: 'https://aiguard.com',
maxMessagesLength: 16,
maxContentSize: 512 * 1024,
timeout: 10_000,
},
},
}
let tracer
let aiguard
let count, inc
const toolCall = [
{ role: 'system', content: 'You are a beautiful AI assistant' },
{ role: 'user', content: 'What is 2 + 2' },
{
role: 'assistant',
tool_calls: [
{
id: 'call_1',
function: {
name: 'calc',
arguments: '{ "operator": "+", "args": [2, 2] }',
},
},
],
},
]
const toolOutput = [
...toolCall,
{ role: 'tool', tool_call_id: 'call_1', content: '5' },
]
const prompt = [
...toolOutput,
{ role: 'assistant', content: '2 + 2 is 5' },
{ role: 'user', content: 'Are you sure?' },
]
let originalFetch
beforeEach(() => {
tracer = require('../../../dd-trace')
tracer.init(config)
originalFetch = global.fetch
global.fetch = sinon.stub()
inc = sinon.spy()
count = sinon.stub(appsecNamespace, 'count').returns({
inc,
})
appsecNamespace.metrics.clear()
aiguard = new AIGuard(tracer, config)
return agent.load(null, [])
})
afterEach(() => {
global.fetch = originalFetch
sinon.restore()
agent.close()
})
const mockFetch = (options) => {
if (options.error) {
global.fetch.rejects(options.error)
} else {
global.fetch.resolves({
status: options.status ?? 200,
json: sinon.stub().resolves(options.body),
})
}
}
const assertFetch = (messages, url) => {
const postData = JSON.stringify(
{ data: { attributes: { messages, meta: { service: config.service, env: config.env } } } }
)
sinon.assert.calledOnceWithExactly(global.fetch,
url ?? `${config.experimental.aiguard.endpoint}/evaluate`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'DD-API-KEY': config.apiKey,
'DD-APPLICATION-KEY': config.appKey,
'DD-AI-GUARD-VERSION': tracerVersion,
'DD-AI-GUARD-SOURCE': 'SDK',
'DD-AI-GUARD-LANGUAGE': 'nodejs',
},
body: postData,
signal: sinon.match.instanceOf(AbortSignal),
}
)
}
const assertAIGuardSpan = async (meta, metaStruct = null) => {
await agent.assertFirstTraceSpan(span => {
assert.strictEqual(span.name, 'ai_guard')
assert.strictEqual(span.resource, 'ai_guard')
assertObjectContains(span.meta, meta)
if (metaStruct) {
assert.deepStrictEqual(msgpack.decode(span.meta_struct.ai_guard), metaStruct)
}
}, { rejectFirst: true })
}
const assertTelemetry = (metric, tags) => {
sinon.assert.calledWith(count, metric, tags)
}
const testSuite = [
{ action: 'ALLOW', reason: 'Go ahead', tagProbs: {} },
{ action: 'DENY', reason: 'Nope', tagProbs: { deny_everything: 0.8, test_deny: 0.2 } },
{ action: 'ABORT', reason: 'Kill it with fire', tagProbs: { alarm_tag: 0.3, abort_everything: 0.7 } },
].flatMap(r => [
{ ...r, blocking: true },
{ ...r, blocking: false },
]).flatMap(r => [
{ ...r, suite: 'tool call', target: 'tool', messages: toolCall },
{ ...r, suite: 'tool output', target: 'tool', messages: toolOutput },
{ ...r, suite: 'prompt', target: 'prompt', messages: prompt },
])
for (const { action, reason, tagProbs, blocking, suite, target, messages } of testSuite) {
const tags = Object.keys(tagProbs)
it(`test evaluate '${suite}' with ${action} action (blocking: ${blocking})`, async () => {
mockFetch({ body: { data: { attributes: { action, reason, tags, tagProbs, is_blocking_enabled: blocking } } } })
const shouldBlock = action !== 'ALLOW' && blocking
if (shouldBlock) {
await rejects(
() => aiguard.evaluate(messages, { block: true }),
err => err.name === 'AIGuardAbortError' && err.reason === reason && err.tags === tags &&
err.tagProbabilities === tagProbs && JSON.stringify(err.sds) === '[]'
)
} else {
const evaluation = await aiguard.evaluate(messages, { block: true })
assert.strictEqual(evaluation.action, action)
assert.strictEqual(evaluation.reason, reason)
if (tagProbs) {
assert.strictEqual(evaluation.tags, tags)
assert.strictEqual(evaluation.tagProbabilities, tagProbs)
}
assert.deepStrictEqual(evaluation.sds, [])
}
assertTelemetry('ai_guard.requests', { error: false, action, block: shouldBlock })
assertFetch(messages)
await assertAIGuardSpan({
'ai_guard.target': target,
'ai_guard.action': action,
'ai_guard.reason': reason,
...(target === 'tool' ? { 'ai_guard.tool_name': 'calc' } : {}),
...(shouldBlock ? { 'ai_guard.blocked': 'true', 'error.type': 'AIGuardAbortError' } : {}),
},
{
messages,
...(tags.length > 0 ? { attack_categories: tags } : {}),
...(Object.keys(tagProbs).length > 0 ? { tag_probs: tagProbs } : {}),
})
})
}
const blockDefaultsSuite = [
{ description: 'no options', opts: undefined, shouldBlock: true },
{ description: 'empty options', opts: {}, shouldBlock: true },
{ description: 'explicit block: false', opts: { block: false }, shouldBlock: false },
]
for (const { description, opts, shouldBlock } of blockDefaultsSuite) {
it(`test evaluate block defaults to remote is_blocking_enabled (${description})`, async () => {
mockFetch({
body: {
data: {
attributes: { action: 'DENY', reason: 'Nope', tags: ['deny'], is_blocking_enabled: true },
},
},
})
if (shouldBlock) {
await rejects(
() => aiguard.evaluate(prompt, opts),
err => err.name === 'AIGuardAbortError' && err.reason === 'Nope'
)
} else {
const evaluation = await aiguard.evaluate(prompt, opts)
assert.strictEqual(evaluation.action, 'DENY')
}
assertTelemetry('ai_guard.requests', { error: false, action: 'DENY', block: shouldBlock })
})
}
it('test evaluate with sds_findings', async () => {
const sdsFindings = [
{
rule_display_name: 'Email Address',
rule_tag: 'email_address',
category: 'pii',
matched_text: 'john.smith@acmebank.com',
location: { start_index: 35, end_index_exclusive: 58, path: 'messages[0].content' },
},
{
rule_display_name: 'Social Security Number',
rule_tag: 'social_security_number',
category: 'pii',
matched_text: '456-78-9012',
location: { start_index: 73, end_index_exclusive: 84, path: 'messages[0].content' },
},
]
const messages = [{ role: 'user', content: 'My SSN is 456-78-9012 and email john.smith@acmebank.com' }]
mockFetch({
body: {
data: {
attributes: {
action: 'ALLOW',
reason: 'No rule match.',
tags: [],
sds_findings: sdsFindings,
is_blocking_enabled: true,
},
},
},
})
const result = await aiguard.evaluate(messages)
assert.deepStrictEqual(result.sds, sdsFindings)
await assertAIGuardSpan(
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
{ messages, sds: sdsFindings }
)
})
it('test evaluate with empty sds_findings', async () => {
const messages = [{ role: 'user', content: 'Hello' }]
mockFetch({
body: {
data: {
attributes: { action: 'ALLOW', reason: 'OK', tags: [], sds_findings: [], is_blocking_enabled: false },
},
},
})
const result = await aiguard.evaluate(messages)
assert.deepStrictEqual(result.sds, [])
await assertAIGuardSpan(
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
{ messages }
)
})
it('test evaluate with sds_findings in abort error', async () => {
const sdsFindings = [
{
rule_display_name: 'Credit Card Number',
rule_tag: 'credit_card',
category: 'pii',
matched_text: '4111111111111111',
location: { start_index: 10, end_index_exclusive: 26, path: 'messages[0].content[0].text' },
},
]
const messages = [{ role: 'user', content: 'My card is 4111111111111111' }]
mockFetch({
body: {
data: {
attributes: {
action: 'ABORT',
reason: 'PII detected',
tags: ['pii'],
sds_findings: sdsFindings,
is_blocking_enabled: true,
},
},
},
})
await rejects(
() => aiguard.evaluate(messages, { block: true }),
err => err.name === 'AIGuardAbortError' && JSON.stringify(err.sds) === JSON.stringify(sdsFindings)
)
})
it('test evaluate with API error', async () => {
const errors = [{ status: 400, title: 'Internal server error' }]
mockFetch({
status: 400,
body: { errors },
})
await rejects(
() => aiguard.evaluate(toolCall),
err =>
err.name === 'AIGuardClientError' && JSON.stringify(err.errors) === JSON.stringify(errors)
)
assertTelemetry('ai_guard.requests', { error: true })
assertFetch(toolCall)
await assertAIGuardSpan({
'ai_guard.target': 'tool',
'error.type': 'AIGuardClientError',
})
})
it('test evaluate with API exception', async () => {
mockFetch({
error: new Error('Boom!!!'),
})
await rejects(
() => aiguard.evaluate(toolCall),
err =>
err.name === 'AIGuardClientError' && err.message === 'Unexpected error calling AI Guard service: Boom!!!',
)
assertTelemetry('ai_guard.requests', { error: true })
assertFetch(toolCall)
await assertAIGuardSpan({
'ai_guard.target': 'tool',
'error.type': 'AIGuardClientError',
})
})
it('test evaluate with invalid JSON', async () => {
mockFetch({ body: { message: 'This is an invalid JSON' } })
await rejects(
() => aiguard.evaluate(toolCall),
err => err.name === 'AIGuardClientError'
)
assertTelemetry('ai_guard.requests', { error: true })
assertFetch(toolCall)
await assertAIGuardSpan({
'ai_guard.target': 'tool',
'error.type': 'AIGuardClientError',
})
})
it('test evaluate with with missing action or response', async () => {
mockFetch({ body: { data: { attributes: { reason: 'I miss something' } } } })
await rejects(
() => aiguard.evaluate(toolCall),
err => err.name === 'AIGuardClientError'
)
assertTelemetry('ai_guard.requests', { error: true })
assertFetch(toolCall)
await assertAIGuardSpan({
'ai_guard.target': 'tool',
'error.type': 'AIGuardClientError',
})
})
it('test noop implementation', async () => {
const noop = new NoopAIGuard()
const result = await noop.evaluate(prompt)
result.action === 'ALLOW'
result.reason === 'AI Guard is not enabled'
})
it('test message length truncation', async () => {
const maxMessages = config.experimental.aiguard.maxMessagesLength
const messages = Array.from({ length: maxMessages + 1 }, (_, i) => ({
role: 'user',
content: `This is a prompt: ${i}`,
}))
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', is_blocking_enabled: false } } },
})
await aiguard.evaluate(messages)
assertTelemetry('ai_guard.truncated', { type: 'messages' })
assertFetch(messages)
await assertAIGuardSpan(
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
{ messages: messages.slice(-maxMessages) }
)
})
it('test message content truncation', async () => {
const maxContent = config.experimental.aiguard.maxContentSize
const content = Array(maxContent + 1).fill('A').join('')
const messages = [{ role: 'user', content }]
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', is_blocking_enabled: false } } },
})
await aiguard.evaluate(messages)
assertTelemetry('ai_guard.truncated', { type: 'content' })
assertFetch(messages)
await assertAIGuardSpan(
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
{ messages: [{ role: 'user', content: content.slice(0, maxContent) }] }
)
})
it('test message immutability', async () => {
const messages = [{
role: 'assistant',
tool_calls: [{ id: 'call_1', function: { name: 'shell', arguments: '{"cmd": "ls -lah"}' } }],
}]
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', is_blocking_enabled: false } } },
})
await tracer.trace('test', async () => {
await aiguard.evaluate(messages)
// update messages before flushing
messages[0].tool_calls.push({ id: 'call_2', function: { name: 'shell', arguments: '{"cmd": "rm -rf"}' } })
messages.push({ role: 'tool', tool_call_id: 'call_1', content: 'dir1, dir2, dir3' })
})
await agent.assertSomeTraces(traces => {
const span = traces[0][1] // second span in the trace
const metaStruct = msgpack.decode(span.meta_struct.ai_guard)
assert.equal(metaStruct.messages.length, 1)
assert.equal(metaStruct.messages[0].tool_calls.length, 1)
})
})
it('test missing required fields uses noop as default', async () => {
const client = new AIGuard(tracer, { aiguard: { endpoint: 'http://aiguard' } })
const result = await client.evaluate(toolCall)
assert.strictEqual(result.action, 'ALLOW')
assert.strictEqual(result.reason, 'AI Guard is not enabled')
})
const sites = [
{ site: 'datad0g.com', endpoint: 'https://app.datad0g.com/api/v2/ai-guard' },
{ site: 'datadoghq.com', endpoint: 'https://app.datadoghq.com/api/v2/ai-guard' },
]
for (const { site, endpoint } of sites) {
it(`test endpoint discovery: ${site}`, async () => {
const newConfig = { site, ...config }
delete newConfig.experimental.aiguard.endpoint
const client = new AIGuard(tracer, newConfig)
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', is_blocking_enabled: false } } },
})
await client.evaluate(toolCall)
assertFetch(toolCall, `${endpoint}/evaluate`)
})
}
describe('manual keep on root span', () => {
const assertRootSpanKept = async () => {
await agent.assertSomeTraces(traces => {
const rootSpan = traces[0][0]
assert.strictEqual(rootSpan.metrics._sampling_priority_v1, USER_KEEP)
assert.strictEqual(rootSpan.meta[DECISION_MAKER_KEY], `-${SAMPLING_MECHANISM_AI_GUARD}`)
})
}
it('sets USER_KEEP on root span after ALLOW evaluation', async () => {
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', tags: [], is_blocking_enabled: false } } },
})
await tracer.trace('root', async () => {
await aiguard.evaluate(prompt)
})
await assertRootSpanKept()
})
it('sets USER_KEEP on root span after DENY evaluation (non-blocking)', async () => {
mockFetch({
body: {
data: { attributes: { action: 'DENY', reason: 'denied', tags: ['deny_tag'], is_blocking_enabled: false } },
},
})
await tracer.trace('root', async () => {
await aiguard.evaluate(prompt, { block: false })
})
await assertRootSpanKept()
})
it('keeps trace even when auto-sampling would drop it', async () => {
// Configure sampler to drop all traces (0% sample rate)
tracer._tracer._prioritySampler.configure('test', { sampleRate: 0 })
try {
mockFetch({
body: { data: { attributes: { action: 'ALLOW', reason: 'OK', tags: [], is_blocking_enabled: false } } },
})
await tracer.trace('root', async () => {
await aiguard.evaluate(prompt)
})
await assertRootSpanKept()
} finally {
tracer._tracer._prioritySampler.configure('test', {})
}
})
it('sets USER_KEEP on root span after ABORT evaluation (blocking)', async () => {
mockFetch({
body: {
data: { attributes: { action: 'ABORT', reason: 'blocked', tags: ['tag'], is_blocking_enabled: true } },
},
})
await tracer.trace('root', async () => {
try {
await aiguard.evaluate(prompt, { block: true })
} catch {
// expected AIGuardAbortError
}
})
await assertRootSpanKept()
})
})
})