@zapier/secret-scrubber 1.2.0 → 1.3.0

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.
package/README.md CHANGED
@@ -85,7 +85,7 @@ scanText('request to https://site.com?api_key=abc123def456 failed')
85
85
  // 'request to https://site.com?api_key=:censored:12:ce9c209b14: failed'
86
86
  ```
87
87
 
88
- It matches known vendor key formats, JWTs, private keys, `Bearer` and `Basic` credentials, credentials in a URL, and the values of secret-looking `key=value`, `"key": "value"` and `access token: value` pairs. Most vendor formats are generated from [gitleaks](https://github.com/gitleaks/gitleaks)' rules, in `src/secret-formats.ts`.
88
+ It matches known vendor key formats, JWTs, private keys (PEM and PGP), password hashes, Slack webhook URLs, `Bearer`, `Basic` and `Token` credentials (also written `Bearer+` or `Bearer%20` in a query string), credentials in a URL, the secret in an MCP server URL path, and the values of secret-looking `key=value` (also `key%3Dvalue` inside an encoded URL), `key = "value"`, `"key": "value"` and `access token: value` or `authorization: value` pairs. Session cookies (`sessionid`, `zapsession`, `wssession`) and `consumer_key`, `hmac_key`, `signing_key` and `checksum_key` count as secret-looking, as does a `{"name": "<secret-looking name>", "value": "..."}` pair. `Authorization:` takes any scheme word before its value. `code=` counts when its value has sixteen or more characters and a digit, which keeps `code=access_denied`. Text with percent-escapes gets a second look: it is decoded up to 3 times and scanned again, and when that censors something the result is the decoded, scanned text. Most vendor formats are generated from [gitleaks](https://github.com/gitleaks/gitleaks)' rules, in `src/secret-formats.ts`.
89
89
 
90
90
  A bare `key` is often not a credential, so its value stays readable when it looks like a Jira issue key (`ABC-123`), a word-based label (`last_run_id`, `lastSyncTimestamp`) or anything under 6 characters. In quoted pairs, a bare `"key"` or `"sig"` is never censored, because JSON error bodies use them as field names, as in `{"key": "email"}`. Other names, such as `api_key`, are always censored.
91
91
 
package/lib/scan-text.js CHANGED
@@ -19,18 +19,55 @@ const SECRET_VALUE_PATTERNS = [
19
19
  /\bya29\.[A-Za-z0-9_-]{20,}/g,
20
20
  /\b1\/\/0[A-Za-z0-9_-]{20,}/g,
21
21
  /\bzap_[a-zA-Z0-9]{24,}/g,
22
- /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----[A-Za-z0-9+/=\\ \t\n\r\f\v]*(?:-----END (?:[A-Z0-9]+ )*PRIVATE KEY-----)?/g,
22
+ /\bdapi[0-9a-f]{32}(?:-[0-9]+)?/g,
23
+ /\bCFPAT-[A-Za-z0-9_-]{43}/g,
24
+ /https:\/\/hooks\.slack\.com\/(?:services|workflows|triggers)\/[A-Za-z0-9+/]{30,}/g,
25
+ // Password hashes: Django pbkdf2, bcrypt, argon2. A leaked hash can be cracked offline.
26
+ /\bpbkdf2_sha(?:1|256)\$[0-9]+\$[A-Za-z0-9./+]+\$[A-Za-z0-9+/]{20,}=*/g,
27
+ /\$2[aby]\$[0-9]{2}\$[./A-Za-z0-9]{53}/g,
28
+ /\$argon2(?:id|i|d)\$v=[0-9]+\$m=[0-9]+,t=[0-9]+,p=[0-9]+\$[A-Za-z0-9+/]+\$[A-Za-z0-9+/]+/g,
29
+ // A Zapier hook URL is itself the credential: anyone holding it can trigger the Zap.
30
+ /\bhooks\.zapier\.com\/hooks\/(?:catch|standard)\/[0-9]+\/[A-Za-z0-9]+/g,
31
+ // Meta Graph user and page tokens. gitleaks knows only the EAAM and EAAC app prefixes.
32
+ /\bEAA[A-Za-z0-9]{100,}/g,
33
+ // WooCommerce REST consumer key and secret.
34
+ /\bc[ks]_[a-f0-9]{40}\b/g,
35
+ /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----(?:(?:\r?\n|(?:\\r)?\\n)[A-Za-z][A-Za-z-]*: [^\r\n\\]*)*[A-Za-z0-9+/=\\ \t\n\r\f\v]*(?:-----END (?:[A-Z0-9]+ )*PRIVATE KEY(?: BLOCK)?-----)?/g,
23
36
  ...secret_formats_1.SECRET_FORMATS.map((pattern) => new RegExp(pattern, 'g')),
24
37
  ];
25
38
  const URL_USERINFO_PATTERN = /(\b[A-Za-z][A-Za-z0-9+.-]{0,31}:\/\/)([^/ \t\n\r\f\v:@"'<>]+:[^/ \t\n\r\f\v@"'<>]+)@/g;
26
39
  // Matches prose like "Invalid access token: abc123"; requiring a digit keeps "token: expired".
27
40
  // URLs are skipped because providers follow "access token:" with docs links. The
28
41
  // bounded lookaheads keep this linear on repeated labels like "key:key:key:".
29
- const LABELLED_VALUE_PATTERN = /\b((?:[a-z]+[ _-])?(?:token|api[ _-]?key|key|secret|password|passwd))([ \t\n\r\f\v]*:[ \t\n\r\f\v]*)((?![a-z][a-z0-9+.-]{0,31}:\/\/)(?=[^ \t\n\r\f\v"'<>,;\]]{0,255}\d)[^ \t\n\r\f\v"'<>,;\]]{8,})/gi;
30
- // Requiring a digit keeps prose like "Bearer authentication required" intact.
31
- const AUTH_SCHEME_PATTERN = /\b(bearer|basic)([ \t\n\r\f\v]+)((?=[A-Za-z0-9\-._~+/]*\d)[A-Za-z0-9\-._~+/]{16,}=*)/gi;
32
- // The lookbehind and length cap keep this linear on long unbroken runs like base64.
33
- const QUERY_PARAM_PATTERN = /(?<![A-Za-z0-9_\-.])([A-Za-z0-9_\-.]{1,64})(=)([^ \t\n\r\f\v&"'<>,;)\]}]+)/g;
42
+ const LABELLED_VALUE_PATTERN = /\b((?:[a-z]+[ _-])?(?:token|api[ _-]?key|key|secret|password|passwd|authorization))([ \t\n\r\f\v]*:[ \t\n\r\f\v]*)((?![a-z][a-z0-9+.-]{0,31}:\/\/)(?=[^ \t\n\r\f\v"'<>,;\]]{0,255}\d)[^ \t\n\r\f\v"'<>,;\]]{8,})/gi;
43
+ // In a query string the space after the scheme is written as "+" or "%20". The digit
44
+ // check lives in the replacement: as a lookahead it rescanned each "+"-joined run
45
+ // once per scheme word in it, which made long form-encoded bodies quadratic.
46
+ // The scheme may follow an escape, as in Authorization%3A%20Bearer%20<t>.
47
+ const AUTH_SCHEME_PATTERN = /(?:\b|(?<=%[0-9A-Fa-f]{2}))(bearer|basic|token)([ \t\n\r\f\v]+|\+|%20)([A-Za-z0-9\-._~+/]{16,}=*)/gi;
48
+ // Authorization: <scheme> <credentials>, for a scheme other than Bearer, Basic and Token,
49
+ // such as Klaviyo-API-Key pk_... Any word can be the scheme there, so the value must have a
50
+ // digit, which keeps prose like "Authorization: required for access".
51
+ const AUTH_HEADER_ANY_SCHEME_PATTERN = /\b(authorization)([ \t]*:[ \t]*)([a-z][a-z0-9_\-.]{0,40}[ \t]+)([^ \t\n\r\f\v"'<>,;\]\\]{8,})/gi;
52
+ // An MCP server URL carries its credential in the path, also inside an encoded URL.
53
+ const MCP_PATH_PATTERN = /((?:\/|%2F)mcp(?:\/|%2F)s(?:\/|%2F))([A-Za-z0-9_-]{20,})/gi;
54
+ // The lookbehind and length cap keep this linear on long unbroken runs like base64. A value
55
+ // stops at "?", so a link in another parameter (next=https://a/cb?token=...) keeps its own
56
+ // pairs for the next match.
57
+ const QUERY_PARAM_PATTERN = /(?<![A-Za-z0-9_\-.])([A-Za-z0-9_\-.]{1,64})(=)([^ \t\n\r\f\v&?"'<>,;)\]}]+)/g;
58
+ // A pair whose name says it holds a secret, inside a percent-encoded URL. Passwords hold
59
+ // characters that encode to other escapes (%40 for @, %21 for !), so the value runs
60
+ // through every escape except the encoded separators: space " # & ' , and ;.
61
+ const ENCODED_SECRET_PARAM_PATTERN = /(?<![A-Za-z0-9_\-.])([A-Za-z0-9_\-.]{1,64})(%3[Dd])((?:[^ \t\n\r\f\v&%"'<>,;)\]}]|%(?:[014-9A-Fa-f][0-9A-Fa-f]|2[14589ABDEFabdef]|3[0-9ACDEFacdef]))+)/g;
62
+ // The same pair inside a percent-encoded URL: cb%3Ftoken%3D<value>%26page%3D2. A value
63
+ // runs through the encoded characters tokens contain (+ / = - . _ ~) and stops at any
64
+ // other escape, such as %26 for the next pair.
65
+ const ENCODED_QUERY_PARAM_PATTERN = /(?<![A-Za-z0-9_\-.])([A-Za-z0-9_\-.]{1,64})(%3[Dd])((?:[^ \t\n\r\f\v&%"'<>,;)\]}]|%(?:2[BbDdEeFf]|3[Dd]|5[Ff]|7[Ee]))+)/g;
66
+ // Code and config: secret_id = "<value>".
67
+ const ASSIGNED_PAIR_PATTERN = /(?<![A-Za-z0-9_\-.])([A-Za-z0-9_\-.]{1,64})([ \t]*=[ \t]*)("[^"\\]+"|'[^'\\]+')/g;
68
+ // A header or variable written as a pair, as in a Kubernetes pod spec:
69
+ // {"name": "GITLAB_ACCESS_TOKEN", "value": "..."}. The name says what the value is.
70
+ const NAME_VALUE_PAIR_PATTERN = /((\\?")(?:name|key|header|field)\2[ \t]*:[ \t]*\2([A-Za-z0-9_\-.]{1,64})\2[ \t]*,[ \t]*\2value\2)([ \t]*:[ \t]*)(\2[^"\\]+\2)/g;
34
71
  const QUOTED_PAIR_PATTERN = /(\\?["'][A-Za-z0-9_\-.]+\\?["'])([ \t\n\r\f\v]*:[ \t\n\r\f\v]*)(\\?["'][^"'\\]+\\?["'])/g;
35
72
  // A bare "key" is often not a credential: a Jira issue key, or a Storage by Zapier label.
36
73
  const JIRA_ISSUE_KEY = /^[A-Z][A-Z0-9_]+-[0-9]+$/;
@@ -56,8 +93,23 @@ const SECRET_KEY_SUBSTRINGS = [
56
93
  'access_key',
57
94
  'accesskey',
58
95
  'private_key',
96
+ 'code_verifier',
97
+ 'sessionid',
98
+ 'zapsession',
99
+ 'wssession',
100
+ 'consumer_key',
101
+ 'consumerkey',
102
+ 'hmac_key',
103
+ 'hmackey',
104
+ 'signing_key',
105
+ 'signingkey',
106
+ 'checksum_key',
107
+ 'checksumkey',
59
108
  ];
60
- const EXACT_SECRET_KEYS = ['key', 'sig'];
109
+ const EXACT_SECRET_KEYS = ['key', 'sig', 'code'];
110
+ // An OAuth authorization code. "code" is also an error field, so only a long value with a
111
+ // digit is censored.
112
+ const OAUTH_CODE = /^(?=.*\d)[A-Za-z0-9\-._~+/%]{16,}$/;
61
113
  const isSecretParam = (key, matchExactKeys) => {
62
114
  const normalized = key.replace(/^[\\"']+|[\\"']+$/g, '').toLowerCase();
63
115
  return ((matchExactKeys && EXACT_SECRET_KEYS.includes(normalized)) ||
@@ -81,6 +133,10 @@ const censorPair = (matchExactKeys) => (match, key, sep, value) => {
81
133
  if (bareKey && isReadableIdentifier(inner)) {
82
134
  return match;
83
135
  }
136
+ const bareCode = key.replace(/^[\\"']+|[\\"']+$/g, '').toLowerCase() === 'code';
137
+ if (bareCode && !OAUTH_CODE.test(inner)) {
138
+ return match;
139
+ }
84
140
  return `${key}${sep}${quoteOpen}${(0, utils_1.censor)(inner)}${quoteClose}`;
85
141
  };
86
142
  const scanString = (text) => {
@@ -88,9 +144,24 @@ const scanString = (text) => {
88
144
  SECRET_VALUE_PATTERNS.forEach((pattern) => {
89
145
  result = result.replace(pattern, (match) => (0, utils_1.censor)(match));
90
146
  });
147
+ result = result.replace(MCP_PATH_PATTERN, (_match, prefix, value) => `${prefix}${(0, utils_1.censor)(value)}`);
91
148
  result = result.replace(URL_USERINFO_PATTERN, (_match, scheme, userinfo) => `${scheme}${(0, utils_1.censor)(userinfo)}@`);
92
- result = result.replace(AUTH_SCHEME_PATTERN, (_match, scheme, sep, value) => `${scheme}${sep}${(0, utils_1.censor)(value)}`);
149
+ result = result.replace(AUTH_SCHEME_PATTERN,
150
+ // Requiring a digit keeps prose like "Bearer authentication required" intact.
151
+ (match, scheme, sep, value) => /\d/.test(value) ? `${scheme}${sep}${(0, utils_1.censor)(value)}` : match);
152
+ result = result.replace(AUTH_HEADER_ANY_SCHEME_PATTERN, (match, key, sep, scheme, value) => value.startsWith(CENSORED_PREFIX) || !/[0-9]/.test(value)
153
+ ? match
154
+ : `${key}${sep}${scheme}${(0, utils_1.censor)(value)}`);
93
155
  result = result.replace(QUERY_PARAM_PATTERN, censorPair(true));
156
+ result = result.replace(ENCODED_SECRET_PARAM_PATTERN, censorPair(true));
157
+ result = result.replace(ENCODED_QUERY_PARAM_PATTERN, censorPair(true));
158
+ result = result.replace(ASSIGNED_PAIR_PATTERN, censorPair(true));
159
+ result = result.replace(NAME_VALUE_PAIR_PATTERN, (match, key, quoteMark, name, sep, value) => {
160
+ const inner = value.slice(quoteMark.length, value.length - quoteMark.length);
161
+ return inner.startsWith(CENSORED_PREFIX) || !isSecretParam(name, false)
162
+ ? match
163
+ : `${key}${sep}${quoteMark}${(0, utils_1.censor)(inner)}${quoteMark}`;
164
+ });
94
165
  // JSON error bodies use "key" for field names, like {"key": "email"}.
95
166
  result = result.replace(QUOTED_PAIR_PATTERN, censorPair(false));
96
167
  return result.replace(LABELLED_VALUE_PATTERN, (match, key, sep, value) => {
@@ -106,6 +177,114 @@ const scanString = (text) => {
106
177
  return isIssueKey ? match : `${key}${sep}${(0, utils_1.censor)(value)}`;
107
178
  });
108
179
  };
180
+ // How many times percent-decoding is repeated: ?u=https%3A%2F%2Fb%2F%3Ftoken%3D... is one
181
+ // level, and the same link inside another link's query (as CloudFront logs it) is two or three.
182
+ const MAX_DECODINGS = 3;
183
+ const PERCENT_ESCAPE = /%[0-9A-Fa-f]{2}/;
184
+ // ASCII runs: every UTF-16 code unit below 0x80. Surrogate halves fall in the excluded range.
185
+ const ASCII_RUN = /[^\u0080-\uffff]+/g;
186
+ const HEX_PAIR = /^[0-9A-Fa-f]{2}$/;
187
+ const REPLACEMENT = '�';
188
+ // UTF-8 decoding as the WHATWG Encoding standard and Python's errors="replace" do it: each
189
+ // maximal invalid sequence becomes one U+FFFD. Written out because TextDecoder is not in
190
+ // every runtime this package supports, and it would drop a leading byte order mark.
191
+ const decodeUtf8 = (bytes) => {
192
+ const out = [];
193
+ let codePoint = 0; // grows byte by byte; const cannot accumulate
194
+ let needed = 0; // continuation bytes the current sequence still expects
195
+ let seen = 0;
196
+ let lower = 0x80;
197
+ let upper = 0xbf;
198
+ for (let i = 0; i < bytes.length; i += 1) {
199
+ const byte = bytes[i];
200
+ if (needed === 0) {
201
+ if (byte <= 0x7f) {
202
+ out.push(String.fromCharCode(byte));
203
+ }
204
+ else if (byte >= 0xc2 && byte <= 0xdf) {
205
+ needed = 1;
206
+ codePoint = byte & 0x1f;
207
+ }
208
+ else if (byte >= 0xe0 && byte <= 0xef) {
209
+ lower = byte === 0xe0 ? 0xa0 : 0x80;
210
+ upper = byte === 0xed ? 0x9f : 0xbf;
211
+ needed = 2;
212
+ codePoint = byte & 0x0f;
213
+ }
214
+ else if (byte >= 0xf0 && byte <= 0xf4) {
215
+ lower = byte === 0xf0 ? 0x90 : 0x80;
216
+ upper = byte === 0xf4 ? 0x8f : 0xbf;
217
+ needed = 3;
218
+ codePoint = byte & 0x07;
219
+ }
220
+ else {
221
+ out.push(REPLACEMENT);
222
+ }
223
+ }
224
+ else if (byte < lower || byte > upper) {
225
+ // The sequence ends early: replace it and read this byte afresh.
226
+ out.push(REPLACEMENT);
227
+ needed = 0;
228
+ seen = 0;
229
+ lower = 0x80;
230
+ upper = 0xbf;
231
+ i -= 1;
232
+ }
233
+ else {
234
+ lower = 0x80;
235
+ upper = 0xbf;
236
+ codePoint = (codePoint << 6) | (byte & 0x3f);
237
+ seen += 1;
238
+ if (seen === needed) {
239
+ out.push(String.fromCodePoint(codePoint));
240
+ needed = 0;
241
+ seen = 0;
242
+ }
243
+ }
244
+ }
245
+ if (needed !== 0) {
246
+ out.push(REPLACEMENT);
247
+ }
248
+ return out.join('');
249
+ };
250
+ const decodeAsciiRun = (run) => {
251
+ const bytes = [];
252
+ for (let i = 0; i < run.length; i += 1) {
253
+ const pair = run.slice(i + 1, i + 3);
254
+ if (run[i] === '%' && HEX_PAIR.test(pair)) {
255
+ bytes.push(parseInt(pair, 16));
256
+ i += 2;
257
+ }
258
+ else {
259
+ bytes.push(run.charCodeAt(i));
260
+ }
261
+ }
262
+ return decodeUtf8(bytes);
263
+ };
264
+ // Python's urllib.parse.unquote: each ASCII run is decoded as UTF-8, an invalid byte becomes
265
+ // U+FFFD, and other characters are kept. decodeURIComponent would throw on those bytes.
266
+ const percentDecode = (text) => text.includes('%')
267
+ ? text.replace(ASCII_RUN, (run) => run.includes('%') ? decodeAsciiRun(run) : run)
268
+ : text;
269
+ // scanString, then a second look at the percent-decoded text. Escaped punctuation (%3D, %3A,
270
+ // %22, %2520) hides a pair or a scheme from every pattern. When scanning the decoded text
271
+ // censors something, the result is that decoded text, scanned: re-encoding what survives
272
+ // could not be told apart from a secret re-encoded.
273
+ const scanStringAndDecoded = (text) => {
274
+ const scanned = scanString(text);
275
+ if (!PERCENT_ESCAPE.test(scanned)) {
276
+ return scanned;
277
+ }
278
+ let decoded = scanned;
279
+ for (let i = 0; i < MAX_DECODINGS; i += 1) {
280
+ decoded = percentDecode(decoded);
281
+ if (!PERCENT_ESCAPE.test(decoded)) {
282
+ break;
283
+ }
284
+ }
285
+ const rescanned = scanString(decoded);
286
+ return rescanned === decoded ? scanned : rescanned;
287
+ };
109
288
  /**
110
289
  * Censors secret-looking text inside string values, without being told the secrets: known
111
290
  * vendor key formats, JWTs, private keys, `Bearer` and `Basic` credentials, credentials in a URL,
@@ -115,5 +294,5 @@ const scanString = (text) => {
115
294
  *
116
295
  * Pattern-based and best effort. A secret with no known format and no telling label gets through.
117
296
  */
118
- const scanText = (input) => (0, utils_1.recurseReplace)(input, (val) => typeof val === 'string' ? scanString(val) : val);
297
+ const scanText = (input) => (0, utils_1.recurseReplace)(input, (val) => typeof val === 'string' ? scanStringAndDecoded(val) : val);
119
298
  exports.scanText = scanText;
package/package.json CHANGED
@@ -1,17 +1,9 @@
1
1
  {
2
2
  "name": "@zapier/secret-scrubber",
3
- "version": "1.2.0",
4
- "packageManager": "pnpm@10.12.1",
3
+ "version": "1.3.0",
5
4
  "description": "Confidently remove secrets and sensitive values from unstructured objects.",
6
5
  "main": "lib/index.js",
7
6
  "typings": "lib/index.d.ts",
8
- "scripts": {
9
- "build": "tsc",
10
- "lint": "eslint src",
11
- "test": "jest",
12
- "prepublishOnly": "pnpm build",
13
- "validate": "pnpm test && pnpm lint"
14
- },
15
7
  "repository": "gitlab:zapier/secret-scrubber-js",
16
8
  "keywords": [],
17
9
  "files": [
@@ -67,5 +59,11 @@
67
59
  "dependencies": {
68
60
  "create-hash": "1.2.0",
69
61
  "lodash.isplainobject": "4.0.6"
62
+ },
63
+ "scripts": {
64
+ "build": "tsc",
65
+ "lint": "eslint src",
66
+ "test": "jest",
67
+ "validate": "pnpm test && pnpm lint"
70
68
  }
71
- }
69
+ }