|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import { describe, test } from 'node:test' |
| 3 | +import { |
| 4 | + redactSensitiveFields, |
| 5 | + isPromptOrSelectionLikeKey, |
| 6 | +} from '../../../src/background/redact.mjs' |
| 7 | + |
| 8 | +describe('redactSensitiveFields', () => { |
| 9 | + test('redacts keys containing sensitive keywords', () => { |
| 10 | + const input = { |
| 11 | + apiKey: 'sk-1234', |
| 12 | + accessToken: 'tok-abc', |
| 13 | + secret: 'shh', |
| 14 | + password: 'hunter2', |
| 15 | + credential: 'cred-value', |
| 16 | + jwt: 'eyJ...', |
| 17 | + session: 'sess-xyz', |
| 18 | + kimimoonshotrefreshtoken: 'refresh-val', |
| 19 | + } |
| 20 | + const result = redactSensitiveFields(input) |
| 21 | + for (const key of Object.keys(input)) { |
| 22 | + assert.equal(result[key], 'REDACTED', `expected ${key} to be redacted`) |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + test('preserves non-sensitive keys', () => { |
| 27 | + const input = { name: 'Alice', age: 30, enabled: true } |
| 28 | + const result = redactSensitiveFields(input) |
| 29 | + assert.deepEqual(result, { name: 'Alice', age: 30, enabled: true }) |
| 30 | + }) |
| 31 | + |
| 32 | + test('handles nested objects', () => { |
| 33 | + const input = { |
| 34 | + user: { name: 'Bob', apiKey: 'sk-nested' }, |
| 35 | + count: 5, |
| 36 | + } |
| 37 | + const result = redactSensitiveFields(input) |
| 38 | + assert.equal(result.user.name, 'Bob') |
| 39 | + assert.equal(result.user.apiKey, 'REDACTED') |
| 40 | + assert.equal(result.count, 5) |
| 41 | + }) |
| 42 | + |
| 43 | + test('handles arrays with mixed objects', () => { |
| 44 | + const input = [{ name: 'a', token: 'tok1' }, 'plain-string', 42, { password: 'pw', safe: true }] |
| 45 | + const result = redactSensitiveFields(input) |
| 46 | + assert.ok(Array.isArray(result)) |
| 47 | + assert.equal(result[0].name, 'a') |
| 48 | + assert.equal(result[0].token, 'REDACTED') |
| 49 | + assert.equal(result[1], 'plain-string') |
| 50 | + assert.equal(result[2], 42) |
| 51 | + assert.equal(result[3].password, 'REDACTED') |
| 52 | + assert.equal(result[3].safe, true) |
| 53 | + }) |
| 54 | + |
| 55 | + test('respects maxDepth', () => { |
| 56 | + const deep = { a: { b: { c: { d: 'value' } } } } |
| 57 | + const result = redactSensitiveFields(deep, 0, 2) |
| 58 | + assert.equal(result.a.b.c, 'REDACTED_TOO_DEEP') |
| 59 | + }) |
| 60 | + |
| 61 | + test('handles null and primitive inputs', () => { |
| 62 | + assert.equal(redactSensitiveFields(null), null) |
| 63 | + assert.equal(redactSensitiveFields(42), 42) |
| 64 | + assert.equal(redactSensitiveFields('hello'), 'hello') |
| 65 | + assert.equal(redactSensitiveFields(undefined), undefined) |
| 66 | + }) |
| 67 | + |
| 68 | + test('handles circular references', () => { |
| 69 | + const obj = { name: 'root' } |
| 70 | + obj.self = obj |
| 71 | + const result = redactSensitiveFields(obj) |
| 72 | + assert.equal(result.name, 'root') |
| 73 | + assert.equal(result.self, 'REDACTED_CIRCULAR_REFERENCE') |
| 74 | + }) |
| 75 | + |
| 76 | + test('redacts prompt/selection-like keys via isPromptOrSelectionLikeKey integration', () => { |
| 77 | + const input = { |
| 78 | + prompt: 'secret input', |
| 79 | + userQuestion: 'what is X?', |
| 80 | + selection: 'highlighted text', |
| 81 | + name: 'safe', |
| 82 | + } |
| 83 | + const result = redactSensitiveFields(input) |
| 84 | + assert.equal(result.prompt, 'REDACTED') |
| 85 | + assert.equal(result.userQuestion, 'REDACTED') |
| 86 | + assert.equal(result.selection, 'REDACTED') |
| 87 | + assert.equal(result.name, 'safe') |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +describe('isPromptOrSelectionLikeKey', () => { |
| 92 | + test('matches prompt/selection-related keys', () => { |
| 93 | + assert.ok(isPromptOrSelectionLikeKey('question')) |
| 94 | + assert.ok(isPromptOrSelectionLikeKey('prompt')) |
| 95 | + assert.ok(isPromptOrSelectionLikeKey('query')) |
| 96 | + assert.ok(isPromptOrSelectionLikeKey('selection')) |
| 97 | + assert.ok(isPromptOrSelectionLikeKey('selectiontext')) |
| 98 | + assert.ok(isPromptOrSelectionLikeKey('systemprompt')) |
| 99 | + assert.ok(isPromptOrSelectionLikeKey('user_question')) |
| 100 | + assert.ok(isPromptOrSelectionLikeKey('searchquery')) |
| 101 | + }) |
| 102 | + |
| 103 | + test('rejects unrelated keys', () => { |
| 104 | + assert.ok(!isPromptOrSelectionLikeKey('name')) |
| 105 | + assert.ok(!isPromptOrSelectionLikeKey('apikey')) |
| 106 | + assert.ok(!isPromptOrSelectionLikeKey('enabled')) |
| 107 | + assert.ok(!isPromptOrSelectionLikeKey('count')) |
| 108 | + assert.ok(!isPromptOrSelectionLikeKey('model')) |
| 109 | + }) |
| 110 | +}) |
0 commit comments