leakshield 0.1.1 → 0.1.3
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 +62 -4
- package/README.zh-TW.md +8 -2
- package/dist/config/loader.js +51 -0
- package/dist/decision/pseudonymization.js +106 -19
- package/dist/doctor.js +167 -0
- package/dist/opencode-transparent.js +1 -1
- package/dist/rules/builtin.js +136 -3
- package/dist/rules/dynamic.js +259 -13
- package/dist/scanner/allowlist.js +37 -0
- package/dist/scanner/cidr.js +96 -0
- package/dist/scanner/encoding.js +5 -1
- package/dist/scanner/engine.js +106 -39
- package/dist/scanner/remotes.js +140 -0
- package/dist/scanner/unicode.js +30 -0
- package/package.json +1 -1
package/dist/scanner/engine.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { BUILTIN_RULES } from "../rules/builtin.js";
|
|
2
2
|
import { buildDynamicRules } from "../rules/dynamic.js";
|
|
3
|
+
import { allowSet, isAllowlisted, noteAllowlistSuppression } from "./allowlist.js";
|
|
4
|
+
import { containsAddressInRun, parseCidrList } from "./cidr.js";
|
|
3
5
|
import { redact, getLineCol, getIgnoredLines } from "./utils.js";
|
|
4
6
|
import { t } from "../i18n/index.js";
|
|
5
7
|
import * as encoding from "./encoding.js";
|
|
6
|
-
import {
|
|
8
|
+
import { foldConfusables } from "./unicode.js";
|
|
7
9
|
// Dynamic rules are derived from config; cache per config object so they are
|
|
8
10
|
// rebuilt only when the config reference changes (e.g. after reload_config).
|
|
9
11
|
const dynamicCache = new WeakMap();
|
|
@@ -18,7 +20,14 @@ function getRules(config) {
|
|
|
18
20
|
// builtin without an id would silently become always-on — tests forbid that.
|
|
19
21
|
const overrides = config.settings.rule_overrides ?? {};
|
|
20
22
|
const activeBuiltins = BUILTIN_RULES.filter((rule) => (rule.id !== undefined && rule.id in overrides ? overrides[rule.id] : !rule.optIn));
|
|
21
|
-
|
|
23
|
+
// The private-range rule is the only builtin whose verdict depends on
|
|
24
|
+
// configuration. Bind the configured networks here so the shared rule object
|
|
25
|
+
// stays constant and an empty list still means the documented defaults.
|
|
26
|
+
const privateNetworks = parseCidrList(config.settings.private_cidrs);
|
|
27
|
+
const configuredBuiltins = activeBuiltins.map((rule) => rule.id === "private-ipv4"
|
|
28
|
+
? { ...rule, validate: (match) => containsAddressInRun(privateNetworks, match) }
|
|
29
|
+
: rule);
|
|
30
|
+
return [...configuredBuiltins, ...dynamic];
|
|
22
31
|
}
|
|
23
32
|
function hasMatchingRange(findings, index, length) {
|
|
24
33
|
return findings.some((finding) => finding.match_start === index && finding.match_end === index + length);
|
|
@@ -51,12 +60,18 @@ function addExactValueFindings(text, config, ignoredLines, findings) {
|
|
|
51
60
|
const detector = config.exact_value_detector;
|
|
52
61
|
if (!detector)
|
|
53
62
|
return;
|
|
63
|
+
const globalAllow = allowSet(config.settings.allowlist);
|
|
54
64
|
for (const match of detector.findAll(text)) {
|
|
55
65
|
if (hasMatchingRange(findings, match.index, match.length))
|
|
56
66
|
continue;
|
|
57
67
|
const { line, col } = getLineCol(text, match.index);
|
|
58
68
|
if (ignoredLines.has(line))
|
|
59
69
|
continue;
|
|
70
|
+
const matchedText = text.slice(match.index, match.index + match.length);
|
|
71
|
+
if (isAllowlisted(matchedText, globalAllow, undefined)) {
|
|
72
|
+
noteAllowlistSuppression();
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
60
75
|
findings.push({
|
|
61
76
|
type: "ENV_SECRET",
|
|
62
77
|
severity: "HIGH",
|
|
@@ -66,7 +81,7 @@ function addExactValueFindings(text, config, ignoredLines, findings) {
|
|
|
66
81
|
description: "Value from configured env file",
|
|
67
82
|
match_start: match.index,
|
|
68
83
|
match_end: match.index + match.length,
|
|
69
|
-
match_text:
|
|
84
|
+
match_text: matchedText,
|
|
70
85
|
});
|
|
71
86
|
}
|
|
72
87
|
}
|
|
@@ -79,22 +94,34 @@ export const ZERO_WIDTH_CHARS = /[\u200B-\u200D\uFEFF\u2060\u180E]/g;
|
|
|
79
94
|
function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
80
95
|
const findings = [];
|
|
81
96
|
const seen = new Set();
|
|
97
|
+
const globalAllow = allowSet(config.settings.allowlist);
|
|
82
98
|
for (const rule of rules) {
|
|
83
|
-
//
|
|
84
|
-
//
|
|
99
|
+
// Start from the beginning. matchAll advances after empty matches (by a
|
|
100
|
+
// code point under /u); exec alone leaves lastIndex unchanged and can hang.
|
|
85
101
|
rule.pattern.lastIndex = 0;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (
|
|
102
|
+
for (const match of text.matchAll(rule.pattern)) {
|
|
103
|
+
// A zero-width match contains no value to report or mask. Skip it before
|
|
104
|
+
// dedupe, validation or suppression, without losing later real matches.
|
|
105
|
+
if (match[0].length === 0)
|
|
106
|
+
continue;
|
|
107
|
+
const offset = rule.skipPrefix ?? 0;
|
|
108
|
+
const matchedText = match[0].slice(offset);
|
|
109
|
+
const start = match.index + offset;
|
|
110
|
+
if (rule.validate !== undefined && !rule.validate(matchedText, { text, index: start }))
|
|
90
111
|
continue;
|
|
91
|
-
const key = `${rule.type}:${
|
|
112
|
+
const key = `${rule.type}:${start}`;
|
|
92
113
|
if (seen.has(key))
|
|
93
114
|
continue;
|
|
94
|
-
|
|
95
|
-
const { line, col } = getLineCol(text, match.index);
|
|
115
|
+
const { line, col } = getLineCol(text, start);
|
|
96
116
|
if (ignoredLines.has(line))
|
|
97
117
|
continue;
|
|
118
|
+
if (isAllowlisted(matchedText, globalAllow, rule.allow)) {
|
|
119
|
+
noteAllowlistSuppression();
|
|
120
|
+
// Do NOT reserve the dedupe key: another rule may match a longer span
|
|
121
|
+
// at the same offset, and suppressing the short one must not hide it.
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
seen.add(key);
|
|
98
125
|
const preview = redact(matchedText, config.settings.preview_length);
|
|
99
126
|
findings.push({
|
|
100
127
|
type: rule.type,
|
|
@@ -103,8 +130,8 @@ function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
|
103
130
|
column: config.settings.show_line_numbers ? col : undefined,
|
|
104
131
|
preview,
|
|
105
132
|
description: rule.description,
|
|
106
|
-
match_start:
|
|
107
|
-
match_end: match.index +
|
|
133
|
+
match_start: start,
|
|
134
|
+
match_end: match.index + match[0].length,
|
|
108
135
|
match_text: matchedText,
|
|
109
136
|
});
|
|
110
137
|
}
|
|
@@ -113,24 +140,22 @@ function collectRuleFindings(text, rules, config, ignoredLines) {
|
|
|
113
140
|
return findings;
|
|
114
141
|
}
|
|
115
142
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* yields a finding the raw text does not. Line breaks are untouched, so the
|
|
121
|
-
* raw text's ignored lines still apply.
|
|
143
|
+
* True when `transform` (an evasion-undoing normalization that never moves
|
|
144
|
+
* line breaks) makes the rules find something the raw text does not. Raw
|
|
145
|
+
* findings are compared by type and transformed matched text, so a value that
|
|
146
|
+
* is already detected raw does not count twice.
|
|
122
147
|
*/
|
|
123
|
-
function
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
148
|
+
function transformHidesFinding(text, transform, rawFindings, rules, config, ignoredLines) {
|
|
149
|
+
const transformed = transform(text);
|
|
150
|
+
if (transformed === text)
|
|
126
151
|
return false;
|
|
127
|
-
const identity = (finding) => `${finding.type}\0${(finding.match_text ?? "")
|
|
152
|
+
const identity = (finding) => `${finding.type}\0${transform(finding.match_text ?? "")}`;
|
|
128
153
|
const unmatched = new Map();
|
|
129
154
|
for (const finding of rawFindings) {
|
|
130
155
|
const id = identity(finding);
|
|
131
156
|
unmatched.set(id, (unmatched.get(id) ?? 0) + 1);
|
|
132
157
|
}
|
|
133
|
-
for (const finding of collectRuleFindings(
|
|
158
|
+
for (const finding of collectRuleFindings(transformed, rules, config, ignoredLines)) {
|
|
134
159
|
const id = identity(finding);
|
|
135
160
|
const remaining = unmatched.get(id) ?? 0;
|
|
136
161
|
if (remaining === 0)
|
|
@@ -139,6 +164,34 @@ function zeroWidthHidesFinding(text, rawFindings, rules, config, ignoredLines) {
|
|
|
139
164
|
}
|
|
140
165
|
return false;
|
|
141
166
|
}
|
|
167
|
+
const stripZeroWidth = (value) => value.replace(ZERO_WIDTH_CHARS, "");
|
|
168
|
+
/**
|
|
169
|
+
* Zero-width characters are common in harmless text: emoji ZWJ sequences, a
|
|
170
|
+
* BOM at the start of a file, ZWNJ in Persian. They only matter when they
|
|
171
|
+
* splice a sensitive value so the rules miss it (`pass\u200Dword=…`, a key
|
|
172
|
+
* split by ZWSP). Strip them, rescan, and report only when the stripped text
|
|
173
|
+
* yields a finding the raw text does not. Line breaks are untouched, so the
|
|
174
|
+
* raw text's ignored lines still apply.
|
|
175
|
+
*/
|
|
176
|
+
function zeroWidthHidesFinding(text, rawFindings, rules, config, ignoredLines) {
|
|
177
|
+
return transformHidesFinding(text, stripZeroWidth, rawFindings, rules, config, ignoredLines);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Cyrillic and Greek text is common and harmless in source code: locale
|
|
181
|
+
* files, comments, UI strings, math. Look-alike letters only matter when they
|
|
182
|
+
* disguise a sensitive value (`рassword=…` with a Cyrillic `р`, an `sk-` key
|
|
183
|
+
* with a Greek `κ`). Fold confusables to Latin (zero-width characters removed
|
|
184
|
+
* too, so a combined splice is caught), rescan, and report only when the
|
|
185
|
+
* folded text yields a finding the raw text does not (issue #46).
|
|
186
|
+
*/
|
|
187
|
+
function homoglyphHidesFinding(text, rawFindings, rules, config, ignoredLines) {
|
|
188
|
+
// Text with zero-width characters but no look-alikes was already judged by
|
|
189
|
+
// zeroWidthHidesFinding; skip the identical rescan.
|
|
190
|
+
if (foldConfusables(text) === text)
|
|
191
|
+
return false;
|
|
192
|
+
const fold = (value) => foldConfusables(stripZeroWidth(value));
|
|
193
|
+
return transformHidesFinding(text, fold, rawFindings, rules, config, ignoredLines);
|
|
194
|
+
}
|
|
142
195
|
/**
|
|
143
196
|
* Internal scan function without encoding detection.
|
|
144
197
|
* Used by scanTextWithDecoding to avoid infinite recursion.
|
|
@@ -154,13 +207,13 @@ function scanTextInternal(text, config, opts) {
|
|
|
154
207
|
const ruleFindings = collectRuleFindings(truncated, allRules, config, ignoredLines);
|
|
155
208
|
// Step 3: Flag suspicious Unicode only when it is an actual threat. This
|
|
156
209
|
// doesn't modify the text, just detects issues.
|
|
157
|
-
const suspicious = detectSuspiciousUnicode(truncated);
|
|
158
210
|
const zeroWidthEvasion = zeroWidthHidesFinding(truncated, ruleFindings, allRules, config, ignoredLines);
|
|
211
|
+
const homoglyphEvasion = !zeroWidthEvasion && homoglyphHidesFinding(truncated, ruleFindings, allRules, config, ignoredLines);
|
|
159
212
|
const findings = [];
|
|
160
|
-
if (zeroWidthEvasion ||
|
|
213
|
+
if (zeroWidthEvasion || homoglyphEvasion) {
|
|
161
214
|
findings.push({
|
|
162
215
|
type: "CUSTOM",
|
|
163
|
-
severity:
|
|
216
|
+
severity: "HIGH",
|
|
164
217
|
line: 1,
|
|
165
218
|
column: 1,
|
|
166
219
|
preview: "****",
|
|
@@ -172,8 +225,8 @@ function scanTextInternal(text, config, opts) {
|
|
|
172
225
|
match_text: truncated.substring(0, 50), // Only store first 50 chars
|
|
173
226
|
unicode_detected: {
|
|
174
227
|
had_zero_width: zeroWidthEvasion,
|
|
175
|
-
had_homographs:
|
|
176
|
-
details:
|
|
228
|
+
had_homographs: homoglyphEvasion,
|
|
229
|
+
details: [zeroWidthEvasion ? "zero-width characters hide a finding" : "look-alike letters hide a finding"],
|
|
177
230
|
},
|
|
178
231
|
});
|
|
179
232
|
}
|
|
@@ -196,7 +249,7 @@ function scanTextInternal(text, config, opts) {
|
|
|
196
249
|
const warnings = [];
|
|
197
250
|
if (zeroWidthEvasion)
|
|
198
251
|
warnings.push("零寬字符");
|
|
199
|
-
if (
|
|
252
|
+
if (homoglyphEvasion)
|
|
200
253
|
warnings.push("可疑 Unicode");
|
|
201
254
|
if (wasTruncated)
|
|
202
255
|
warnings.push("內容截斷");
|
|
@@ -275,22 +328,33 @@ protectedSpans = []) {
|
|
|
275
328
|
const ignoredLines = honorDirectives(opts) ? getIgnoredLines(truncated) : new Set();
|
|
276
329
|
const matches = [];
|
|
277
330
|
const seen = new Set();
|
|
331
|
+
const globalAllow = allowSet(config.settings.allowlist);
|
|
278
332
|
for (const rule of allRules) {
|
|
279
333
|
rule.pattern.lastIndex = 0;
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
if (
|
|
334
|
+
// Keep the same progress and empty-span contract as collectRuleFindings.
|
|
335
|
+
for (const match of truncated.matchAll(rule.pattern)) {
|
|
336
|
+
if (match[0].length === 0)
|
|
283
337
|
continue;
|
|
284
|
-
|
|
338
|
+
const offset = rule.skipPrefix ?? 0;
|
|
339
|
+
const start = match.index + offset;
|
|
340
|
+
const end = match.index + match[0].length;
|
|
341
|
+
const matchedText = match[0].slice(offset);
|
|
342
|
+
if (rule.validate !== undefined && !rule.validate(matchedText, { text: truncated, index: start }))
|
|
285
343
|
continue;
|
|
286
|
-
|
|
344
|
+
if (overlapsSpan(start, end, protectedSpans))
|
|
345
|
+
continue;
|
|
346
|
+
const key = `${start}:${end - start}`;
|
|
287
347
|
if (seen.has(key))
|
|
288
348
|
continue;
|
|
289
|
-
|
|
290
|
-
const { line } = getLineCol(truncated, match.index);
|
|
349
|
+
const { line } = getLineCol(truncated, start);
|
|
291
350
|
if (ignoredLines.has(line))
|
|
292
351
|
continue;
|
|
293
|
-
|
|
352
|
+
// An allowlisted literal is left as written, exactly as the scanner leaves
|
|
353
|
+
// it unreported; redacting it would contradict the bypass the user chose.
|
|
354
|
+
if (isAllowlisted(matchedText, globalAllow, rule.allow))
|
|
355
|
+
continue;
|
|
356
|
+
seen.add(key);
|
|
357
|
+
matches.push({ index: start, length: end - start, type: rule.type });
|
|
294
358
|
}
|
|
295
359
|
}
|
|
296
360
|
const detector = config.exact_value_detector;
|
|
@@ -303,6 +367,9 @@ protectedSpans = []) {
|
|
|
303
367
|
const { line } = getLineCol(truncated, match.index);
|
|
304
368
|
if (ignoredLines.has(line))
|
|
305
369
|
continue;
|
|
370
|
+
const matchedText = truncated.slice(match.index, match.index + match.length);
|
|
371
|
+
if (isAllowlisted(matchedText, globalAllow, undefined))
|
|
372
|
+
continue;
|
|
306
373
|
matches.push({ index: match.index, length: match.length, type: "ENV_SECRET" });
|
|
307
374
|
}
|
|
308
375
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport-aware classification for email-shaped spans.
|
|
3
|
+
*
|
|
4
|
+
* A Git remote written the way SSH expects it (`user@host:path`) and the
|
|
5
|
+
* userinfo of an authenticated URL (`scheme://user:password@host`) both
|
|
6
|
+
* contain a `user@host` run that the email rule would happily report as PII.
|
|
7
|
+
* Reporting it is wrong twice over: a public host gets pseudonymized for no
|
|
8
|
+
* privacy gain, and the password half of a credential is labelled as personal
|
|
9
|
+
* data instead of a secret.
|
|
10
|
+
*
|
|
11
|
+
* The classifier reads the first scheme of the whitespace-delimited run and
|
|
12
|
+
* parses that URL's authority once. The scheme does not have to start the run,
|
|
13
|
+
* so a wrapped URL (`{"url": "https://…"}`, `url=https://…`) is recognised.
|
|
14
|
+
*
|
|
15
|
+
* Every expansion is bounded. When a boundary cannot be proven within the
|
|
16
|
+
* window the span is reported: an unproven exemption must never hide an
|
|
17
|
+
* address. The scp fallback is not attempted in that case, because it would
|
|
18
|
+
* turn a truncated run into an exemption.
|
|
19
|
+
*
|
|
20
|
+
* What it does NOT do: whitelist hosts, paths, files or commands. It only
|
|
21
|
+
* tells the email rule that this span belongs to a different construct. Every
|
|
22
|
+
* other rule still scans the same bytes.
|
|
23
|
+
*/
|
|
24
|
+
const SSH_SCHEMES = new Set(["ssh", "ssh+git", "git+ssh"]);
|
|
25
|
+
/** A run ends at whitespace; an authority cannot contain it. */
|
|
26
|
+
const RUN_BREAK = /\s/;
|
|
27
|
+
/** Characters that close an SSH host, but may appear inside a password. */
|
|
28
|
+
const FRAMING_BREAK = /["'`<>()[\]{}]/;
|
|
29
|
+
/** An SSH host is letters, digits, dot, dash, underscore, with an optional port. */
|
|
30
|
+
const SSH_HOST = /^[A-Za-z0-9._-]+(?::\d{1,5})?/;
|
|
31
|
+
/** An SSH user name; anything else means the `@` belongs to following prose. */
|
|
32
|
+
const SSH_USER = /^[A-Za-z0-9._-]+$/;
|
|
33
|
+
/** First character of an scp path. Excludes whitespace, quotes and brackets. */
|
|
34
|
+
const PATH_START = /[A-Za-z0-9._~\-/]/;
|
|
35
|
+
/** `host:2222` is a port, not an scp path; the lookahead covers `)` or EOL. */
|
|
36
|
+
const PORT_AFTER_COLON = /^\d{1,5}(?![A-Za-z0-9])/;
|
|
37
|
+
/**
|
|
38
|
+
* A scheme name must not be preceded by another scheme character, otherwise
|
|
39
|
+
* `123ssh://…` would be read as `ssh`.
|
|
40
|
+
*/
|
|
41
|
+
const SCHEME_BEFORE = /(?:^|[^A-Za-z0-9+.-])([A-Za-z][A-Za-z0-9+.-]*)$/;
|
|
42
|
+
/**
|
|
43
|
+
* Bound on every expansion. A scheme, authority or token longer than this is
|
|
44
|
+
* not a real remote; when the bound is hit without proof, report the span.
|
|
45
|
+
*/
|
|
46
|
+
const WINDOW = 512;
|
|
47
|
+
/**
|
|
48
|
+
* Start of the surrounding run, or null when its edge is farther than the
|
|
49
|
+
* window. A truncated run cannot prove which URL encloses the candidate.
|
|
50
|
+
*/
|
|
51
|
+
function runStart(text, index) {
|
|
52
|
+
const floor = Math.max(0, index - WINDOW);
|
|
53
|
+
let start = index;
|
|
54
|
+
while (start > floor && !RUN_BREAK.test(text[start - 1]))
|
|
55
|
+
start -= 1;
|
|
56
|
+
if (start === 0)
|
|
57
|
+
return 0;
|
|
58
|
+
return RUN_BREAK.test(text[start - 1]) ? start : null;
|
|
59
|
+
}
|
|
60
|
+
/** Token start, or -1 when the token edge is farther than the window. */
|
|
61
|
+
function tokenStart(text, index) {
|
|
62
|
+
const floor = Math.max(0, index - WINDOW);
|
|
63
|
+
let start = index;
|
|
64
|
+
while (start > floor && !FRAMING_BREAK.test(text[start - 1]) && !RUN_BREAK.test(text[start - 1])) {
|
|
65
|
+
start -= 1;
|
|
66
|
+
}
|
|
67
|
+
if (start === floor && floor > 0) {
|
|
68
|
+
const previous = text[start - 1];
|
|
69
|
+
if (!FRAMING_BREAK.test(previous) && !RUN_BREAK.test(previous))
|
|
70
|
+
return -1;
|
|
71
|
+
}
|
|
72
|
+
return start;
|
|
73
|
+
}
|
|
74
|
+
/** End of the authority: `/`, `?`, `#`, whitespace, or the window bound. */
|
|
75
|
+
function authorityLimit(text, authorityStart) {
|
|
76
|
+
const cap = Math.min(text.length, authorityStart + WINDOW);
|
|
77
|
+
for (let at = authorityStart; at < cap; at += 1) {
|
|
78
|
+
const char = text[at];
|
|
79
|
+
if (char === "/" || char === "?" || char === "#" || RUN_BREAK.test(char))
|
|
80
|
+
return at;
|
|
81
|
+
}
|
|
82
|
+
return cap;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Classify the span `text.slice(index, index + length)`, or return null when it
|
|
86
|
+
* is an ordinary address that the email rule should keep reporting.
|
|
87
|
+
*/
|
|
88
|
+
export function classifyTransportSpan(text, index, length) {
|
|
89
|
+
const start = runStart(text, index);
|
|
90
|
+
// Unproven run boundary: report. Falling through to scp here would exempt an
|
|
91
|
+
// address inside a URL whose start we could not see.
|
|
92
|
+
if (start === null)
|
|
93
|
+
return null;
|
|
94
|
+
// The first scheme in the run owns it. A later `://` inside the path
|
|
95
|
+
// (`https://a.example/(ssh://b/…)`) does not start a new remote.
|
|
96
|
+
const marker = text.indexOf("://", start);
|
|
97
|
+
if (marker >= 0 && marker < index) {
|
|
98
|
+
const name = SCHEME_BEFORE.exec(text.slice(Math.max(start, marker - 32), marker));
|
|
99
|
+
if (name !== null) {
|
|
100
|
+
const scheme = name[1].toLowerCase();
|
|
101
|
+
const authorityStart = marker + 3;
|
|
102
|
+
const limit = authorityLimit(text, authorityStart);
|
|
103
|
+
if (index < authorityStart || index >= limit)
|
|
104
|
+
return null;
|
|
105
|
+
const firstAt = text.indexOf("@", authorityStart);
|
|
106
|
+
if (firstAt < 0 || firstAt >= limit)
|
|
107
|
+
return null;
|
|
108
|
+
// `user:password@` — punctuation is legal inside the password, so the
|
|
109
|
+
// userinfo closes at the last `@` before the authority ends.
|
|
110
|
+
const userinfo = text.slice(authorityStart, firstAt);
|
|
111
|
+
if (userinfo.includes(":"))
|
|
112
|
+
return "url-credential";
|
|
113
|
+
if (!SSH_SCHEMES.has(scheme))
|
|
114
|
+
return null;
|
|
115
|
+
// A host-only URL has no userinfo; the first `@` then belongs to the
|
|
116
|
+
// prose after it. Only a plain SSH user name is accepted, so a separator
|
|
117
|
+
// (em dash, quote, comma) leaves the following address reported.
|
|
118
|
+
if (!SSH_USER.test(userinfo))
|
|
119
|
+
return null;
|
|
120
|
+
// The exemption covers the host only. A whitelist of host characters is
|
|
121
|
+
// deliberate: any other character ends the host.
|
|
122
|
+
const host = SSH_HOST.exec(text.slice(firstAt + 1, limit));
|
|
123
|
+
if (host === null)
|
|
124
|
+
return null;
|
|
125
|
+
return index < firstAt + 1 + host[0].length ? "ssh-authority" : null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// scp-like form. The address must open its token: `foo=user@host:path` is
|
|
129
|
+
// left alone rather than guessed.
|
|
130
|
+
const tokenEdge = tokenStart(text, index);
|
|
131
|
+
if (tokenEdge !== index)
|
|
132
|
+
return null;
|
|
133
|
+
if (text[index + length] !== ":")
|
|
134
|
+
return null;
|
|
135
|
+
const after = text.slice(index + length + 1, index + length + 8);
|
|
136
|
+
if (PORT_AFTER_COLON.test(after))
|
|
137
|
+
return null;
|
|
138
|
+
const first = after[0];
|
|
139
|
+
return first !== undefined && PATH_START.test(first) ? "ssh-authority" : null;
|
|
140
|
+
}
|
package/dist/scanner/unicode.js
CHANGED
|
@@ -155,3 +155,33 @@ export function sanitizeForScanning(text) {
|
|
|
155
155
|
},
|
|
156
156
|
};
|
|
157
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Cyrillic and Greek letters that render like a Latin letter in common fonts,
|
|
160
|
+
* mapped to that Latin letter. One UTF-16 unit in, one out, so offsets and
|
|
161
|
+
* line numbers of the folded text match the original.
|
|
162
|
+
*/
|
|
163
|
+
const CONFUSABLE_TO_LATIN = {
|
|
164
|
+
// Cyrillic lowercase
|
|
165
|
+
"а": "a", "е": "e", "о": "o", "р": "p", "с": "c",
|
|
166
|
+
"х": "x", "у": "y", "і": "i", "ј": "j", "ѕ": "s",
|
|
167
|
+
"ԁ": "d", "һ": "h", "ӏ": "l", "ԛ": "q", "ԝ": "w",
|
|
168
|
+
// Cyrillic uppercase
|
|
169
|
+
"А": "A", "В": "B", "Е": "E", "К": "K", "М": "M",
|
|
170
|
+
"Н": "H", "О": "O", "Р": "P", "С": "C", "Т": "T",
|
|
171
|
+
"Х": "X", "У": "Y", "І": "I", "Ј": "J", "Ѕ": "S",
|
|
172
|
+
// Greek lowercase
|
|
173
|
+
"α": "a", "ο": "o", "ρ": "p", "ν": "v", "ι": "i",
|
|
174
|
+
"κ": "k", "υ": "u", "χ": "x",
|
|
175
|
+
// Greek uppercase
|
|
176
|
+
"Α": "A", "Β": "B", "Ε": "E", "Ζ": "Z", "Η": "H",
|
|
177
|
+
"Ι": "I", "Κ": "K", "Μ": "M", "Ν": "N", "Ο": "O",
|
|
178
|
+
"Ρ": "P", "Τ": "T", "Υ": "Y", "Χ": "X",
|
|
179
|
+
};
|
|
180
|
+
const CONFUSABLE_PATTERN = new RegExp(`[${Object.keys(CONFUSABLE_TO_LATIN).join("")}]`, "g");
|
|
181
|
+
/**
|
|
182
|
+
* Replaces Cyrillic/Greek look-alike letters with their Latin counterparts.
|
|
183
|
+
* Length-preserving; returns the input unchanged when it has none.
|
|
184
|
+
*/
|
|
185
|
+
export function foldConfusables(text) {
|
|
186
|
+
return text.replace(CONFUSABLE_PATTERN, (char) => CONFUSABLE_TO_LATIN[char] ?? char);
|
|
187
|
+
}
|