forked from ChatGPTBox-dev/chatGPTBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact.test.mjs
More file actions
110 lines (100 loc) · 3.75 KB
/
redact.test.mjs
File metadata and controls
110 lines (100 loc) · 3.75 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
import assert from 'node:assert/strict'
import { describe, test } from 'node:test'
import {
redactSensitiveFields,
isPromptOrSelectionLikeKey,
} from '../../../src/background/redact.mjs'
describe('redactSensitiveFields', () => {
test('redacts keys containing sensitive keywords', () => {
const input = {
apiKey: 'sk-1234',
accessToken: 'tok-abc',
secret: 'shh',
password: 'hunter2',
credential: 'cred-value',
jwt: 'eyJ...',
session: 'sess-xyz',
kimimoonshotrefreshtoken: 'refresh-val',
}
const result = redactSensitiveFields(input)
for (const key of Object.keys(input)) {
assert.equal(result[key], 'REDACTED', `expected ${key} to be redacted`)
}
})
test('preserves non-sensitive keys', () => {
const input = { name: 'Alice', age: 30, enabled: true }
const result = redactSensitiveFields(input)
assert.deepEqual(result, { name: 'Alice', age: 30, enabled: true })
})
test('handles nested objects', () => {
const input = {
user: { name: 'Bob', apiKey: 'sk-nested' },
count: 5,
}
const result = redactSensitiveFields(input)
assert.equal(result.user.name, 'Bob')
assert.equal(result.user.apiKey, 'REDACTED')
assert.equal(result.count, 5)
})
test('handles arrays with mixed objects', () => {
const input = [{ name: 'a', token: 'tok1' }, 'plain-string', 42, { password: 'pw', safe: true }]
const result = redactSensitiveFields(input)
assert.ok(Array.isArray(result))
assert.equal(result[0].name, 'a')
assert.equal(result[0].token, 'REDACTED')
assert.equal(result[1], 'plain-string')
assert.equal(result[2], 42)
assert.equal(result[3].password, 'REDACTED')
assert.equal(result[3].safe, true)
})
test('respects maxDepth', () => {
const deep = { a: { b: { c: { d: 'value' } } } }
const result = redactSensitiveFields(deep, 0, 2)
assert.equal(result.a.b.c, 'REDACTED_TOO_DEEP')
})
test('handles null and primitive inputs', () => {
assert.equal(redactSensitiveFields(null), null)
assert.equal(redactSensitiveFields(42), 42)
assert.equal(redactSensitiveFields('hello'), 'hello')
assert.equal(redactSensitiveFields(undefined), undefined)
})
test('handles circular references', () => {
const obj = { name: 'root' }
obj.self = obj
const result = redactSensitiveFields(obj)
assert.equal(result.name, 'root')
assert.equal(result.self, 'REDACTED_CIRCULAR_REFERENCE')
})
test('redacts prompt/selection-like keys via isPromptOrSelectionLikeKey integration', () => {
const input = {
prompt: 'secret input',
userQuestion: 'what is X?',
selection: 'highlighted text',
name: 'safe',
}
const result = redactSensitiveFields(input)
assert.equal(result.prompt, 'REDACTED')
assert.equal(result.userQuestion, 'REDACTED')
assert.equal(result.selection, 'REDACTED')
assert.equal(result.name, 'safe')
})
})
describe('isPromptOrSelectionLikeKey', () => {
test('matches prompt/selection-related keys', () => {
assert.ok(isPromptOrSelectionLikeKey('question'))
assert.ok(isPromptOrSelectionLikeKey('prompt'))
assert.ok(isPromptOrSelectionLikeKey('query'))
assert.ok(isPromptOrSelectionLikeKey('selection'))
assert.ok(isPromptOrSelectionLikeKey('selectiontext'))
assert.ok(isPromptOrSelectionLikeKey('systemprompt'))
assert.ok(isPromptOrSelectionLikeKey('user_question'))
assert.ok(isPromptOrSelectionLikeKey('searchquery'))
})
test('rejects unrelated keys', () => {
assert.ok(!isPromptOrSelectionLikeKey('name'))
assert.ok(!isPromptOrSelectionLikeKey('apikey'))
assert.ok(!isPromptOrSelectionLikeKey('enabled'))
assert.ok(!isPromptOrSelectionLikeKey('count'))
assert.ok(!isPromptOrSelectionLikeKey('model'))
})
})