forked from ChatGPTBox-dev/chatGPTBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact.mjs
More file actions
74 lines (69 loc) · 2.07 KB
/
redact.mjs
File metadata and controls
74 lines (69 loc) · 2.07 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
const SENSITIVE_KEYWORDS = [
'apikey', // Covers apiKey, customApiKey, claudeApiKey, etc.
'token', // Covers accessToken, refreshToken, etc.
'secret',
'password',
'kimimoonshotrefreshtoken',
'credential',
'jwt',
'session',
]
export function isPromptOrSelectionLikeKey(lowerKey) {
lowerKey = lowerKey.toLowerCase()
const normalizedKey = lowerKey.replace(/[^a-z0-9]/g, '')
return (
normalizedKey.endsWith('question') ||
normalizedKey.endsWith('prompt') ||
normalizedKey.endsWith('query') ||
normalizedKey === 'selection' ||
normalizedKey === 'selectiontext'
)
}
export function redactSensitiveFields(obj, recursionDepth = 0, maxDepth = 5, seen = new WeakSet()) {
if (recursionDepth > maxDepth) {
// Prevent infinite recursion on circular objects or excessively deep structures
return 'REDACTED_TOO_DEEP'
}
if (obj === null || typeof obj !== 'object') {
return obj
}
if (seen.has(obj)) {
return 'REDACTED_CIRCULAR_REFERENCE'
}
seen.add(obj)
if (Array.isArray(obj)) {
const redactedArray = []
for (let i = 0; i < obj.length; i++) {
const item = obj[i]
if (item !== null && typeof item === 'object') {
redactedArray[i] = redactSensitiveFields(item, recursionDepth + 1, maxDepth, seen)
} else {
redactedArray[i] = item
}
}
return redactedArray
}
const redactedObj = {}
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const lowerKey = key.toLowerCase()
let isKeySensitive = isPromptOrSelectionLikeKey(lowerKey)
if (!isKeySensitive) {
for (const keyword of SENSITIVE_KEYWORDS) {
if (lowerKey.includes(keyword)) {
isKeySensitive = true
break
}
}
}
if (isKeySensitive) {
redactedObj[key] = 'REDACTED'
} else if (obj[key] !== null && typeof obj[key] === 'object') {
redactedObj[key] = redactSensitiveFields(obj[key], recursionDepth + 1, maxDepth, seen)
} else {
redactedObj[key] = obj[key]
}
}
}
return redactedObj
}