burnoff 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @file src/guards/credential-scrubber.mjs
3
+ * Pure zero-I/O credential scrubber and indirect prompt injection (IPI) sanitizer.
4
+ * Uses functional replacers so `$` sequences in dynamic strings are never interpreted.
5
+ */
6
+
7
+ export const LIVE_SECRET_DEFINITIONS = Object.freeze([
8
+ {
9
+ id: 'pem_private_key',
10
+ label: 'PEM Private Key',
11
+ re: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY-----[\s\S]*?(?:-----END (?:RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY-----|$)/g,
12
+ redaction: '[REDACTED:PEM_PRIVATE_KEY]'
13
+ },
14
+ {
15
+ id: 'anthropic_api_key',
16
+ label: 'Anthropic API Key (sk-ant-api03-)',
17
+ re: /\bsk-ant-(?:api03-)?[A-Za-z0-9_-]{16,}\b/g,
18
+ redaction: '[REDACTED:ANTHROPIC_KEY]'
19
+ },
20
+ {
21
+ id: 'google_api_key',
22
+ label: 'Google API Key (AIzaSy)',
23
+ re: /\bAIzaSy[0-9A-Za-z_-]{25,}\b/g,
24
+ redaction: '[REDACTED:GOOGLE_API_KEY]'
25
+ },
26
+ {
27
+ id: 'github_classic_pat',
28
+ label: 'GitHub PAT (ghp_)',
29
+ re: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/g,
30
+ redaction: '[REDACTED:GITHUB_PAT]'
31
+ },
32
+ {
33
+ id: 'github_fine_pat',
34
+ label: 'GitHub Fine-Grained PAT (github_pat_)',
35
+ re: /\bgithub_pat_[A-Za-z0-9_]{20,}\b/g,
36
+ redaction: '[REDACTED:GITHUB_PAT]'
37
+ },
38
+ {
39
+ id: 'aws_access_key',
40
+ label: 'AWS Access Key ID (AKIA)',
41
+ re: /\bAKIA[0-9A-Z]{16}\b/g,
42
+ redaction: '[REDACTED:AWS_ACCESS_KEY]'
43
+ },
44
+ {
45
+ id: 'slack_bot_token',
46
+ label: 'Slack Token (xoxb-)',
47
+ re: /\bxox[baprs]-[0-9]{8,}-[0-9A-Za-z-]{16,}\b/g,
48
+ redaction: '[REDACTED:SLACK_TOKEN]'
49
+ },
50
+ {
51
+ id: 'openai_api_key',
52
+ label: 'OpenAI API Key (sk-proj-/sk-live-)',
53
+ re: /\bsk-(?:proj-|live-)[A-Za-z0-9_-]{16,}\b/g,
54
+ redaction: '[REDACTED:OPENAI_KEY]'
55
+ },
56
+ {
57
+ id: 'bearer_token',
58
+ label: 'Bearer Authorization Token',
59
+ re: /\bBearer\s+[A-Za-z0-9._~+/-]{20,}\b/gi,
60
+ redaction: 'Bearer [REDACTED:TOKEN]'
61
+ }
62
+ ]);
63
+
64
+ const PLACEHOLDER_VALUE_RE =
65
+ /^(?:your[_-]?|my[_-]?|insert[_-]?|replace[_-]?|enter[_-]?|example|changeme|placeholder|dummy|redacted|todo|none|null|undefined|test[_-]?key|sample|<[^>]+>|\$\{[^}]+\}|x{4,}|\*{4,}|0{6,})/i;
66
+
67
+ /**
68
+ * Returns true when a matched value looks like documentation/example placeholder text.
69
+ */
70
+ export function isPlaceholderSecretValue(rawVal = '') {
71
+ const cleaned = String(rawVal || '').trim().replace(/^['"]|['"]$/g, () => '');
72
+ if (!cleaned || cleaned.length < 8) return true;
73
+ if (PLACEHOLDER_VALUE_RE.test(cleaned)) return true;
74
+ if (/YOUR_API_KEY|EXAMPLE|CHANGEME|PLACEHOLDER|DUMMY|REDACTED|FAKE_KEY|SAMPLE_KEY/i.test(cleaned)) {
75
+ return true;
76
+ }
77
+ return false;
78
+ }
79
+
80
+ /**
81
+ * Computes Shannon entropy (bits/char) of a candidate secret string.
82
+ */
83
+ export function computeShannonEntropy(str = '') {
84
+ const s = String(str || '');
85
+ if (!s.length) return 0;
86
+ const counts = new Map();
87
+ for (let i = 0; i < s.length; i += 1) {
88
+ const ch = s[i];
89
+ counts.set(ch, (counts.get(ch) || 0) + 1);
90
+ }
91
+ let entropy = 0;
92
+ for (const count of counts.values()) {
93
+ const p = count / s.length;
94
+ entropy -= p * Math.log2(p);
95
+ }
96
+ return entropy;
97
+ }
98
+
99
+ /**
100
+ * Redacts live credentials and sensitive `.env`-style assignments without mangling placeholders.
101
+ */
102
+ export function redactSecretsPure(rawText = '') {
103
+ let out = String(rawText ?? '');
104
+ let redactedCount = 0;
105
+
106
+ for (const def of LIVE_SECRET_DEFINITIONS) {
107
+ const re = new RegExp(def.re.source, def.re.flags);
108
+ out = out.replace(re, (match) => {
109
+ const candidate = def.id === 'bearer_token' ? match.replace(/^Bearer\s+/i, () => '') : match;
110
+ if (def.id !== 'pem_private_key' && isPlaceholderSecretValue(candidate)) {
111
+ return match;
112
+ }
113
+ redactedCount += 1;
114
+ return def.redaction;
115
+ });
116
+ }
117
+
118
+ const envAssignRe =
119
+ /\b([A-Z][A-Z0-9_]*(?:SECRET|TOKEN|PASSWORD|PASSWD|API_KEY|PRIVATE_KEY|ACCESS_KEY|AUTH_KEY|CLIENT_SECRET)[A-Z0-9_]*)\s*=\s*(['"]?)([^\s'"\r\n#]+)\2/g;
120
+ out = out.replace(envAssignRe, (full, keyName, _quote, val) => {
121
+ if (isPlaceholderSecretValue(val)) return full;
122
+ redactedCount += 1;
123
+ return `${keyName}=[REDACTED:ENV_SECRET]`;
124
+ });
125
+
126
+ return {
127
+ sanitized: out,
128
+ redactedCount
129
+ };
130
+ }
131
+
132
+ const IPI_PATTERNS = Object.freeze([
133
+ {
134
+ id: 'ignore_previous_instructions',
135
+ re: /\b(?:ignore|disregard|forget)\s+(?:all\s+)?(?:previous|prior|above|system)\s+(?:instructions|rules|prompts|directives)\b/gi,
136
+ replacement: '[IPI_FILTERED:INSTRUCTION_OVERRIDE]'
137
+ },
138
+ {
139
+ id: 'fake_system_tag',
140
+ re: /<\s*\/?\s*(?:system|system_prompt|developer_instructions|user_rules|antml:thinking)\s*>/gi,
141
+ replacement: '[IPI_FILTERED:TAG]'
142
+ },
143
+ {
144
+ id: 'unicode_tag_stego',
145
+ re: /[\u{E0000}-\u{E007F}]/gu,
146
+ replacement: ''
147
+ },
148
+ {
149
+ id: 'zero_width_stego',
150
+ re: /[\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF]/gu,
151
+ replacement: ''
152
+ }
153
+ ]);
154
+
155
+ /**
156
+ * Strips indirect prompt injection markers and redacts live credentials.
157
+ */
158
+ export function sanitizeIpiPure(rawText = '') {
159
+ let out = String(rawText ?? '');
160
+ const detections = [];
161
+
162
+ for (const pat of IPI_PATTERNS) {
163
+ const re = new RegExp(pat.re.source, pat.re.flags);
164
+ out = out.replace(re, () => {
165
+ detections.push(pat.id);
166
+ return pat.replacement;
167
+ });
168
+ }
169
+
170
+ const secretRes = redactSecretsPure(out);
171
+ return {
172
+ sanitized: secretRes.sanitized,
173
+ ipiDetected: detections.length > 0,
174
+ detections,
175
+ redactedSecretCount: secretRes.redactedCount
176
+ };
177
+ }
178
+
179
+ export default {
180
+ LIVE_SECRET_DEFINITIONS,
181
+ isPlaceholderSecretValue,
182
+ computeShannonEntropy,
183
+ redactSecretsPure,
184
+ sanitizeIpiPure
185
+ };