leakshield 0.5.2 → 0.5.4

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.
@@ -138,8 +138,32 @@ const CODE_CHAIN = new RegExp(`^${IDENT}${STEP}*${TAIL}`);
138
138
  const CONFIG_MEMBER = new RegExp(`^(?:process\\.env|import\\.meta\\.env|os\\.environ|ENV|this|self|config|cfg|conf|settings|options|opts|props|params|args|req|request|ctx|context|env|secrets)${STEP}+${TAIL}`);
139
139
  /** Longer values are not exempted; a real expression on one line is short. */
140
140
  const MAX_EXPRESSION_LENGTH = 256;
141
+ function assignedValue(match) {
142
+ return match.replace(/^[^:=]*[:=]\s*/, "");
143
+ }
144
+ /** A whole issued placeholder as an unquoted value. A quoted value is
145
+ * ambiguous (a shell word can continue after the closing quote), so it stays
146
+ * a finding and refuses as token_overlap rather than risking a hidden suffix. */
147
+ const ISSUED_PLACEHOLDER_VALUE = /^\[SGP(?:1:[0-9a-f]{32}-\d{8,12}|2:[A-Z][A-Z_]{0,31}-[a-z]{4}-[a-z]{4}-[a-z]{4})\]$/;
148
+ function isIssuedPlaceholderValue(match, text, index) {
149
+ // The unquoted value branch swallows a trailing `;` (`\S` includes it); in a
150
+ // connection string that semicolon is the field delimiter, not the value.
151
+ const value = assignedValue(match).replace(/;+$/, "").trim();
152
+ if (!ISSUED_PLACEHOLDER_VALUE.test(value))
153
+ return false;
154
+ // JS `\S` stops at some characters a shell keeps in the word (NBSP, VT, FF,
155
+ // lone CR). Only end-of-text, space, tab, LF, `;` and CRLF end the value.
156
+ const end = index + match.length;
157
+ const after = text[end];
158
+ if (after === undefined || after === " " || after === "\t" || after === "\n")
159
+ return true;
160
+ // `;` ends a shell word (command separator) and a connection-string field.
161
+ if (after === ";")
162
+ return true;
163
+ return after === "\r" && text[end + 1] === "\n";
164
+ }
141
165
  function isCodeExpressionValue(match) {
142
- const value = match.replace(/^[^:=]*[:=]\s*/, "");
166
+ const value = assignedValue(match);
143
167
  if (value.length > MAX_EXPRESSION_LENGTH)
144
168
  return false;
145
169
  if (value.startsWith("'") || value.startsWith('"'))
@@ -157,13 +181,38 @@ const PLACEHOLDER = /^(?:\$\{\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\s*\}|\{\
157
181
  * as in `mongodb://${user}:${pass}@host` or `postgres://<user>:<password>@…`.
158
182
  * `userinfo` runs from just after `://` to (and possibly including) the `@`.
159
183
  */
160
- function placeholderPassword(userinfo) {
184
+ function passwordPart(userinfo) {
161
185
  const at = userinfo.lastIndexOf("@");
162
186
  const body = at === -1 ? userinfo : userinfo.slice(0, at);
163
187
  const colon = body.indexOf(":");
164
- if (colon === -1)
188
+ return colon === -1 ? undefined : body.slice(colon + 1);
189
+ }
190
+ function placeholderPassword(userinfo) {
191
+ const password = passwordPart(userinfo);
192
+ return password !== undefined && PLACEHOLDER.test(password);
193
+ }
194
+ /**
195
+ * The password part is exactly one issued placeholder: the secret it replaced
196
+ * is already masked, and the rule's own delimiter (`@` or `;`/end) ends the
197
+ * value. Any surrounding text, quoting, or continuation makes the segment
198
+ * differ from the token and stays a finding.
199
+ */
200
+ function issuedPlaceholderPassword(userinfo) {
201
+ const password = passwordPart(userinfo);
202
+ return password !== undefined && ISSUED_PLACEHOLDER_VALUE.test(password);
203
+ }
204
+ /** The `Password=`/`Pwd=` field at the end of an ADO string, checked whole. */
205
+ function issuedPlaceholderAdoPassword(match) {
206
+ // The pattern's greedy middle can cover more than one password field. Only a
207
+ // match with a single field can be exempted: an earlier plaintext field would
208
+ // otherwise pass next to the masked one. Embedded or quoted `Password=` text
209
+ // also counts, so the overcount refuses conservatively instead of exempting a
210
+ // match that might hide another field.
211
+ const fields = match.match(/(?:Password|Pwd)\s*=/gi);
212
+ if (fields === null || fields.length !== 1)
165
213
  return false;
166
- return PLACEHOLDER.test(body.slice(colon + 1));
214
+ const field = /(?:Password|Pwd)\s*=\s*([^;]*)$/i.exec(match);
215
+ return field !== null && ISSUED_PLACEHOLDER_VALUE.test(field[1].trim());
167
216
  }
168
217
  const UUID_SHAPE = /[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}/g;
169
218
  /** True when [index, index+length) lies inside a UUID-shaped run in `text`. */
@@ -266,8 +315,9 @@ export const BUILTIN_RULES = [
266
315
  // 同時涵蓋有引號(YAML/JSON)與無引號(.env)寫法
267
316
  // (?!\$\{) 排除環境變數參照 ${VAR};(?!\*) 排除遮罩值 ********
268
317
  pattern: /(?:password|passwd|secret|private_key|client_secret)\s*[:=]\s*(?:['"](?!\$\{)[^'"]{8,}['"]|(?!['"\$\*])\S{8,})/gi,
269
- // 無引號值若是程式碼表達式(process.env.X、z.string()、config.secret)不算機密(#49)
270
- validate: (match) => !isCodeExpressionValue(match),
318
+ // 無引號值若是程式碼表達式(process.env.X、z.string()、config.secret)不算機密(#49);
319
+ // 值「整個」是已發出的 placeholder 時才是已遮罩內容;混入其他文字仍要偵測(可能跨 token 而拒絕)。
320
+ validate: (match, context) => !isCodeExpressionValue(match) && !isIssuedPlaceholderValue(match, context.text, context.index),
271
321
  },
272
322
  {
273
323
  id: "url-userinfo",
@@ -280,8 +330,9 @@ export const BUILTIN_RULES = [
280
330
  // 重掃造成二次方成長。
281
331
  pattern: /:\/\/[^\s/?#:]*:[^\s/?#]*@/gi,
282
332
  skipPrefix: 3,
283
- // 密碼段是 ${…}/{{…}}/<…> 模板佔位時不是憑證(#49)
284
- validate: (match) => !placeholderPassword(match),
333
+ // 密碼段是 ${…}/{{…}}/<…> 模板佔位時不是憑證(#49);整個密碼段剛好是
334
+ // 已發出的 placeholder 時,憑證內容已遮罩(#154)。
335
+ validate: (match) => !placeholderPassword(match) && !issuedPlaceholderPassword(match),
285
336
  },
286
337
  {
287
338
  id: "jwt",
@@ -421,8 +472,12 @@ export const BUILTIN_RULES = [
421
472
  severity: "HIGH",
422
473
  description: "資料庫連線字串(含密碼)",
423
474
  pattern: /(?:mongodb(?:\+srv)?|postgresql|postgres|mysql|redis|mssql):\/\/[^:\s]+:[^@\s]+@[^\s"']+/gi,
424
- // 密碼段是模板佔位時不是憑證(#49)
425
- validate: (match) => !placeholderPassword(match.slice(match.indexOf("://") + 3)),
475
+ // 密碼段是模板佔位時不是憑證(#49);整個密碼段剛好是已發出的 placeholder
476
+ // 時,憑證內容已遮罩(#154)。
477
+ validate: (match) => {
478
+ const userinfo = match.slice(match.indexOf("://") + 3);
479
+ return !placeholderPassword(userinfo) && !issuedPlaceholderPassword(userinfo);
480
+ },
426
481
  },
427
482
  {
428
483
  id: "db-connection-ado",
@@ -431,6 +486,9 @@ export const BUILTIN_RULES = [
431
486
  description: "DB Connection String(ADO 格式)",
432
487
  // (?:[^;]+;)* 允許 Server 與 Password 之間存在任意數量的中間欄位
433
488
  pattern: /(?:Data Source|Server|Host)\s*=\s*[^;]+;(?:[^;]+;)*\s*(?:Password|Pwd)\s*=\s*[^;]+/gi,
489
+ // 密碼欄位整個值剛好是已發出的 placeholder 時,憑證內容已遮罩(#154);
490
+ // 值前後有任何其他字元就不豁免。
491
+ validate: (match) => !issuedPlaceholderAdoPassword(match),
434
492
  },
435
493
  // ── 金融/網路 PII(regex 框形狀,validate 再驗校驗碼/可路由性)────────
436
494
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leakshield",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Masks sensitive content before it reaches an AI model: OpenCode plugin, pi extension, Claude Code proxy, CLI, and web scanner",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Jared Wang",