File size: 3,599 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect } from 'vitest';
import {
  sanitizeErrorMessage,
  sanitizeToolArgs,
  sanitizeThoughtContent,
} from './agent-sanitization-utils.js';

describe('agent-sanitization-utils', () => {
  describe('sanitizeErrorMessage', () => {
    it('should redact standard inline PEM content', () => {
      const input =
        'Here is my key: -----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA12345\n-----END RSA PRIVATE KEY----- do not share.';
      const expected = 'Here is my key: [REDACTED_PEM] do not share.';
      expect(sanitizeErrorMessage(input)).toBe(expected);
    });

    it('should redact non-standard inline PEM content (with punctuation)', () => {
      const input =
        '-----BEGIN X.509 CERTIFICATE-----\nMIIEowIBAAKCAQEA12345\n-----END X.509 CERTIFICATE-----';
      const expected = '[REDACTED_PEM]';
      expect(sanitizeErrorMessage(input)).toBe(expected);
    });

    it('should not hang on ReDoS attack string for PEM redaction', () => {
      const start = Date.now();
      // A string that starts with -----BEGIN but has no ending, with many spaces
      // In the vulnerable regex, this would cause catastrophic backtracking.
      const maliciousInput = '-----BEGIN ' + ' '.repeat(50000) + 'A';
      const result = sanitizeErrorMessage(maliciousInput);
      const duration = Date.now() - start;

      // Should process very quickly (e.g. < 50ms)
      expect(duration).toBeLessThan(50);

      // Since it doesn't match the full PEM block pattern, it should return the input unaltered
      expect(result).toBe(maliciousInput);
    });

    it('should redact key-value pairs with sensitive keys', () => {
      const input = 'Error: connection failed. --api-key="secret123"';
      const result = sanitizeErrorMessage(input);
      expect(result).toContain('[REDACTED]');
      expect(result).not.toContain('secret123');
    });

    it('should redact space-separated sensitive keywords', () => {
      // The keyword regex requires tokens to be 8+ chars
      const input = 'Using password mySuperSecretPassword123';
      const result = sanitizeErrorMessage(input);
      expect(result).toContain('[REDACTED]');
      expect(result).not.toContain('mySuperSecretPassword123');
    });
  });

  describe('sanitizeToolArgs', () => {
    it('should redact sensitive fields in an object', () => {
      const input = {
        username: 'admin',
        password: 'superSecretPassword',
        nested: {
          api_key: 'abc123xyz',
          normal_field: 'hello',
        },
      };

      const result = sanitizeToolArgs(input);

      expect(result).toEqual({
        username: 'admin',
        password: '[REDACTED]',
        nested: {
          api_key: '[REDACTED]',
          normal_field: 'hello',
        },
      });
    });

    it('should handle arrays and strings correctly', () => {
      const input = ['normal string', '--api-key="secret123"'];
      const result = sanitizeToolArgs(input) as string[];

      expect(result[0]).toBe('normal string');
      expect(result[1]).toContain('[REDACTED]');
      expect(result[1]).not.toContain('secret123');
    });
  });

  describe('sanitizeThoughtContent', () => {
    it('should redact sensitive patterns from thought content', () => {
      const input = 'I will now authenticate using token 1234567890abcdef.';
      const result = sanitizeThoughtContent(input);

      expect(result).toContain('[REDACTED]');
      expect(result).not.toContain('1234567890abcdef');
    });
  });
});