-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathsanitize.test.ts
More file actions
499 lines (444 loc) · 15.6 KB
/
sanitize.test.ts
File metadata and controls
499 lines (444 loc) · 15.6 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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Tests for telemetry sanitization functions.
*
* This test file focuses on validating PII protection through sanitization,
* particularly for hook names that may contain sensitive information like
* API keys, credentials, file paths, and command arguments.
*/
import { describe, it, expect } from 'vitest';
import { HookCallEvent, EVENT_HOOK_CALL } from './types.js';
import { HookType } from '../hooks/types.js';
import type { Config } from '../config/config.js';
/**
* Create a mock config for testing.
*
* @param logPromptsEnabled Whether telemetry logging of prompts is enabled.
* @returns Mock config object.
*/
function createMockConfig(logPromptsEnabled: boolean): Config {
return {
getTelemetryLogPromptsEnabled: () => logPromptsEnabled,
getSessionId: () => 'test-session-id',
getExperiments: () => undefined,
getExperimentsAsync: async () => undefined,
getModel: () => 'gemini-1.5-flash',
isInteractive: () => true,
getUserEmail: () => undefined,
getContentGeneratorConfig: () => undefined,
} as unknown as Config;
}
describe('Telemetry Sanitization', () => {
describe('HookCallEvent', () => {
describe('constructor', () => {
it('should create an event with all fields', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'test-hook',
{ tool_name: 'ReadFile' },
100,
true,
{ decision: 'allow' },
0,
'output',
'error',
undefined,
);
expect(event['event.name']).toBe('hook_call');
expect(event.hook_event_name).toBe('BeforeTool');
expect(event.hook_type).toBe('command');
expect(event.hook_name).toBe('test-hook');
expect(event.hook_input).toEqual({ tool_name: 'ReadFile' });
expect(event.hook_output).toEqual({ decision: 'allow' });
expect(event.exit_code).toBe(0);
expect(event.stdout).toBe('output');
expect(event.stderr).toBe('error');
expect(event.duration_ms).toBe(100);
expect(event.success).toBe(true);
expect(event.error).toBeUndefined();
});
it('should create an event with minimal fields', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'test-hook',
{ tool_name: 'ReadFile' },
100,
true,
);
expect(event.hook_output).toBeUndefined();
expect(event.exit_code).toBeUndefined();
expect(event.stdout).toBeUndefined();
expect(event.stderr).toBeUndefined();
expect(event.error).toBeUndefined();
});
});
describe('toOpenTelemetryAttributes with logPrompts=true', () => {
const config = createMockConfig(true);
it('should include all fields when logPrompts is enabled', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'/path/to/.gemini/hooks/check-secrets.sh --api-key=abc123',
{ tool_name: 'ReadFile', args: { file: 'secret.txt' } },
100,
true,
{ decision: 'allow' },
0,
'hook executed successfully',
'no errors',
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['event.name']).toBe(EVENT_HOOK_CALL);
expect(attributes['hook_event_name']).toBe('BeforeTool');
expect(attributes['hook_type']).toBe('command');
// With logPrompts=true, full hook name is included
expect(attributes['hook_name']).toBe(
'/path/to/.gemini/hooks/check-secrets.sh --api-key=abc123',
);
expect(attributes['duration_ms']).toBe(100);
expect(attributes['success']).toBe(true);
expect(attributes['exit_code']).toBe(0);
// PII-sensitive fields should be included
expect(attributes['hook_input']).toBeDefined();
expect(attributes['hook_output']).toBeDefined();
expect(attributes['stdout']).toBe('hook executed successfully');
expect(attributes['stderr']).toBe('no errors');
});
it('should include hook_input and hook_output as JSON strings', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'test-hook',
{ tool_name: 'ReadFile', args: { file: 'test.txt' } },
100,
true,
{ decision: 'allow', reason: 'approved' },
);
const attributes = event.toOpenTelemetryAttributes(config);
// Should be JSON stringified
// eslint-disable-next-line no-restricted-syntax
expect(typeof attributes['hook_input']).toBe('string');
// eslint-disable-next-line no-restricted-syntax
expect(typeof attributes['hook_output']).toBe('string');
const parsedInput = JSON.parse(attributes['hook_input'] as string);
expect(parsedInput).toEqual({
tool_name: 'ReadFile',
args: { file: 'test.txt' },
});
const parsedOutput = JSON.parse(attributes['hook_output'] as string);
expect(parsedOutput).toEqual({ decision: 'allow', reason: 'approved' });
});
});
describe('toOpenTelemetryAttributes with logPrompts=false', () => {
const config = createMockConfig(false);
it('should exclude PII-sensitive fields when logPrompts is disabled', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'/path/to/.gemini/hooks/check-secrets.sh --api-key=abc123',
{ tool_name: 'ReadFile', args: { file: 'secret.txt' } },
100,
true,
{ decision: 'allow' },
0,
'hook executed successfully',
'no errors',
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['event.name']).toBe(EVENT_HOOK_CALL);
expect(attributes['hook_event_name']).toBe('BeforeTool');
expect(attributes['hook_type']).toBe('command');
expect(attributes['duration_ms']).toBe(100);
expect(attributes['success']).toBe(true);
expect(attributes['exit_code']).toBe(0);
// PII-sensitive fields should NOT be included
expect(attributes['hook_input']).toBeUndefined();
expect(attributes['hook_output']).toBeUndefined();
expect(attributes['stdout']).toBeUndefined();
expect(attributes['stderr']).toBeUndefined();
});
it('should sanitize hook_name when logPrompts is disabled', () => {
const testCases = [
{
input: '/path/to/.gemini/hooks/check-secrets.sh --api-key=abc123',
expected: 'check-secrets.sh',
description: 'full path with arguments',
},
{
input: 'python /home/user/script.py --token=xyz',
expected: 'python',
description: 'command with script path and token',
},
{
input: 'node index.js',
expected: 'node',
description: 'simple command with file',
},
{
input: '/usr/bin/bash -c "echo $SECRET"',
expected: 'bash',
description: 'path with inline script',
},
{
input: 'C:\\Windows\\System32\\cmd.exe /c secret.bat',
expected: 'cmd.exe',
description: 'Windows path with arguments',
},
{
input: './hooks/local-hook.sh',
expected: 'local-hook.sh',
description: 'relative path',
},
{
input: 'simple-command',
expected: 'simple-command',
description: 'command without path or args',
},
{
input: '',
expected: 'unknown-command',
description: 'empty string',
},
{
input: ' ',
expected: 'unknown-command',
description: 'whitespace only',
},
];
for (const testCase of testCases) {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
testCase.input,
{ tool_name: 'ReadFile' },
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe(testCase.expected);
}
});
it('should still include error field even when logPrompts is disabled', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'test-hook',
{ tool_name: 'ReadFile' },
100,
false,
undefined,
undefined,
undefined,
undefined,
'Hook execution failed',
);
const attributes = event.toOpenTelemetryAttributes(config);
// Error should be included for debugging
expect(attributes['error']).toBe('Hook execution failed');
// But other PII fields should not
expect(attributes['hook_input']).toBeUndefined();
expect(attributes['stdout']).toBeUndefined();
});
});
describe('sanitizeHookName edge cases', () => {
const config = createMockConfig(false);
it('should handle commands with multiple spaces', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'python script.py --arg1 --arg2',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('python');
});
it('should handle mixed path separators', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'/path/to\\mixed\\separators.sh',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('separators.sh');
});
it('should handle trailing slashes', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'/path/to/directory/',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('unknown-command');
});
});
describe('toLogBody', () => {
it('should format success message correctly', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'test-hook',
{},
150,
true,
);
expect(event.toLogBody()).toBe(
'Hook call BeforeTool.test-hook succeeded in 150ms',
);
});
it('should format failure message correctly', () => {
const event = new HookCallEvent(
'AfterTool',
HookType.Command,
'validation-hook',
{},
75,
false,
);
expect(event.toLogBody()).toBe(
'Hook call AfterTool.validation-hook failed in 75ms',
);
});
});
describe('integration scenarios', () => {
it('should handle enterprise scenario with full logging', () => {
const enterpriseConfig = createMockConfig(true);
const event = new HookCallEvent(
'BeforeModel',
HookType.Command,
'$GEMINI_PROJECT_DIR/.gemini/hooks/add-context.sh',
{
llm_request: {
model: 'gemini-1.5-flash',
messages: [{ role: 'user', content: 'Hello' }],
},
},
250,
true,
{
hookSpecificOutput: {
llm_request: {
messages: [
{ role: 'user', content: 'Hello' },
{ role: 'system', content: 'Additional context...' },
],
},
},
},
0,
'Context added successfully',
);
const attributes = event.toOpenTelemetryAttributes(enterpriseConfig);
// In enterprise mode, everything is logged
expect(attributes['hook_name']).toBe(
'$GEMINI_PROJECT_DIR/.gemini/hooks/add-context.sh',
);
expect(attributes['hook_input']).toBeDefined();
expect(attributes['hook_output']).toBeDefined();
expect(attributes['stdout']).toBe('Context added successfully');
});
it('should handle public telemetry scenario with minimal logging', () => {
const publicConfig = createMockConfig(false);
const event = new HookCallEvent(
'BeforeModel',
HookType.Command,
'$GEMINI_PROJECT_DIR/.gemini/hooks/add-context.sh',
{
llm_request: {
model: 'gemini-1.5-flash',
messages: [{ role: 'user', content: 'Hello' }],
},
},
250,
true,
{
hookSpecificOutput: {
llm_request: {
messages: [
{ role: 'user', content: 'Hello' },
{ role: 'system', content: 'Additional context...' },
],
},
},
},
0,
'Context added successfully',
);
const attributes = event.toOpenTelemetryAttributes(publicConfig);
// In public mode, only metadata
expect(attributes['hook_name']).toBe('add-context.sh');
expect(attributes['hook_input']).toBeUndefined();
expect(attributes['hook_output']).toBeUndefined();
expect(attributes['stdout']).toBeUndefined();
// But metadata is still there
expect(attributes['hook_event_name']).toBe('BeforeModel');
expect(attributes['duration_ms']).toBe(250);
expect(attributes['success']).toBe(true);
});
});
describe('real-world sensitive command examples', () => {
const config = createMockConfig(false);
it('should sanitize commands with API keys', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'curl https://api.example.com -H "Authorization: Bearer sk-abc123xyz"',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('curl');
});
it('should sanitize commands with database credentials', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'psql postgresql://user:password@localhost/db',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('psql');
});
it('should sanitize commands with environment variables containing secrets', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'AWS_SECRET_KEY=abc123 aws s3 ls',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('AWS_SECRET_KEY=abc123');
});
it('should sanitize Python scripts with file paths', () => {
const event = new HookCallEvent(
'BeforeTool',
HookType.Command,
'python /home/john.doe/projects/secret-scanner/scan.py --config=/etc/secrets.yml',
{},
100,
true,
);
const attributes = event.toOpenTelemetryAttributes(config);
expect(attributes['hook_name']).toBe('python');
});
});
});
});