leakshield 0.1.3 → 0.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 +294 -173
- package/README.zh-TW.md +290 -164
- package/dist/config/loader.js +14 -10
- package/dist/config/plain-object.js +14 -0
- package/dist/decision/pseudonymization.js +133 -82
- package/dist/decision/redaction.js +2 -0
- package/dist/decision/value-search.js +250 -0
- package/dist/doctor.js +429 -138
- package/dist/i18n/en.js +4 -0
- package/dist/i18n/zh-TW.js +4 -0
- package/dist/index.js +3 -2
- package/dist/opencode-env-tools.js +0 -75
- package/dist/opencode-hooks.js +4 -73
- package/dist/opencode-transparent.js +582 -89
- package/dist/opencode.js +12 -54
- package/dist/plugin-options.js +15 -21
- package/dist/scanner/encoding.js +291 -7
- package/dist/scanner/engine.js +68 -40
- package/dist/scanner/utils.js +26 -2
- package/dist/transports/cli.js +2 -2
- package/dist/transports/web.js +3 -3
- package/dist/types.js +1 -0
- package/package.json +3 -3
package/dist/scanner/engine.js
CHANGED
|
@@ -2,7 +2,7 @@ import { BUILTIN_RULES } from "../rules/builtin.js";
|
|
|
2
2
|
import { buildDynamicRules } from "../rules/dynamic.js";
|
|
3
3
|
import { allowSet, isAllowlisted, noteAllowlistSuppression } from "./allowlist.js";
|
|
4
4
|
import { containsAddressInRun, parseCidrList } from "./cidr.js";
|
|
5
|
-
import { redact,
|
|
5
|
+
import { redact, createLineIndex, lineColAt, getIgnoredLines } from "./utils.js";
|
|
6
6
|
import { t } from "../i18n/index.js";
|
|
7
7
|
import * as encoding from "./encoding.js";
|
|
8
8
|
import { foldConfusables } from "./unicode.js";
|
|
@@ -29,8 +29,8 @@ function getRules(config) {
|
|
|
29
29
|
: rule);
|
|
30
30
|
return [...configuredBuiltins, ...dynamic];
|
|
31
31
|
}
|
|
32
|
-
function
|
|
33
|
-
return findings.some((finding) => finding.match_start === index && finding.match_end === index + length);
|
|
32
|
+
function hasHighFindingAtRange(findings, index, length) {
|
|
33
|
+
return findings.some((finding) => finding.severity === "HIGH" && finding.match_start === index && finding.match_end === index + length);
|
|
34
34
|
}
|
|
35
35
|
function collapseOverlappingMatches(matches) {
|
|
36
36
|
const sorted = [...matches].sort((left, right) => left.index - right.index || right.length - left.length);
|
|
@@ -56,15 +56,17 @@ function collapseOverlappingMatches(matches) {
|
|
|
56
56
|
}))
|
|
57
57
|
.sort((left, right) => right.index - left.index);
|
|
58
58
|
}
|
|
59
|
-
function addExactValueFindings(text, config, ignoredLines, findings) {
|
|
59
|
+
function addExactValueFindings(text, lineStarts, config, ignoredLines, findings) {
|
|
60
60
|
const detector = config.exact_value_detector;
|
|
61
61
|
if (!detector)
|
|
62
62
|
return;
|
|
63
63
|
const globalAllow = allowSet(config.settings.allowlist);
|
|
64
64
|
for (const match of detector.findAll(text)) {
|
|
65
|
-
|
|
65
|
+
// A HIGH rule finding already says all ENV_SECRET would. A weaker one must
|
|
66
|
+
// not hide it, or the decision drops and --min-severity high loses the match.
|
|
67
|
+
if (hasHighFindingAtRange(findings, match.index, match.length))
|
|
66
68
|
continue;
|
|
67
|
-
const { line, col } =
|
|
69
|
+
const { line, col } = lineColAt(lineStarts, match.index);
|
|
68
70
|
if (ignoredLines.has(line))
|
|
69
71
|
continue;
|
|
70
72
|
const matchedText = text.slice(match.index, match.index + match.length);
|
|
@@ -90,10 +92,17 @@ function honorDirectives(opts) {
|
|
|
90
92
|
}
|
|
91
93
|
/** Invisible format characters that can splice a value so the rules miss it. */
|
|
92
94
|
export const ZERO_WIDTH_CHARS = /[\u200B-\u200D\uFEFF\u2060\u180E]/g;
|
|
93
|
-
|
|
95
|
+
const SEVERITY_RANK = { HIGH: 3, MEDIUM: 2, LOW: 1 };
|
|
96
|
+
/**
|
|
97
|
+
* Rule and exact-value findings for one text, in rule order. Rules of one type
|
|
98
|
+
* matching at one offset report one finding: the highest severity, over the
|
|
99
|
+
* longest span. Rule order must not decide either, or a LOW rule listed first
|
|
100
|
+
* would downgrade the decision and leave the tail of a longer match unmasked.
|
|
101
|
+
*/
|
|
94
102
|
function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
103
|
+
const lineStarts = createLineIndex(text);
|
|
95
104
|
const findings = [];
|
|
96
|
-
const
|
|
105
|
+
const findingAtOffset = new Map();
|
|
97
106
|
const globalAllow = allowSet(config.settings.allowlist);
|
|
98
107
|
for (const rule of rules) {
|
|
99
108
|
// Start from the beginning. matchAll advances after empty matches (by a
|
|
@@ -109,10 +118,8 @@ function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
|
109
118
|
const start = match.index + offset;
|
|
110
119
|
if (rule.validate !== undefined && !rule.validate(matchedText, { text, index: start }))
|
|
111
120
|
continue;
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
continue;
|
|
115
|
-
const { line, col } = getLineCol(text, start);
|
|
121
|
+
const end = match.index + match[0].length;
|
|
122
|
+
const { line, col } = lineColAt(lineStarts, start);
|
|
116
123
|
if (ignoredLines.has(line))
|
|
117
124
|
continue;
|
|
118
125
|
if (isAllowlisted(matchedText, globalAllow, rule.allow)) {
|
|
@@ -121,9 +128,22 @@ function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
|
121
128
|
// at the same offset, and suppressing the short one must not hide it.
|
|
122
129
|
continue;
|
|
123
130
|
}
|
|
124
|
-
seen.add(key);
|
|
125
131
|
const preview = redact(matchedText, config.settings.preview_length);
|
|
126
|
-
|
|
132
|
+
const key = `${rule.type}:${start}`;
|
|
133
|
+
const earlier = findingAtOffset.get(key);
|
|
134
|
+
if (earlier) {
|
|
135
|
+
if (SEVERITY_RANK[rule.severity] > SEVERITY_RANK[earlier.severity]) {
|
|
136
|
+
earlier.severity = rule.severity;
|
|
137
|
+
earlier.description = rule.description;
|
|
138
|
+
}
|
|
139
|
+
if (end > (earlier.match_end ?? 0)) {
|
|
140
|
+
earlier.preview = preview;
|
|
141
|
+
earlier.match_end = end;
|
|
142
|
+
earlier.match_text = matchedText;
|
|
143
|
+
}
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const finding = {
|
|
127
147
|
type: rule.type,
|
|
128
148
|
severity: rule.severity,
|
|
129
149
|
line: config.settings.show_line_numbers ? line : undefined,
|
|
@@ -131,12 +151,14 @@ function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
|
131
151
|
preview,
|
|
132
152
|
description: rule.description,
|
|
133
153
|
match_start: start,
|
|
134
|
-
match_end:
|
|
154
|
+
match_end: end,
|
|
135
155
|
match_text: matchedText,
|
|
136
|
-
}
|
|
156
|
+
};
|
|
157
|
+
findingAtOffset.set(key, finding);
|
|
158
|
+
findings.push(finding);
|
|
137
159
|
}
|
|
138
160
|
}
|
|
139
|
-
addExactValueFindings(text, config, ignoredLines, findings);
|
|
161
|
+
addExactValueFindings(text, lineStarts, config, ignoredLines, findings);
|
|
140
162
|
return findings;
|
|
141
163
|
}
|
|
142
164
|
/**
|
|
@@ -192,6 +214,10 @@ function homoglyphHidesFinding(text, rawFindings, rules, config, ignoredLines) {
|
|
|
192
214
|
const fold = (value) => foldConfusables(stripZeroWidth(value));
|
|
193
215
|
return transformHidesFinding(text, fold, rawFindings, rules, config, ignoredLines);
|
|
194
216
|
}
|
|
217
|
+
/** Appends the ` [WARNING: a, b]` suffix to a summary; no suffix when there is none. */
|
|
218
|
+
export function appendWarnings(summary, warnings) {
|
|
219
|
+
return warnings !== undefined && warnings.length > 0 ? `${summary} [WARNING: ${warnings.join(", ")}]` : summary;
|
|
220
|
+
}
|
|
195
221
|
/**
|
|
196
222
|
* Internal scan function without encoding detection.
|
|
197
223
|
* Used by scanTextWithDecoding to avoid infinite recursion.
|
|
@@ -248,36 +274,37 @@ function scanTextInternal(text, config, opts) {
|
|
|
248
274
|
// Add warnings for Unicode issues and truncation
|
|
249
275
|
const warnings = [];
|
|
250
276
|
if (zeroWidthEvasion)
|
|
251
|
-
warnings.push("
|
|
277
|
+
warnings.push(t("scan.warning_zero_width"));
|
|
252
278
|
if (homoglyphEvasion)
|
|
253
|
-
warnings.push("
|
|
279
|
+
warnings.push(t("scan.warning_suspicious_unicode"));
|
|
254
280
|
if (wasTruncated)
|
|
255
|
-
warnings.push("
|
|
256
|
-
const finalSummary = warnings.length > 0
|
|
257
|
-
? summary + ` [WARNING: ${warnings.join(', ')}]`
|
|
258
|
-
: summary;
|
|
281
|
+
warnings.push(t("scan.warning_truncated"));
|
|
259
282
|
return {
|
|
260
283
|
safe,
|
|
261
284
|
total_findings: findings.length,
|
|
262
285
|
findings,
|
|
263
|
-
summary:
|
|
286
|
+
summary: appendWarnings(summary, warnings),
|
|
264
287
|
recommendation,
|
|
265
288
|
was_truncated: wasTruncated || undefined,
|
|
289
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
266
290
|
};
|
|
267
291
|
}
|
|
268
292
|
export function scanText(text, config, opts) {
|
|
269
293
|
// If encoding detection is enabled, use the enhanced scanner
|
|
270
294
|
if (config.settings.encoding_detection) {
|
|
271
295
|
const result = encoding.scanTextWithDecoding(text, config, opts);
|
|
272
|
-
//
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
const
|
|
296
|
+
// Decoded findings carry offsets of the decoded string; anchor them to
|
|
297
|
+
// spans of `text` so redaction and previews apply to the right bytes.
|
|
298
|
+
const findings = encoding.anchorDecodedFindings(text, result, config, opts);
|
|
299
|
+
// Generate proper summary based on the anchored results
|
|
300
|
+
const safe = findings.length === 0;
|
|
301
|
+
const highCount = findings.filter((f) => f.severity === "HIGH").length;
|
|
302
|
+
const medCount = findings.filter((f) => f.severity === "MEDIUM").length;
|
|
303
|
+
const lowCount = findings.length - highCount - medCount;
|
|
277
304
|
const summary = safe
|
|
278
305
|
? t("scan.safe_summary")
|
|
279
306
|
: t("scan.unsafe_summary", {
|
|
280
|
-
count:
|
|
307
|
+
count: findings.length,
|
|
281
308
|
high: highCount,
|
|
282
309
|
med: medCount,
|
|
283
310
|
low: lowCount,
|
|
@@ -288,21 +315,19 @@ export function scanText(text, config, opts) {
|
|
|
288
315
|
// Add warnings for encoding detection and truncation
|
|
289
316
|
const warnings = [];
|
|
290
317
|
if (result.was_decoded) {
|
|
291
|
-
warnings.push(
|
|
318
|
+
warnings.push(t("scan.warning_encoded", { types: result.encoding_types_found.join(", ") }));
|
|
292
319
|
}
|
|
293
320
|
if (result.original.was_truncated) {
|
|
294
|
-
warnings.push("
|
|
321
|
+
warnings.push(t("scan.warning_truncated"));
|
|
295
322
|
}
|
|
296
|
-
const finalSummary = warnings.length > 0
|
|
297
|
-
? summary + ` [WARNING: ${warnings.join(', ')}]`
|
|
298
|
-
: summary;
|
|
299
323
|
return {
|
|
300
324
|
safe,
|
|
301
|
-
total_findings:
|
|
302
|
-
findings
|
|
303
|
-
summary:
|
|
325
|
+
total_findings: findings.length,
|
|
326
|
+
findings,
|
|
327
|
+
summary: appendWarnings(summary, warnings),
|
|
304
328
|
recommendation,
|
|
305
329
|
was_truncated: result.original.was_truncated,
|
|
330
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
306
331
|
};
|
|
307
332
|
}
|
|
308
333
|
// Otherwise, use internal scan without encoding detection
|
|
@@ -326,6 +351,7 @@ protectedSpans = []) {
|
|
|
326
351
|
const truncated = text.slice(0, config.settings.max_chars);
|
|
327
352
|
const allRules = getRules(config);
|
|
328
353
|
const ignoredLines = honorDirectives(opts) ? getIgnoredLines(truncated) : new Set();
|
|
354
|
+
const truncatedLineStarts = createLineIndex(truncated);
|
|
329
355
|
const matches = [];
|
|
330
356
|
const seen = new Set();
|
|
331
357
|
const globalAllow = allowSet(config.settings.allowlist);
|
|
@@ -346,7 +372,7 @@ protectedSpans = []) {
|
|
|
346
372
|
const key = `${start}:${end - start}`;
|
|
347
373
|
if (seen.has(key))
|
|
348
374
|
continue;
|
|
349
|
-
const { line } =
|
|
375
|
+
const { line } = lineColAt(truncatedLineStarts, start);
|
|
350
376
|
if (ignoredLines.has(line))
|
|
351
377
|
continue;
|
|
352
378
|
// An allowlisted literal is left as written, exactly as the scanner leaves
|
|
@@ -362,9 +388,11 @@ protectedSpans = []) {
|
|
|
362
388
|
for (const match of detector.findAll(truncated)) {
|
|
363
389
|
if (overlapsSpan(match.index, match.index + match.length, protectedSpans))
|
|
364
390
|
continue;
|
|
391
|
+
// Only the mask is decided here, so any match on this range already covers
|
|
392
|
+
// it. Severity is reported by the scanText call below.
|
|
365
393
|
if (matches.some((entry) => entry.index === match.index && entry.length === match.length))
|
|
366
394
|
continue;
|
|
367
|
-
const { line } =
|
|
395
|
+
const { line } = lineColAt(truncatedLineStarts, match.index);
|
|
368
396
|
if (ignoredLines.has(line))
|
|
369
397
|
continue;
|
|
370
398
|
const matchedText = truncated.slice(match.index, match.index + match.length);
|
package/dist/scanner/utils.js
CHANGED
|
@@ -7,8 +7,32 @@ export function redact(_value, _previewLen) {
|
|
|
7
7
|
}
|
|
8
8
|
export function getLineCol(text, index) {
|
|
9
9
|
const before = text.slice(0, index);
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
return lineColAt(createLineIndex(before), before.length);
|
|
11
|
+
}
|
|
12
|
+
/** Start offset of every line in a text; a line break is `\r?\n`, so only `\n` ends a line. */
|
|
13
|
+
export function createLineIndex(text) {
|
|
14
|
+
const starts = [0];
|
|
15
|
+
let from = 0;
|
|
16
|
+
for (;;) {
|
|
17
|
+
const nl = text.indexOf("\n", from);
|
|
18
|
+
if (nl === -1)
|
|
19
|
+
return starts;
|
|
20
|
+
from = nl + 1;
|
|
21
|
+
starts.push(from);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Line (1-based) and column (1-based) of `index`, given the starts from createLineIndex of the same text. */
|
|
25
|
+
export function lineColAt(lineStarts, index) {
|
|
26
|
+
let lo = 0;
|
|
27
|
+
let hi = lineStarts.length - 1;
|
|
28
|
+
while (lo < hi) {
|
|
29
|
+
const mid = (lo + hi + 1) >> 1;
|
|
30
|
+
if (lineStarts[mid] <= index)
|
|
31
|
+
lo = mid;
|
|
32
|
+
else
|
|
33
|
+
hi = mid - 1;
|
|
34
|
+
}
|
|
35
|
+
return { line: lo + 1, col: index - lineStarts[lo] + 1 };
|
|
12
36
|
}
|
|
13
37
|
/**
|
|
14
38
|
* Multiple ignore patterns to prevent bypass attempts.
|
package/dist/transports/cli.js
CHANGED
|
@@ -2,7 +2,7 @@ import * as fs from "fs";
|
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
import chalk from "chalk";
|
|
4
4
|
import { loadConfig } from "../config/loader.js";
|
|
5
|
-
import { scanText, redactText } from "../scanner/engine.js";
|
|
5
|
+
import { scanText, redactText, appendWarnings } from "../scanner/engine.js";
|
|
6
6
|
import { redactAndVerify } from "../scanner/redaction-verifier.js";
|
|
7
7
|
import { sanitizeRedactResultForExport } from "../decision/redaction.js";
|
|
8
8
|
import { toDecisionResult } from "../decision/model.js";
|
|
@@ -28,7 +28,7 @@ export function filterBySeverity(result, min) {
|
|
|
28
28
|
med: medCount,
|
|
29
29
|
low: findings.length - highCount - medCount,
|
|
30
30
|
});
|
|
31
|
-
return { ...result, findings, total_findings: findings.length, safe, summary };
|
|
31
|
+
return { ...result, findings, total_findings: findings.length, safe, summary: appendWarnings(summary, result.warnings) };
|
|
32
32
|
}
|
|
33
33
|
// ─── Decision-aware CLI formatting ──────────────────────────────────────────
|
|
34
34
|
const SEPARATOR = "─".repeat(60);
|
package/dist/transports/web.js
CHANGED
|
@@ -147,7 +147,8 @@ function handleScanRequest(req, res, config) {
|
|
|
147
147
|
: undefined;
|
|
148
148
|
writeJson(res, 200, {
|
|
149
149
|
...decision,
|
|
150
|
-
|
|
150
|
+
// Replaces the decision's copy too: only text the verifier passed is returned.
|
|
151
|
+
redactedContent: redaction?.verified ? redaction.redacted : undefined,
|
|
151
152
|
redactionVerified: redaction?.verified,
|
|
152
153
|
residualFindings: redaction?.residualFindings,
|
|
153
154
|
wasTruncated: redaction?.wasTruncated ?? scan.was_truncated ?? false,
|
|
@@ -164,8 +165,7 @@ function handleScanRequest(req, res, config) {
|
|
|
164
165
|
res.destroy();
|
|
165
166
|
});
|
|
166
167
|
}
|
|
167
|
-
export function createWebServer() {
|
|
168
|
-
const config = loadConfig();
|
|
168
|
+
export function createWebServer(config = loadConfig()) {
|
|
169
169
|
return http.createServer((req, res) => {
|
|
170
170
|
if (!isAllowedHost(req.headers.host)) {
|
|
171
171
|
writeError(res, 403, "不允許的 Host");
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "leakshield",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "OpenCode plugin, CLI, and web scanner for sensitive content before sending it to AI",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -55,12 +55,12 @@
|
|
|
55
55
|
"bundle:mac": "npm run binary && bash scripts/bundle-mac.sh"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@opencode-ai/plugin": "^1.17.18",
|
|
59
58
|
"chalk": "^5.3.0",
|
|
60
59
|
"js-yaml": "^4.3.2",
|
|
61
|
-
"zod": "^
|
|
60
|
+
"zod": "^4.6.7"
|
|
62
61
|
},
|
|
63
62
|
"devDependencies": {
|
|
63
|
+
"@opencode-ai/plugin": "^1.17.18",
|
|
64
64
|
"@opencode-ai/sdk": "^1.17.18",
|
|
65
65
|
"@opencode/plugin": "^2.0.19",
|
|
66
66
|
"@types/js-yaml": "^4.0.9",
|