-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathaltimate-core-query-pii.ts
More file actions
56 lines (54 loc) · 2.33 KB
/
altimate-core-query-pii.ts
File metadata and controls
56 lines (54 loc) · 2.33 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
import z from "zod"
import { Tool } from "../../tool/tool"
import { Dispatcher } from "../native"
export const AltimateCoreQueryPiiTool = Tool.define("altimate_core_query_pii", {
description:
"Analyze query-level PII exposure using the Rust-based altimate-core engine. Checks if a SQL query accesses columns classified as PII and reports the exposure risk.",
parameters: z.object({
sql: z.string().describe("SQL query to check for PII access"),
schema_path: z.string().optional().describe("Path to YAML/JSON schema file"),
schema_context: z.record(z.string(), z.any()).optional().describe("Inline schema definition"),
}),
async execute(args, ctx) {
try {
const result = await Dispatcher.call("altimate_core.query_pii", {
sql: args.sql,
schema_path: args.schema_path ?? "",
schema_context: args.schema_context,
})
const data = result.data as Record<string, any>
const piiCols = data.pii_columns ?? data.exposures ?? []
const exposureCount = piiCols.length
return {
title: `Query PII: ${exposureCount === 0 ? "CLEAN" : `${exposureCount} exposure(s)`}`,
metadata: { success: result.success, exposure_count: exposureCount },
output: formatQueryPii(data),
}
} catch (e) {
const msg = e instanceof Error ? e.message : String(e)
return { title: "Query PII: ERROR", metadata: { success: false, exposure_count: 0 }, output: `Failed: ${msg}` }
}
},
})
function formatQueryPii(data: Record<string, any>): string {
if (data.error) return `Error: ${data.error}`
const piiCols = data.pii_columns ?? data.exposures ?? []
if (!piiCols.length) return "Query does not access PII columns."
const lines: string[] = []
if (data.risk_level) lines.push(`Risk level: ${data.risk_level}`)
lines.push("PII exposure detected:\n")
for (const e of piiCols) {
const classification = e.classification ?? e.category ?? "PII"
const table = e.table ?? "unknown"
const column = e.column ?? "unknown"
lines.push(` ${table}.${column}: ${classification}`)
if (e.suggested_masking) lines.push(` Masking: ${e.suggested_masking}`)
}
if (data.suggested_alternatives?.length) {
lines.push("\nSuggested alternatives:")
for (const alt of data.suggested_alternatives) {
lines.push(` - ${alt}`)
}
}
return lines.join("\n")
}