Skip to content

Commit 94c9de4

Browse files
authored
chore: add the SDS tags returned by the evaluator service to the ai_guard span (#7702)
chore: add the SDS tags returned by the evaluator service to the ai_guard span fix lint Co-authored-by: oceane.bordeau <[email protected]>
1 parent 44733b7 commit 94c9de4

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/dd-trace/src/aiguard/sdk.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class AIGuard extends NoopAIGuard {
175175
`AI Guard service call failed, status ${response.status}`,
176176
{ errors: response.body?.errors })
177177
}
178-
let action, reason, tags, blockingEnabled
178+
let action, reason, tags, sdsFindings, blockingEnabled
179179
try {
180180
const attr = response.body.data.attributes
181181
if (!attr.action) {
@@ -184,6 +184,7 @@ class AIGuard extends NoopAIGuard {
184184
action = attr.action
185185
reason = attr.reason
186186
tags = attr.tags
187+
sdsFindings = attr.sds_findings
187188
blockingEnabled = attr.is_blocking_enabled ?? false
188189
} catch (e) {
189190
appsecMetrics.count(AI_GUARD_TELEMETRY_REQUESTS, { error: true }).inc(1)
@@ -198,6 +199,9 @@ class AIGuard extends NoopAIGuard {
198199
if (tags?.length > 0) {
199200
metaStruct.attack_categories = tags
200201
}
202+
if (sdsFindings?.length > 0) {
203+
metaStruct.sds = sdsFindings
204+
}
201205
if (shouldBlock) {
202206
span.setTag(AI_GUARD_BLOCKED_TAG_KEY, 'true')
203207
throw new AIGuardAbortError(reason, tags)

packages/dd-trace/test/aiguard/index.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,64 @@ describe('AIGuard SDK', () => {
189189
})
190190
}
191191

192+
it('test evaluate with sds_findings', async () => {
193+
const sdsFindings = [
194+
{
195+
rule_display_name: 'Email Address',
196+
rule_tag: 'email_address',
197+
category: 'pii',
198+
matched_text: '[email protected]',
199+
location: { start_index: 35, end_index_exclusive: 58, path: 'messages[0].content' },
200+
},
201+
{
202+
rule_display_name: 'Social Security Number',
203+
rule_tag: 'social_security_number',
204+
category: 'pii',
205+
matched_text: '456-78-9012',
206+
location: { start_index: 73, end_index_exclusive: 84, path: 'messages[0].content' },
207+
},
208+
]
209+
const messages = [{ role: 'user', content: 'My SSN is 456-78-9012 and email [email protected]' }]
210+
mockFetch({
211+
body: {
212+
data: {
213+
attributes: {
214+
action: 'ALLOW',
215+
reason: 'No rule match.',
216+
tags: [],
217+
sds_findings: sdsFindings,
218+
is_blocking_enabled: true,
219+
},
220+
},
221+
},
222+
})
223+
224+
await aiguard.evaluate(messages)
225+
226+
await assertAIGuardSpan(
227+
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
228+
{ messages, sds: sdsFindings }
229+
)
230+
})
231+
232+
it('test evaluate with empty sds_findings', async () => {
233+
const messages = [{ role: 'user', content: 'Hello' }]
234+
mockFetch({
235+
body: {
236+
data: {
237+
attributes: { action: 'ALLOW', reason: 'OK', tags: [], sds_findings: [], is_blocking_enabled: false },
238+
},
239+
},
240+
})
241+
242+
await aiguard.evaluate(messages)
243+
244+
await assertAIGuardSpan(
245+
{ 'ai_guard.target': 'prompt', 'ai_guard.action': 'ALLOW' },
246+
{ messages }
247+
)
248+
})
249+
192250
it('test evaluate with API error', async () => {
193251
const errors = [{ status: 400, title: 'Internal server error' }]
194252
mockFetch({

0 commit comments

Comments
 (0)