leakshield 0.5.3 → 0.5.5
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/dist/core/wire.js +45 -4
- package/dist/rules/builtin.js +48 -9
- package/package.json +1 -1
package/dist/core/wire.js
CHANGED
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
* then bypass rewriting; source errors and cancellation remain failures.
|
|
42
42
|
* Non-stream restoration failures are recovered by the session hook.
|
|
43
43
|
*/
|
|
44
|
+
import { createHash } from "node:crypto";
|
|
44
45
|
import { containsTokenPrefix, PSEUDONYM_TOKEN_PREFIXES, } from "../decision/pseudonymization.js";
|
|
45
46
|
import { walkSchemaStrings } from "../decision/schema-strings.js";
|
|
46
47
|
import { createRecoveringSseStream } from "./sse-recovery.js";
|
|
@@ -67,8 +68,8 @@ const STREAM_BUFFER_LIMIT = "LeakShield blocked an oversized stream buffer.";
|
|
|
67
68
|
* and user-facing errors. Existing substring assertions keep passing: the
|
|
68
69
|
* base sentence is unchanged and the code is appended in brackets.
|
|
69
70
|
*/
|
|
70
|
-
function refusal(code) {
|
|
71
|
-
return `${UNSUPPORTED_REQUEST_BODY} [${code}]`;
|
|
71
|
+
function refusal(code, detail) {
|
|
72
|
+
return `${UNSUPPORTED_REQUEST_BODY} [${detail === undefined ? code : `${code}:${detail}`}]`;
|
|
72
73
|
}
|
|
73
74
|
/**
|
|
74
75
|
* All byte caps are UTF-8 bytes (not UTF-16 code units): the bound is a memory
|
|
@@ -1176,6 +1177,45 @@ function validateResponsesReasoningParts(parts) {
|
|
|
1176
1177
|
}
|
|
1177
1178
|
}
|
|
1178
1179
|
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Standard Responses input item types this masker recognizes but still
|
|
1182
|
+
* refuses (issue #148). Membership only lets the refusal name the type: an
|
|
1183
|
+
* item reference or an unhandled tool-call output points at content the
|
|
1184
|
+
* masker has not seen, so it cannot be placed or scanned and keeps refusing.
|
|
1185
|
+
*/
|
|
1186
|
+
const UNSUPPORTED_RESPONSES_ITEM_TYPES = new Set([
|
|
1187
|
+
"item_reference",
|
|
1188
|
+
"local_shell_call",
|
|
1189
|
+
"local_shell_call_output",
|
|
1190
|
+
"shell_call",
|
|
1191
|
+
"shell_call_output",
|
|
1192
|
+
"apply_patch_call",
|
|
1193
|
+
"apply_patch_call_output",
|
|
1194
|
+
"web_search_call",
|
|
1195
|
+
"file_search_call",
|
|
1196
|
+
"computer_call",
|
|
1197
|
+
"computer_call_output",
|
|
1198
|
+
"image_generation_call",
|
|
1199
|
+
"code_interpreter_call",
|
|
1200
|
+
"mcp_call",
|
|
1201
|
+
"mcp_list_tools",
|
|
1202
|
+
"mcp_approval_request",
|
|
1203
|
+
"mcp_approval_response",
|
|
1204
|
+
]);
|
|
1205
|
+
/**
|
|
1206
|
+
* Content-free label for a refused input item (issue #148): a standard
|
|
1207
|
+
* protocol type is named as-is; anything else appears only as a short digest
|
|
1208
|
+
* and its length, because an unhandled `type` string is host-controlled text
|
|
1209
|
+
* that must not reach the log.
|
|
1210
|
+
*/
|
|
1211
|
+
function describeRefusedItemType(type) {
|
|
1212
|
+
if (typeof type !== "string")
|
|
1213
|
+
return `non-string-${type === null ? "null" : typeof type}`;
|
|
1214
|
+
if (UNSUPPORTED_RESPONSES_ITEM_TYPES.has(type))
|
|
1215
|
+
return type;
|
|
1216
|
+
const digest = createHash("sha256").update(type).digest("hex").slice(0, 8);
|
|
1217
|
+
return `unknown-${digest}-len${type.length}`;
|
|
1218
|
+
}
|
|
1179
1219
|
function validateResponsesInputItem(item, allowAttachments) {
|
|
1180
1220
|
if (typeof item === "string")
|
|
1181
1221
|
return;
|
|
@@ -1244,8 +1284,9 @@ function validateResponsesInputItem(item, allowAttachments) {
|
|
|
1244
1284
|
}
|
|
1245
1285
|
return;
|
|
1246
1286
|
default:
|
|
1247
|
-
// item_reference / unknown: cannot verify or place.
|
|
1248
|
-
|
|
1287
|
+
// item_reference / unknown: cannot verify or place. The refusal names
|
|
1288
|
+
// the type (content-free) so a host-log failure is attributable.
|
|
1289
|
+
throw new Error(refusal("responses-item-type", describeRefusedItemType(item.type)));
|
|
1249
1290
|
}
|
|
1250
1291
|
}
|
|
1251
1292
|
function validateResponsesDialect(body, allowAttachments) {
|
package/dist/rules/builtin.js
CHANGED
|
@@ -146,14 +146,20 @@ function assignedValue(match) {
|
|
|
146
146
|
* a finding and refuses as token_overlap rather than risking a hidden suffix. */
|
|
147
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
148
|
function isIssuedPlaceholderValue(match, text, index) {
|
|
149
|
-
|
|
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))
|
|
150
153
|
return false;
|
|
151
154
|
// JS `\S` stops at some characters a shell keeps in the word (NBSP, VT, FF,
|
|
152
|
-
// lone CR). Only end-of-text, space, tab, LF and CRLF end the value.
|
|
155
|
+
// lone CR). Only end-of-text, space, tab, LF, `;` and CRLF end the value.
|
|
153
156
|
const end = index + match.length;
|
|
154
157
|
const after = text[end];
|
|
155
158
|
if (after === undefined || after === " " || after === "\t" || after === "\n")
|
|
156
159
|
return true;
|
|
160
|
+
// `;` ends a shell word (command separator) and a connection-string field.
|
|
161
|
+
if (after === ";")
|
|
162
|
+
return true;
|
|
157
163
|
return after === "\r" && text[end + 1] === "\n";
|
|
158
164
|
}
|
|
159
165
|
function isCodeExpressionValue(match) {
|
|
@@ -175,13 +181,38 @@ const PLACEHOLDER = /^(?:\$\{\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\s*\}|\{\
|
|
|
175
181
|
* as in `mongodb://${user}:${pass}@host` or `postgres://<user>:<password>@…`.
|
|
176
182
|
* `userinfo` runs from just after `://` to (and possibly including) the `@`.
|
|
177
183
|
*/
|
|
178
|
-
function
|
|
184
|
+
function passwordPart(userinfo) {
|
|
179
185
|
const at = userinfo.lastIndexOf("@");
|
|
180
186
|
const body = at === -1 ? userinfo : userinfo.slice(0, at);
|
|
181
187
|
const colon = body.indexOf(":");
|
|
182
|
-
|
|
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)
|
|
183
213
|
return false;
|
|
184
|
-
|
|
214
|
+
const field = /(?:Password|Pwd)\s*=\s*([^;]*)$/i.exec(match);
|
|
215
|
+
return field !== null && ISSUED_PLACEHOLDER_VALUE.test(field[1].trim());
|
|
185
216
|
}
|
|
186
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;
|
|
187
218
|
/** True when [index, index+length) lies inside a UUID-shaped run in `text`. */
|
|
@@ -299,8 +330,9 @@ export const BUILTIN_RULES = [
|
|
|
299
330
|
// 重掃造成二次方成長。
|
|
300
331
|
pattern: /:\/\/[^\s/?#:]*:[^\s/?#]*@/gi,
|
|
301
332
|
skipPrefix: 3,
|
|
302
|
-
// 密碼段是 ${…}/{{…}}/<…> 模板佔位時不是憑證(#49
|
|
303
|
-
|
|
333
|
+
// 密碼段是 ${…}/{{…}}/<…> 模板佔位時不是憑證(#49);整個密碼段剛好是
|
|
334
|
+
// 已發出的 placeholder 時,憑證內容已遮罩(#154)。
|
|
335
|
+
validate: (match) => !placeholderPassword(match) && !issuedPlaceholderPassword(match),
|
|
304
336
|
},
|
|
305
337
|
{
|
|
306
338
|
id: "jwt",
|
|
@@ -440,8 +472,12 @@ export const BUILTIN_RULES = [
|
|
|
440
472
|
severity: "HIGH",
|
|
441
473
|
description: "資料庫連線字串(含密碼)",
|
|
442
474
|
pattern: /(?:mongodb(?:\+srv)?|postgresql|postgres|mysql|redis|mssql):\/\/[^:\s]+:[^@\s]+@[^\s"']+/gi,
|
|
443
|
-
// 密碼段是模板佔位時不是憑證(#49
|
|
444
|
-
|
|
475
|
+
// 密碼段是模板佔位時不是憑證(#49);整個密碼段剛好是已發出的 placeholder
|
|
476
|
+
// 時,憑證內容已遮罩(#154)。
|
|
477
|
+
validate: (match) => {
|
|
478
|
+
const userinfo = match.slice(match.indexOf("://") + 3);
|
|
479
|
+
return !placeholderPassword(userinfo) && !issuedPlaceholderPassword(userinfo);
|
|
480
|
+
},
|
|
445
481
|
},
|
|
446
482
|
{
|
|
447
483
|
id: "db-connection-ado",
|
|
@@ -450,6 +486,9 @@ export const BUILTIN_RULES = [
|
|
|
450
486
|
description: "DB Connection String(ADO 格式)",
|
|
451
487
|
// (?:[^;]+;)* 允許 Server 與 Password 之間存在任意數量的中間欄位
|
|
452
488
|
pattern: /(?:Data Source|Server|Host)\s*=\s*[^;]+;(?:[^;]+;)*\s*(?:Password|Pwd)\s*=\s*[^;]+/gi,
|
|
489
|
+
// 密碼欄位整個值剛好是已發出的 placeholder 時,憑證內容已遮罩(#154);
|
|
490
|
+
// 值前後有任何其他字元就不豁免。
|
|
491
|
+
validate: (match) => !issuedPlaceholderAdoPassword(match),
|
|
453
492
|
},
|
|
454
493
|
// ── 金融/網路 PII(regex 框形狀,validate 再驗校驗碼/可路由性)────────
|
|
455
494
|
{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leakshield",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.5",
|
|
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",
|