@dotsetlabs/tollgate 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +885 -0
- package/dist/analyzers/filesystem.d.ts +26 -0
- package/dist/analyzers/filesystem.d.ts.map +1 -0
- package/dist/analyzers/filesystem.js +284 -0
- package/dist/analyzers/filesystem.js.map +1 -0
- package/dist/analyzers/http.d.ts +90 -0
- package/dist/analyzers/http.d.ts.map +1 -0
- package/dist/analyzers/http.js +433 -0
- package/dist/analyzers/http.js.map +1 -0
- package/dist/analyzers/index.d.ts +101 -0
- package/dist/analyzers/index.d.ts.map +1 -0
- package/dist/analyzers/index.js +342 -0
- package/dist/analyzers/index.js.map +1 -0
- package/dist/analyzers/loader.d.ts +114 -0
- package/dist/analyzers/loader.d.ts.map +1 -0
- package/dist/analyzers/loader.js +184 -0
- package/dist/analyzers/loader.js.map +1 -0
- package/dist/analyzers/prompt-injection.d.ts +95 -0
- package/dist/analyzers/prompt-injection.d.ts.map +1 -0
- package/dist/analyzers/prompt-injection.js +725 -0
- package/dist/analyzers/prompt-injection.js.map +1 -0
- package/dist/analyzers/sdk.d.ts +230 -0
- package/dist/analyzers/sdk.d.ts.map +1 -0
- package/dist/analyzers/sdk.js +283 -0
- package/dist/analyzers/sdk.js.map +1 -0
- package/dist/analyzers/shell.d.ts +20 -0
- package/dist/analyzers/shell.d.ts.map +1 -0
- package/dist/analyzers/shell.js +297 -0
- package/dist/analyzers/shell.js.map +1 -0
- package/dist/analyzers/sql.d.ts +37 -0
- package/dist/analyzers/sql.d.ts.map +1 -0
- package/dist/analyzers/sql.js +455 -0
- package/dist/analyzers/sql.js.map +1 -0
- package/dist/analyzers/types.d.ts +117 -0
- package/dist/analyzers/types.d.ts.map +1 -0
- package/dist/analyzers/types.js +46 -0
- package/dist/analyzers/types.js.map +1 -0
- package/dist/approval/interactive.d.ts +72 -0
- package/dist/approval/interactive.d.ts.map +1 -0
- package/dist/approval/interactive.js +550 -0
- package/dist/approval/interactive.js.map +1 -0
- package/dist/approval/terminal.d.ts +59 -0
- package/dist/approval/terminal.d.ts.map +1 -0
- package/dist/approval/terminal.js +238 -0
- package/dist/approval/terminal.js.map +1 -0
- package/dist/approval/types.d.ts +66 -0
- package/dist/approval/types.d.ts.map +1 -0
- package/dist/approval/types.js +2 -0
- package/dist/approval/types.js.map +1 -0
- package/dist/audit/exporter.d.ts +138 -0
- package/dist/audit/exporter.d.ts.map +1 -0
- package/dist/audit/exporter.js +366 -0
- package/dist/audit/exporter.js.map +1 -0
- package/dist/audit/logger.d.ts +156 -0
- package/dist/audit/logger.d.ts.map +1 -0
- package/dist/audit/logger.js +406 -0
- package/dist/audit/logger.js.map +1 -0
- package/dist/audit/redaction.d.ts +110 -0
- package/dist/audit/redaction.d.ts.map +1 -0
- package/dist/audit/redaction.js +307 -0
- package/dist/audit/redaction.js.map +1 -0
- package/dist/audit/schema.d.ts +76 -0
- package/dist/audit/schema.d.ts.map +1 -0
- package/dist/audit/schema.js +122 -0
- package/dist/audit/schema.js.map +1 -0
- package/dist/cli/commands/doctor.d.ts +34 -0
- package/dist/cli/commands/doctor.d.ts.map +1 -0
- package/dist/cli/commands/doctor.js +431 -0
- package/dist/cli/commands/doctor.js.map +1 -0
- package/dist/cli/commands/export.d.ts +18 -0
- package/dist/cli/commands/export.d.ts.map +1 -0
- package/dist/cli/commands/export.js +63 -0
- package/dist/cli/commands/export.js.map +1 -0
- package/dist/cli/commands/init.d.ts +12 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/init.js +102 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/logs.d.ts +11 -0
- package/dist/cli/commands/logs.d.ts.map +1 -0
- package/dist/cli/commands/logs.js +60 -0
- package/dist/cli/commands/logs.js.map +1 -0
- package/dist/cli/commands/scan.d.ts +29 -0
- package/dist/cli/commands/scan.d.ts.map +1 -0
- package/dist/cli/commands/scan.js +251 -0
- package/dist/cli/commands/scan.js.map +1 -0
- package/dist/cli/commands/serve.d.ts +26 -0
- package/dist/cli/commands/serve.d.ts.map +1 -0
- package/dist/cli/commands/serve.js +424 -0
- package/dist/cli/commands/serve.js.map +1 -0
- package/dist/cli/commands/start.d.ts +20 -0
- package/dist/cli/commands/start.d.ts.map +1 -0
- package/dist/cli/commands/start.js +82 -0
- package/dist/cli/commands/start.js.map +1 -0
- package/dist/cli/commands/stats.d.ts +10 -0
- package/dist/cli/commands/stats.d.ts.map +1 -0
- package/dist/cli/commands/stats.js +42 -0
- package/dist/cli/commands/stats.js.map +1 -0
- package/dist/cli/commands/templates.d.ts +26 -0
- package/dist/cli/commands/templates.d.ts.map +1 -0
- package/dist/cli/commands/templates.js +221 -0
- package/dist/cli/commands/templates.js.map +1 -0
- package/dist/cli/commands/validate.d.ts +12 -0
- package/dist/cli/commands/validate.d.ts.map +1 -0
- package/dist/cli/commands/validate.js +107 -0
- package/dist/cli/commands/validate.js.map +1 -0
- package/dist/cli/commands/wrap.d.ts +19 -0
- package/dist/cli/commands/wrap.d.ts.map +1 -0
- package/dist/cli/commands/wrap.js +59 -0
- package/dist/cli/commands/wrap.js.map +1 -0
- package/dist/cli/index.d.ts +17 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +202 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/ui.d.ts +139 -0
- package/dist/cli/ui.d.ts.map +1 -0
- package/dist/cli/ui.js +271 -0
- package/dist/cli/ui.js.map +1 -0
- package/dist/constants.d.ts +33 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +54 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors.d.ts +28 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/orchestrator/index.d.ts +11 -0
- package/dist/orchestrator/index.d.ts.map +1 -0
- package/dist/orchestrator/index.js +10 -0
- package/dist/orchestrator/index.js.map +1 -0
- package/dist/orchestrator/manager.d.ts +127 -0
- package/dist/orchestrator/manager.d.ts.map +1 -0
- package/dist/orchestrator/manager.js +498 -0
- package/dist/orchestrator/manager.js.map +1 -0
- package/dist/orchestrator/types.d.ts +141 -0
- package/dist/orchestrator/types.d.ts.map +1 -0
- package/dist/orchestrator/types.js +9 -0
- package/dist/orchestrator/types.js.map +1 -0
- package/dist/policy/engine.d.ts +55 -0
- package/dist/policy/engine.d.ts.map +1 -0
- package/dist/policy/engine.js +288 -0
- package/dist/policy/engine.js.map +1 -0
- package/dist/policy/natural-language.d.ts +141 -0
- package/dist/policy/natural-language.d.ts.map +1 -0
- package/dist/policy/natural-language.js +552 -0
- package/dist/policy/natural-language.js.map +1 -0
- package/dist/policy/parser.d.ts +141 -0
- package/dist/policy/parser.d.ts.map +1 -0
- package/dist/policy/parser.js +314 -0
- package/dist/policy/parser.js.map +1 -0
- package/dist/policy/types.d.ts +428 -0
- package/dist/policy/types.d.ts.map +1 -0
- package/dist/policy/types.js +32 -0
- package/dist/policy/types.js.map +1 -0
- package/dist/policy/validator.d.ts +72 -0
- package/dist/policy/validator.d.ts.map +1 -0
- package/dist/policy/validator.js +453 -0
- package/dist/policy/validator.js.map +1 -0
- package/dist/proxy/bridge.d.ts +84 -0
- package/dist/proxy/bridge.d.ts.map +1 -0
- package/dist/proxy/bridge.js +217 -0
- package/dist/proxy/bridge.js.map +1 -0
- package/dist/proxy/client.d.ts +130 -0
- package/dist/proxy/client.d.ts.map +1 -0
- package/dist/proxy/client.js +290 -0
- package/dist/proxy/client.js.map +1 -0
- package/dist/proxy/server.d.ts +111 -0
- package/dist/proxy/server.d.ts.map +1 -0
- package/dist/proxy/server.js +444 -0
- package/dist/proxy/server.js.map +1 -0
- package/dist/scanner.d.ts +91 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +373 -0
- package/dist/scanner.js.map +1 -0
- package/dist/session/index.d.ts +32 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +31 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/manager.d.ts +166 -0
- package/dist/session/manager.d.ts.map +1 -0
- package/dist/session/manager.js +454 -0
- package/dist/session/manager.js.map +1 -0
- package/dist/session/sqlite-store.d.ts +54 -0
- package/dist/session/sqlite-store.d.ts.map +1 -0
- package/dist/session/sqlite-store.js +209 -0
- package/dist/session/sqlite-store.js.map +1 -0
- package/dist/session/types.d.ts +179 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/session/types.js +38 -0
- package/dist/session/types.js.map +1 -0
- package/dist/templates.d.ts +64 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +451 -0
- package/dist/templates.js.map +1 -0
- package/dist/utils/config.d.ts +57 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +104 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/errors.d.ts +18 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +35 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/logger.d.ts +144 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +300 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/wizard.d.ts +68 -0
- package/dist/wizard.d.ts.map +1 -0
- package/dist/wizard.js +395 -0
- package/dist/wizard.js.map +1 -0
- package/package.json +99 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audit Logger for Tollgate
|
|
3
|
+
*
|
|
4
|
+
* Logs all tool invocations and session grants to SQLite database
|
|
5
|
+
* for compliance, debugging, and analytics.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Schema versioning with automatic migrations
|
|
9
|
+
* - PII redaction for compliance (GDPR, SOC2)
|
|
10
|
+
* - Enhanced metadata for security analysis
|
|
11
|
+
*/
|
|
12
|
+
import Database from 'better-sqlite3';
|
|
13
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
14
|
+
import { dirname, join } from 'node:path';
|
|
15
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
16
|
+
import { getDataDir } from '../utils/config.js';
|
|
17
|
+
import { AUDIT_SCHEMA, SCHEMA_VERSION, } from './schema.js';
|
|
18
|
+
import { PiiRedactor } from './redaction.js';
|
|
19
|
+
/**
|
|
20
|
+
* AuditLogger records all tool calls and session grants to SQLite.
|
|
21
|
+
*
|
|
22
|
+
* Uses WAL mode for concurrent access and prepared statements for performance.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const logger = new AuditLogger();
|
|
27
|
+
*
|
|
28
|
+
* // Log a tool call attempt
|
|
29
|
+
* const id = logger.logAttempt(context, decision);
|
|
30
|
+
*
|
|
31
|
+
* // Later, log the result
|
|
32
|
+
* logger.logResult(id, 'approved', 'success', undefined, 150);
|
|
33
|
+
*
|
|
34
|
+
* // Log a session grant
|
|
35
|
+
* logger.logSessionGrant(grant);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export class AuditLogger {
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Private State
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
db;
|
|
43
|
+
insertToolCallStmt;
|
|
44
|
+
updateToolCallStmt;
|
|
45
|
+
insertSessionGrantStmt;
|
|
46
|
+
updateSessionGrantUsageStmt;
|
|
47
|
+
redactor;
|
|
48
|
+
enableRedaction;
|
|
49
|
+
storeRawArgs;
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// Constructor
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
constructor(optionsOrPath) {
|
|
54
|
+
// Handle both old (string path) and new (options object) signatures
|
|
55
|
+
const options = typeof optionsOrPath === 'string'
|
|
56
|
+
? { dbPath: optionsOrPath }
|
|
57
|
+
: optionsOrPath ?? {};
|
|
58
|
+
const path = options.dbPath ?? join(getDataDir(), 'audit.db');
|
|
59
|
+
this.enableRedaction = options.enableRedaction ?? true;
|
|
60
|
+
// By default, don't store raw args for compliance (GDPR, SOC2, HIPAA)
|
|
61
|
+
this.storeRawArgs = options.storeRawArgs ?? false;
|
|
62
|
+
this.redactor = this.enableRedaction
|
|
63
|
+
? new PiiRedactor(options.redactionOptions)
|
|
64
|
+
: null;
|
|
65
|
+
// Ensure directory exists
|
|
66
|
+
const dir = dirname(path);
|
|
67
|
+
if (!existsSync(dir)) {
|
|
68
|
+
mkdirSync(dir, { recursive: true });
|
|
69
|
+
}
|
|
70
|
+
this.db = new Database(path);
|
|
71
|
+
this.db.pragma('journal_mode = WAL');
|
|
72
|
+
// Initialize schema with migrations
|
|
73
|
+
this.initializeSchema();
|
|
74
|
+
// Prepared statements for tool calls (updated for new schema)
|
|
75
|
+
this.insertToolCallStmt = this.db.prepare(`
|
|
76
|
+
INSERT INTO tool_calls (
|
|
77
|
+
id, server, tool, args, args_redacted, policy_decision, policy_rule,
|
|
78
|
+
policy_reason, analyzer, risk_level, session_grant_id, correlation_id, client_id
|
|
79
|
+
)
|
|
80
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
81
|
+
`);
|
|
82
|
+
this.updateToolCallStmt = this.db.prepare(`
|
|
83
|
+
UPDATE tool_calls
|
|
84
|
+
SET user_decision = ?, result = ?, error_message = ?, duration_ms = ?
|
|
85
|
+
WHERE id = ?
|
|
86
|
+
`);
|
|
87
|
+
// Prepared statements for session grants
|
|
88
|
+
this.insertSessionGrantStmt = this.db.prepare(`
|
|
89
|
+
INSERT INTO session_grants (id, expires_at, server, scope, scope_value, tool, granted_by, original_request_id)
|
|
90
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
91
|
+
`);
|
|
92
|
+
this.updateSessionGrantUsageStmt = this.db.prepare(`
|
|
93
|
+
UPDATE session_grants SET usage_count = usage_count + 1 WHERE id = ?
|
|
94
|
+
`);
|
|
95
|
+
}
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// Schema Management
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
/**
|
|
100
|
+
* Initializes the database schema with version tracking and migrations.
|
|
101
|
+
*/
|
|
102
|
+
initializeSchema() {
|
|
103
|
+
// Check if this is a fresh database
|
|
104
|
+
const tableExists = this.db
|
|
105
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='tool_calls'")
|
|
106
|
+
.get();
|
|
107
|
+
if (!tableExists) {
|
|
108
|
+
// Fresh database - use current schema
|
|
109
|
+
this.db.exec(AUDIT_SCHEMA);
|
|
110
|
+
this.db
|
|
111
|
+
.prepare('INSERT OR REPLACE INTO schema_version (version) VALUES (?)')
|
|
112
|
+
.run(SCHEMA_VERSION);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
// Check if schema_version table exists
|
|
116
|
+
const versionTableExists = this.db
|
|
117
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='schema_version'")
|
|
118
|
+
.get();
|
|
119
|
+
let currentVersion = 1;
|
|
120
|
+
if (versionTableExists) {
|
|
121
|
+
const row = this.db
|
|
122
|
+
.prepare('SELECT MAX(version) as version FROM schema_version')
|
|
123
|
+
.get();
|
|
124
|
+
currentVersion = row?.version ?? 1;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
// Create version table for legacy databases
|
|
128
|
+
this.db.exec(`
|
|
129
|
+
CREATE TABLE IF NOT EXISTS schema_version (
|
|
130
|
+
version INTEGER PRIMARY KEY,
|
|
131
|
+
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
132
|
+
);
|
|
133
|
+
INSERT INTO schema_version (version) VALUES (1);
|
|
134
|
+
`);
|
|
135
|
+
}
|
|
136
|
+
// Apply migrations
|
|
137
|
+
if (currentVersion < 2) {
|
|
138
|
+
this.applyMigrationV2();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Applies migration to version 2 (compliance fields).
|
|
143
|
+
*/
|
|
144
|
+
applyMigrationV2() {
|
|
145
|
+
// Check if columns already exist (idempotent migration)
|
|
146
|
+
const columnInfo = this.db
|
|
147
|
+
.prepare("PRAGMA table_info(tool_calls)")
|
|
148
|
+
.all();
|
|
149
|
+
const existingColumns = new Set(columnInfo.map((c) => c.name));
|
|
150
|
+
const newColumns = [
|
|
151
|
+
{ name: 'args_redacted', sql: 'ALTER TABLE tool_calls ADD COLUMN args_redacted TEXT' },
|
|
152
|
+
{ name: 'policy_reason', sql: 'ALTER TABLE tool_calls ADD COLUMN policy_reason TEXT' },
|
|
153
|
+
{ name: 'analyzer', sql: 'ALTER TABLE tool_calls ADD COLUMN analyzer TEXT' },
|
|
154
|
+
{ name: 'risk_level', sql: 'ALTER TABLE tool_calls ADD COLUMN risk_level TEXT' },
|
|
155
|
+
{ name: 'correlation_id', sql: 'ALTER TABLE tool_calls ADD COLUMN correlation_id TEXT' },
|
|
156
|
+
{ name: 'client_id', sql: 'ALTER TABLE tool_calls ADD COLUMN client_id TEXT' },
|
|
157
|
+
];
|
|
158
|
+
for (const col of newColumns) {
|
|
159
|
+
if (!existingColumns.has(col.name)) {
|
|
160
|
+
this.db.exec(col.sql);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// Add new indexes (CREATE INDEX IF NOT EXISTS is safe to run)
|
|
164
|
+
this.db.exec(`
|
|
165
|
+
CREATE INDEX IF NOT EXISTS idx_tool_calls_risk_level ON tool_calls(risk_level);
|
|
166
|
+
CREATE INDEX IF NOT EXISTS idx_tool_calls_correlation ON tool_calls(correlation_id);
|
|
167
|
+
`);
|
|
168
|
+
// Update version
|
|
169
|
+
this.db
|
|
170
|
+
.prepare('INSERT OR REPLACE INTO schema_version (version) VALUES (?)')
|
|
171
|
+
.run(2);
|
|
172
|
+
}
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
// Tool Call Logging
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
/**
|
|
177
|
+
* Logs a tool call attempt.
|
|
178
|
+
*
|
|
179
|
+
* @param context - The tool call context
|
|
180
|
+
* @param decision - The policy decision
|
|
181
|
+
* @param sessionGrantId - ID of session grant that authorized this call (if any)
|
|
182
|
+
* @param metadata - Additional metadata for the audit record
|
|
183
|
+
* @returns The generated audit record ID
|
|
184
|
+
*/
|
|
185
|
+
logAttempt(context, decision, sessionGrantId, metadata) {
|
|
186
|
+
const id = uuidv4();
|
|
187
|
+
// By default, only store redacted args for compliance.
|
|
188
|
+
// Raw args are only stored if explicitly enabled (storeRawArgs: true).
|
|
189
|
+
let argsToStore = null;
|
|
190
|
+
let argsRedacted = null;
|
|
191
|
+
if (this.redactor) {
|
|
192
|
+
// Always generate redacted version when redaction is enabled
|
|
193
|
+
argsRedacted = this.redactor.redactObject(context.args);
|
|
194
|
+
}
|
|
195
|
+
if (this.storeRawArgs) {
|
|
196
|
+
// Only store raw args if explicitly enabled (non-compliant mode)
|
|
197
|
+
argsToStore = JSON.stringify(context.args);
|
|
198
|
+
}
|
|
199
|
+
else if (argsRedacted) {
|
|
200
|
+
// Store redacted version in the args column (compliant mode)
|
|
201
|
+
argsToStore = argsRedacted;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
// Redaction disabled, but also not storing raw - store as JSON
|
|
205
|
+
// This shouldn't normally happen, but handle it safely
|
|
206
|
+
argsToStore = JSON.stringify(context.args);
|
|
207
|
+
}
|
|
208
|
+
// Extract analysis metadata if available
|
|
209
|
+
const analyzer = decision.analysis?.analyzer ?? null;
|
|
210
|
+
const riskLevel = decision.analysis?.risk ?? null;
|
|
211
|
+
this.insertToolCallStmt.run(id, context.server, context.tool, argsToStore, argsRedacted, decision.action, decision.matchedRule ?? null, decision.reason ?? null, analyzer, riskLevel, sessionGrantId ?? null, metadata?.correlationId ?? null, metadata?.clientId ?? null);
|
|
212
|
+
return id;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Updates a tool call record with the final result.
|
|
216
|
+
*
|
|
217
|
+
* @param id - The audit record ID from logAttempt
|
|
218
|
+
* @param userDecision - The user's approval decision (if prompted)
|
|
219
|
+
* @param result - Whether the tool call succeeded or failed
|
|
220
|
+
* @param errorMessage - Error message (if result is 'error')
|
|
221
|
+
* @param durationMs - Total duration of the tool call
|
|
222
|
+
*/
|
|
223
|
+
logResult(id, userDecision, result, errorMessage, durationMs) {
|
|
224
|
+
this.updateToolCallStmt.run(userDecision, result, errorMessage ?? null, durationMs ?? null, id);
|
|
225
|
+
}
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// Session Grant Logging
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
/**
|
|
230
|
+
* Logs a new session grant.
|
|
231
|
+
*
|
|
232
|
+
* @param grant - The session grant to log
|
|
233
|
+
*/
|
|
234
|
+
logSessionGrant(grant) {
|
|
235
|
+
this.insertSessionGrantStmt.run(grant.id, grant.expiresAt?.toISOString() ?? null, grant.server, grant.scope, grant.scopeValue ?? null, grant.tool ?? null, grant.grantedBy, grant.originalRequestId ?? null);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Increments the usage count for a session grant.
|
|
239
|
+
*
|
|
240
|
+
* @param grantId - The session grant ID
|
|
241
|
+
*/
|
|
242
|
+
logSessionGrantUsage(grantId) {
|
|
243
|
+
this.updateSessionGrantUsageStmt.run(grantId);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Marks a session grant as revoked.
|
|
247
|
+
*
|
|
248
|
+
* @param grantId - The session grant ID
|
|
249
|
+
* @param revokedBy - Who revoked it (e.g., 'user', 'timeout', 'policy')
|
|
250
|
+
*/
|
|
251
|
+
revokeSessionGrant(grantId, revokedBy) {
|
|
252
|
+
const stmt = this.db.prepare(`
|
|
253
|
+
UPDATE session_grants
|
|
254
|
+
SET revoked_at = datetime('now'), revoked_by = ?
|
|
255
|
+
WHERE id = ?
|
|
256
|
+
`);
|
|
257
|
+
stmt.run(revokedBy, grantId);
|
|
258
|
+
}
|
|
259
|
+
// ---------------------------------------------------------------------------
|
|
260
|
+
// Query Methods
|
|
261
|
+
// ---------------------------------------------------------------------------
|
|
262
|
+
/**
|
|
263
|
+
* Gets recent tool calls with optional filtering.
|
|
264
|
+
*/
|
|
265
|
+
getRecentCalls(limitOrOptions = 50) {
|
|
266
|
+
// Handle both old (number) and new (options) signatures
|
|
267
|
+
const options = typeof limitOrOptions === 'number'
|
|
268
|
+
? { limit: limitOrOptions }
|
|
269
|
+
: limitOrOptions;
|
|
270
|
+
const limit = options.limit ?? 50;
|
|
271
|
+
const conditions = [];
|
|
272
|
+
const params = [];
|
|
273
|
+
if (options.server) {
|
|
274
|
+
conditions.push('server = ?');
|
|
275
|
+
params.push(options.server);
|
|
276
|
+
}
|
|
277
|
+
if (options.since) {
|
|
278
|
+
conditions.push('timestamp >= ?');
|
|
279
|
+
params.push(options.since.toISOString());
|
|
280
|
+
}
|
|
281
|
+
if (options.until) {
|
|
282
|
+
conditions.push('timestamp <= ?');
|
|
283
|
+
params.push(options.until.toISOString());
|
|
284
|
+
}
|
|
285
|
+
if (options.riskLevel) {
|
|
286
|
+
conditions.push('risk_level = ?');
|
|
287
|
+
params.push(options.riskLevel);
|
|
288
|
+
}
|
|
289
|
+
const whereClause = conditions.length > 0
|
|
290
|
+
? `WHERE ${conditions.join(' AND ')}`
|
|
291
|
+
: '';
|
|
292
|
+
const stmt = this.db.prepare(`
|
|
293
|
+
SELECT * FROM tool_calls
|
|
294
|
+
${whereClause}
|
|
295
|
+
ORDER BY timestamp DESC
|
|
296
|
+
LIMIT ?
|
|
297
|
+
`);
|
|
298
|
+
params.push(limit);
|
|
299
|
+
const rows = stmt.all(...params);
|
|
300
|
+
return rows.map((row) => ({
|
|
301
|
+
id: row.id,
|
|
302
|
+
timestamp: new Date(row.timestamp),
|
|
303
|
+
server: row.server,
|
|
304
|
+
tool: row.tool,
|
|
305
|
+
// Use redacted args if requested and available
|
|
306
|
+
args: options.includeRedacted && row.args_redacted
|
|
307
|
+
? JSON.parse(row.args_redacted)
|
|
308
|
+
: JSON.parse(row.args),
|
|
309
|
+
argsRedacted: row.args_redacted ?? undefined,
|
|
310
|
+
policyDecision: row.policy_decision,
|
|
311
|
+
policyRule: row.policy_rule ?? undefined,
|
|
312
|
+
policyReason: row.policy_reason ?? undefined,
|
|
313
|
+
analyzer: row.analyzer ?? undefined,
|
|
314
|
+
riskLevel: row.risk_level,
|
|
315
|
+
userDecision: row.user_decision,
|
|
316
|
+
result: row.result,
|
|
317
|
+
errorMessage: row.error_message ?? undefined,
|
|
318
|
+
durationMs: row.duration_ms ?? undefined,
|
|
319
|
+
sessionGrantId: row.session_grant_id ?? undefined,
|
|
320
|
+
correlationId: row.correlation_id ?? undefined,
|
|
321
|
+
clientId: row.client_id ?? undefined,
|
|
322
|
+
}));
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Gets active session grants for a server.
|
|
326
|
+
*
|
|
327
|
+
* @param server - Server name to filter by (optional)
|
|
328
|
+
* @returns Array of active grants
|
|
329
|
+
*/
|
|
330
|
+
getActiveSessionGrants(server) {
|
|
331
|
+
const sql = server
|
|
332
|
+
? `SELECT * FROM session_grants WHERE server = ? AND revoked_at IS NULL AND (expires_at IS NULL OR expires_at > datetime('now')) ORDER BY created_at DESC`
|
|
333
|
+
: `SELECT * FROM session_grants WHERE revoked_at IS NULL AND (expires_at IS NULL OR expires_at > datetime('now')) ORDER BY created_at DESC`;
|
|
334
|
+
const stmt = this.db.prepare(sql);
|
|
335
|
+
const rows = (server ? stmt.all(server) : stmt.all());
|
|
336
|
+
return rows.map((row) => ({
|
|
337
|
+
id: row.id,
|
|
338
|
+
createdAt: new Date(row.created_at),
|
|
339
|
+
expiresAt: row.expires_at ? new Date(row.expires_at) : null,
|
|
340
|
+
server: row.server,
|
|
341
|
+
scope: row.scope,
|
|
342
|
+
scopeValue: row.scope_value ?? undefined,
|
|
343
|
+
tool: row.tool ?? undefined,
|
|
344
|
+
grantedBy: row.granted_by,
|
|
345
|
+
originalRequestId: row.original_request_id ?? undefined,
|
|
346
|
+
usageCount: row.usage_count,
|
|
347
|
+
revokedAt: row.revoked_at ? new Date(row.revoked_at) : undefined,
|
|
348
|
+
revokedBy: row.revoked_by ?? undefined,
|
|
349
|
+
}));
|
|
350
|
+
}
|
|
351
|
+
// ---------------------------------------------------------------------------
|
|
352
|
+
// Statistics
|
|
353
|
+
// ---------------------------------------------------------------------------
|
|
354
|
+
/**
|
|
355
|
+
* Gets aggregate statistics for tool calls.
|
|
356
|
+
*/
|
|
357
|
+
getStats() {
|
|
358
|
+
const stmt = this.db.prepare(`
|
|
359
|
+
SELECT
|
|
360
|
+
COUNT(*) as total,
|
|
361
|
+
SUM(CASE WHEN policy_decision = 'allow' THEN 1 ELSE 0 END) as allowed,
|
|
362
|
+
SUM(CASE WHEN policy_decision = 'deny' OR user_decision = 'denied' OR user_decision = 'timeout' THEN 1 ELSE 0 END) as denied,
|
|
363
|
+
SUM(CASE WHEN policy_decision = 'prompt' THEN 1 ELSE 0 END) as prompted,
|
|
364
|
+
SUM(CASE WHEN session_grant_id IS NOT NULL THEN 1 ELSE 0 END) as session_authorized
|
|
365
|
+
FROM tool_calls
|
|
366
|
+
`);
|
|
367
|
+
const result = stmt.get();
|
|
368
|
+
return {
|
|
369
|
+
total: result.total,
|
|
370
|
+
allowed: result.allowed,
|
|
371
|
+
denied: result.denied,
|
|
372
|
+
prompted: result.prompted,
|
|
373
|
+
sessionAuthorized: result.session_authorized,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Gets session grant statistics.
|
|
378
|
+
*/
|
|
379
|
+
getSessionStats() {
|
|
380
|
+
const stmt = this.db.prepare(`
|
|
381
|
+
SELECT
|
|
382
|
+
COUNT(*) as total,
|
|
383
|
+
SUM(CASE WHEN revoked_at IS NULL AND (expires_at IS NULL OR expires_at > datetime('now')) THEN 1 ELSE 0 END) as active,
|
|
384
|
+
SUM(CASE WHEN revoked_at IS NULL AND expires_at IS NOT NULL AND expires_at <= datetime('now') THEN 1 ELSE 0 END) as expired,
|
|
385
|
+
SUM(CASE WHEN revoked_at IS NOT NULL THEN 1 ELSE 0 END) as revoked,
|
|
386
|
+
COALESCE(SUM(usage_count), 0) as total_usage
|
|
387
|
+
FROM session_grants
|
|
388
|
+
`);
|
|
389
|
+
const result = stmt.get();
|
|
390
|
+
return {
|
|
391
|
+
totalGrants: result.total,
|
|
392
|
+
activeGrants: result.active,
|
|
393
|
+
expiredGrants: result.expired,
|
|
394
|
+
revokedGrants: result.revoked,
|
|
395
|
+
totalUsage: result.total_usage,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
// ---------------------------------------------------------------------------
|
|
399
|
+
// Lifecycle
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
/** Closes the database connection. */
|
|
402
|
+
close() {
|
|
403
|
+
this.db.close();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/audit/logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EACL,YAAY,EACZ,cAAc,GAIf,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,WAAW,EAAyB,MAAM,gBAAgB,CAAC;AA0BpE;;;;;;;;;;;;;;;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,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,EAMtB,CAAC;QAEF,OAAO;YACL,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,iBAAiB,EAAE,MAAM,CAAC,kBAAkB;SAC7C,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,eAAe;QAOb,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC;;;;;;;;KAQ5B,CAAC,CAAC;QAEH,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,EAMtB,CAAC;QAEF,OAAO;YACL,WAAW,EAAE,MAAM,CAAC,KAAK;YACzB,YAAY,EAAE,MAAM,CAAC,MAAM;YAC3B,aAAa,EAAE,MAAM,CAAC,OAAO;YAC7B,aAAa,EAAE,MAAM,CAAC,OAAO;YAC7B,UAAU,EAAE,MAAM,CAAC,WAAW;SAC/B,CAAC;IACJ,CAAC;IAED,8EAA8E;IAC9E,YAAY;IACZ,8EAA8E;IAE9E,sCAAsC;IACtC,KAAK;QACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;IAClB,CAAC;CACF"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PII Redaction Module for Tollgate
|
|
3
|
+
*
|
|
4
|
+
* Detects and masks sensitive information in audit logs to comply with
|
|
5
|
+
* data protection regulations (GDPR, CCPA, SOC2).
|
|
6
|
+
*
|
|
7
|
+
* Supported patterns:
|
|
8
|
+
* - Passwords and secrets
|
|
9
|
+
* - API keys and tokens
|
|
10
|
+
* - Credit card numbers
|
|
11
|
+
* - Social Security Numbers (SSN)
|
|
12
|
+
* - Email addresses
|
|
13
|
+
* - Phone numbers
|
|
14
|
+
* - IP addresses (optional)
|
|
15
|
+
* - Private keys and certificates
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { redactPii, createRedactor } from './redaction.js';
|
|
20
|
+
*
|
|
21
|
+
* // Quick redaction with defaults
|
|
22
|
+
* const redacted = redactPii('password=secret123');
|
|
23
|
+
* // Returns: 'password=[REDACTED]'
|
|
24
|
+
*
|
|
25
|
+
* // Custom redactor with options
|
|
26
|
+
* const redactor = createRedactor({ redactEmails: true, redactIPs: true });
|
|
27
|
+
* const result = redactor.redact({ email: 'user@example.com' });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Configuration options for the PII redactor.
|
|
32
|
+
*/
|
|
33
|
+
export interface RedactionOptions {
|
|
34
|
+
/** Redact email addresses (default: true) */
|
|
35
|
+
redactEmails?: boolean;
|
|
36
|
+
/** Redact IP addresses (default: false - often needed for security) */
|
|
37
|
+
redactIPs?: boolean;
|
|
38
|
+
/** Redact phone numbers (default: true) */
|
|
39
|
+
redactPhones?: boolean;
|
|
40
|
+
/** Custom patterns to redact */
|
|
41
|
+
customPatterns?: RegExp[];
|
|
42
|
+
/** Replacement string (default: '[REDACTED]') */
|
|
43
|
+
replacement?: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* PII Redactor class for detecting and masking sensitive data.
|
|
47
|
+
*/
|
|
48
|
+
export declare class PiiRedactor {
|
|
49
|
+
private options;
|
|
50
|
+
private patterns;
|
|
51
|
+
constructor(options?: RedactionOptions);
|
|
52
|
+
/**
|
|
53
|
+
* Builds the list of patterns to apply based on options.
|
|
54
|
+
*/
|
|
55
|
+
private buildPatternList;
|
|
56
|
+
/**
|
|
57
|
+
* Redacts sensitive data from a string.
|
|
58
|
+
*
|
|
59
|
+
* @param input - The string to redact
|
|
60
|
+
* @returns The redacted string
|
|
61
|
+
*/
|
|
62
|
+
redactString(input: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Redacts sensitive data from an object by converting to JSON and back.
|
|
65
|
+
*
|
|
66
|
+
* @param input - The object to redact
|
|
67
|
+
* @returns The redacted object as a JSON string
|
|
68
|
+
*/
|
|
69
|
+
redactObject(input: Record<string, unknown>): string;
|
|
70
|
+
/**
|
|
71
|
+
* Checks if a string contains potentially sensitive data.
|
|
72
|
+
*
|
|
73
|
+
* @param input - The string to check
|
|
74
|
+
* @returns True if sensitive data was detected
|
|
75
|
+
*/
|
|
76
|
+
containsSensitiveData(input: string): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the list of pattern names being applied.
|
|
79
|
+
*/
|
|
80
|
+
getActivePatterns(): string[];
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a new PII redactor with the specified options.
|
|
84
|
+
*
|
|
85
|
+
* @param options - Redaction options
|
|
86
|
+
* @returns A configured PiiRedactor instance
|
|
87
|
+
*/
|
|
88
|
+
export declare function createRedactor(options?: RedactionOptions): PiiRedactor;
|
|
89
|
+
/**
|
|
90
|
+
* Redacts PII from a string using default settings.
|
|
91
|
+
*
|
|
92
|
+
* @param input - The string to redact
|
|
93
|
+
* @returns The redacted string
|
|
94
|
+
*/
|
|
95
|
+
export declare function redactPii(input: string): string;
|
|
96
|
+
/**
|
|
97
|
+
* Redacts PII from an object and returns a JSON string.
|
|
98
|
+
*
|
|
99
|
+
* @param input - The object to redact
|
|
100
|
+
* @returns The redacted JSON string
|
|
101
|
+
*/
|
|
102
|
+
export declare function redactPiiFromObject(input: Record<string, unknown>): string;
|
|
103
|
+
/**
|
|
104
|
+
* Checks if a string contains potentially sensitive data.
|
|
105
|
+
*
|
|
106
|
+
* @param input - The string to check
|
|
107
|
+
* @returns True if sensitive data was detected
|
|
108
|
+
*/
|
|
109
|
+
export declare function containsPii(input: string): boolean;
|
|
110
|
+
//# sourceMappingURL=redaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redaction.d.ts","sourceRoot":"","sources":["../../src/audit/redaction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;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