markdown-parity-check 0.2.38 → 0.2.40
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 +18 -7
- package/SECURITY.md +3 -3
- package/dist/src/align.js +10 -1
- package/dist/src/cli.d.ts +15 -1
- package/dist/src/cli.js +76 -7
- package/dist/src/compare.js +22 -11
- package/dist/src/html.js +35 -11
- package/dist/src/markdown.js +44 -12
- package/dist/src/model.d.ts +8 -0
- package/dist/src/netguard.d.ts +7 -1
- package/dist/src/netguard.js +11 -2
- package/dist/src/normalize.d.ts +107 -5
- package/dist/src/normalize.js +526 -14
- package/dist/src/report.js +13 -3
- package/dist/src/run.js +3 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +1 -1
- package/package.json +1 -1
package/dist/src/normalize.js
CHANGED
|
@@ -61,11 +61,18 @@ export function textKey(text) {
|
|
|
61
61
|
export function codeWhitespaceKey(code) {
|
|
62
62
|
return code.replace(/[\p{Zs}\t\r\n\f\v]+/gu, ' ').replace(/^ | $/g, '');
|
|
63
63
|
}
|
|
64
|
-
/** Code content: CRLF and CR line endings become LF and every trailing newline is removed; other whitespace is kept.
|
|
64
|
+
/** Code content: CRLF and CR line endings become LF and every trailing newline is removed; other whitespace is kept.
|
|
65
|
+
* The trailing newlines are counted off from the end. The expression /\n+$/ that did this up to 0.2.38 starts a new
|
|
66
|
+
* scan at every newline of a long run and takes time quadratic in its length. */
|
|
65
67
|
export function normalizeCode(code) {
|
|
66
|
-
|
|
68
|
+
const text = code.replace(/\r\n?/g, '\n');
|
|
69
|
+
let end = text.length;
|
|
70
|
+
while (end > 0 && text.charCodeAt(end - 1) === 0x0a)
|
|
71
|
+
end--;
|
|
72
|
+
return end === text.length ? text : text.slice(0, end);
|
|
67
73
|
}
|
|
68
|
-
// Terminal control characters (ANSI escape sequences, C0 and C1 controls)
|
|
74
|
+
// Terminal control characters (ANSI escape sequences, C0 and C1 controls), the Unicode bidi controls, the line and
|
|
75
|
+
// paragraph separators and the tag characters reach a text report unescaped if a
|
|
69
76
|
// compared page or an argument carries one. --format json is safe only in part on its own: JSON.stringify escapes
|
|
70
77
|
// the C0 controls (U+0000 to U+001F) and lone surrogates, and leaves U+007F, the C1 controls and the bidi controls
|
|
71
78
|
// literal. It is safe in practice because renderJson sends every string value through redactText first, so this
|
|
@@ -74,7 +81,11 @@ export function normalizeCode(code) {
|
|
|
74
81
|
// C1 controls (U+0080 to U+009F) include the single-byte CSI (U+009B), which some terminals read as an escape. The bidi
|
|
75
82
|
// controls (U+202A to U+202E and U+2066 to U+2069) reorder the text a reader sees, so a quoted excerpt could read
|
|
76
83
|
// differently in a report than in the page. Both ranges match the hosted validator's stripControls(stripBidi()).
|
|
77
|
-
|
|
84
|
+
// Since 0.2.39 the left-to-right and right-to-left marks and the Arabic letter mark (U+200E, U+200F, U+061C) go too,
|
|
85
|
+
// as the comment above said the bidi controls did, and so do U+2028 and U+2029, which some viewers read as a line
|
|
86
|
+
// break, and the tag characters (U+E0000 to U+E007F), which no reader sees and a program reading the report can. The
|
|
87
|
+
// zero width joiner and non-joiner and the variation selectors stay: emoji and several scripts need them.
|
|
88
|
+
const CONTROL_CHARS = /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f\u061c\u200e\u200f\u2028\u2029\u202A-\u202E\u2066-\u2069\u{e0000}-\u{e007f}]/gu;
|
|
78
89
|
/** Removes the escape character (which starts an ANSI escape sequence), the other C0 and C1 controls and the Unicode
|
|
79
90
|
* bidi controls from a value that may come from a compared page or a raw argument, so a plain --format text report
|
|
80
91
|
* cannot replay them at the terminal (0.2.16, found by an outside review 2026-09-28). The printable rest of an escape
|
|
@@ -98,7 +109,7 @@ export function extractNumbers(text) {
|
|
|
98
109
|
}
|
|
99
110
|
/** Report excerpt: URLs inside the text are masked BEFORE truncation, so a cut query value cannot leak. */
|
|
100
111
|
export function excerpt(text, max = 80) {
|
|
101
|
-
const t = redactText(strictNormalize(text));
|
|
112
|
+
const t = redactText(strictNormalize(dropAuthorityControls(text)));
|
|
102
113
|
return t.length <= max ? t : t.slice(0, max - 1) + '\u2026';
|
|
103
114
|
}
|
|
104
115
|
// URL-like runs inside free text: absolute URLs, www. hosts, and root-relative paths that carry a query
|
|
@@ -106,7 +117,7 @@ export function excerpt(text, max = 80) {
|
|
|
106
117
|
//
|
|
107
118
|
// Up to 0.2.1 this was one regular expression, kept in test/linear.test.ts as the reference. Its time was
|
|
108
119
|
// quadratic in the length of a run without whitespace (CodeQL js/polynomial-redos; 80 000 characters took
|
|
109
|
-
//
|
|
120
|
+
// seconds), and the text comes from the compared page. Every alternative of that expression ends where the
|
|
110
121
|
// run of characters other than whitespace and < > " ' ( ) ends, so a run holds at most one match, and the
|
|
111
122
|
// match starts at the leftmost position where any alternative can start. urlStart finds that position
|
|
112
123
|
// with a few linear scans of the run, and the test compares the two on random input.
|
|
@@ -161,6 +172,17 @@ function urlStart(t, s, e) {
|
|
|
161
172
|
break;
|
|
162
173
|
}
|
|
163
174
|
}
|
|
175
|
+
// (?<![\w.-])(?:localhost|(?:\d{1,3}\.){3}\d{1,3}|(?:[\w-]+\.)+(?:[a-z]{2,}|xn--[a-z0-9-]+))(?::\d+)?(?:[\/\\]|#(?=...=))
|
|
176
|
+
// A bare host name followed by a path (0.2.39): dotted labels that end in a letters-only label of two or more
|
|
177
|
+
// characters or in an xn-- label, a dotted IPv4 address or localhost, then an optional port, then a slash or a
|
|
178
|
+
// backslash, or a fragment with an equals sign somewhere after it. The host is not glued to a word, dot or hyphen
|
|
179
|
+
// before it. A query value or fragment of such an address, example.com/cb#access_token=..., reached a report
|
|
180
|
+
// unmasked; rewriteBareHost hides a query or a fragment of such an address only when it holds an equals sign. A
|
|
181
|
+
// file name, a version number and an abbreviation have no such ending, and a fragment without an equals
|
|
182
|
+
// sign after a host that has no slash, as in README.md#section, is prose.
|
|
183
|
+
const host = bareHostStart(t, s, e);
|
|
184
|
+
if (host >= 0 && (best < 0 || host < best))
|
|
185
|
+
best = host;
|
|
164
186
|
// (?<![\w/])\/[^\s<>"'()]*[?#][^\s<>"'()]* The first '/' that does not follow a word character or '/',
|
|
165
187
|
// when a '?' or '#' comes after it in the run.
|
|
166
188
|
let lastQueryOrHash = -1;
|
|
@@ -232,6 +254,91 @@ function urlStart(t, s, e) {
|
|
|
232
254
|
}
|
|
233
255
|
return best;
|
|
234
256
|
}
|
|
257
|
+
/** Start of the first bare host name with a path in the run t[s, e), or -1. A chain of labels is read once from its
|
|
258
|
+
* first character, so the scan is linear: the position of the last equals sign of the run is found once, so a fragment
|
|
259
|
+
* is not rescanned for each chain before it. */
|
|
260
|
+
function bareHostStart(t, s, e) {
|
|
261
|
+
const isLabelChar = (c) => isLetter(c) || isDigit(c) || c === 0x2d || c === 0x5f;
|
|
262
|
+
let lastEquals = e - 1;
|
|
263
|
+
while (lastEquals >= s && t.charCodeAt(lastEquals) !== 0x3d)
|
|
264
|
+
lastEquals--;
|
|
265
|
+
let i = s;
|
|
266
|
+
while (i < e) {
|
|
267
|
+
const before = i > 0 ? t.charCodeAt(i - 1) : -1;
|
|
268
|
+
const c = t.charCodeAt(i);
|
|
269
|
+
if (!isLabelChar(c) || (i > s && (isWord(before) || before === 0x2e || before === 0x2d))) {
|
|
270
|
+
i++;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
// One chain of labels starting at i.
|
|
274
|
+
let j = i;
|
|
275
|
+
let labels = 0;
|
|
276
|
+
let lastStart = i;
|
|
277
|
+
for (;;) {
|
|
278
|
+
lastStart = j;
|
|
279
|
+
while (j < e && isLabelChar(t.charCodeAt(j)))
|
|
280
|
+
j++;
|
|
281
|
+
labels++;
|
|
282
|
+
if (j + 1 < e && t.charCodeAt(j) === 0x2e && isLabelChar(t.charCodeAt(j + 1)))
|
|
283
|
+
j++;
|
|
284
|
+
else
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
let tld = j - lastStart >= 2;
|
|
288
|
+
for (let k = lastStart; tld && k < j; k++)
|
|
289
|
+
if (!isLetter(t.charCodeAt(k)))
|
|
290
|
+
tld = false;
|
|
291
|
+
if (!tld && j - lastStart >= 5) {
|
|
292
|
+
// A punycode label, xn-- and then letters, digits and hyphens.
|
|
293
|
+
const fold = (c) => (c >= 0x41 && c <= 0x5a ? c + 0x20 : c);
|
|
294
|
+
tld = fold(t.charCodeAt(lastStart)) === 0x78 && fold(t.charCodeAt(lastStart + 1)) === 0x6e && t.charCodeAt(lastStart + 2) === 0x2d && t.charCodeAt(lastStart + 3) === 0x2d;
|
|
295
|
+
for (let k = lastStart + 4; tld && k < j; k++)
|
|
296
|
+
if (t.charCodeAt(k) === 0x5f)
|
|
297
|
+
tld = false;
|
|
298
|
+
}
|
|
299
|
+
let host = labels >= 2 && tld;
|
|
300
|
+
if (!host && labels === 4) {
|
|
301
|
+
// A dotted IPv4 address: four labels of one to three digits.
|
|
302
|
+
host = true;
|
|
303
|
+
for (let k = i, len = 0; host && k <= j; k++) {
|
|
304
|
+
if (k === j || t.charCodeAt(k) === 0x2e) {
|
|
305
|
+
if (len < 1 || len > 3)
|
|
306
|
+
host = false;
|
|
307
|
+
len = 0;
|
|
308
|
+
}
|
|
309
|
+
else if (isDigit(t.charCodeAt(k)))
|
|
310
|
+
len++;
|
|
311
|
+
else
|
|
312
|
+
host = false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (!host && labels === 1 && j - i === 9) {
|
|
316
|
+
// localhost, in any case.
|
|
317
|
+
host = true;
|
|
318
|
+
for (let k = 0; host && k < 9; k++) {
|
|
319
|
+
const c = t.charCodeAt(i + k);
|
|
320
|
+
const f = c === 0x17f ? 0x73 : c >= 0x41 && c <= 0x5a ? c + 0x20 : c;
|
|
321
|
+
host = f === 'localhost'.charCodeAt(k);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
let k = j;
|
|
325
|
+
if (k < e && t.charCodeAt(k) === 0x3a) {
|
|
326
|
+
k++;
|
|
327
|
+
const digits = k;
|
|
328
|
+
while (k < e && isDigit(t.charCodeAt(k)))
|
|
329
|
+
k++;
|
|
330
|
+
if (k === digits)
|
|
331
|
+
k = j;
|
|
332
|
+
}
|
|
333
|
+
if (host && k < e) {
|
|
334
|
+
const c = t.charCodeAt(k);
|
|
335
|
+
if (c === 0x2f || c === 0x5c || (c === 0x23 && lastEquals > k))
|
|
336
|
+
return i;
|
|
337
|
+
}
|
|
338
|
+
i = j > i ? j : i + 1;
|
|
339
|
+
}
|
|
340
|
+
return -1;
|
|
341
|
+
}
|
|
235
342
|
const SPECIAL_SCHEME_NAMES = ['https', 'http', 'ftp', 'wss', 'ws', 'file'];
|
|
236
343
|
/** Length of the special scheme name at t[p] when a colon follows it, else -1. Letters compare with the old
|
|
237
344
|
* expression's i and u flags, under which U+017F matches s. */
|
|
@@ -286,6 +393,29 @@ function tokenEnd(text, e) {
|
|
|
286
393
|
const m = TOKEN_TAIL.exec(text);
|
|
287
394
|
return e + (m ? m[0].length : 0);
|
|
288
395
|
}
|
|
396
|
+
/** Whether the URL-like match at p in the run t[s, e) is a bare host with a path: a match that starts at a host name
|
|
397
|
+
* and not at a scheme, "www.", slashes or a path. */
|
|
398
|
+
function isBareHostMatch(t, s, e, p) {
|
|
399
|
+
if (bareHostStart(t, s, e) !== p)
|
|
400
|
+
return false;
|
|
401
|
+
const w = (c) => (c === 0x57 || c === 0x77 ? 1 : 0);
|
|
402
|
+
return !(p + 3 < e && w(t.charCodeAt(p)) && w(t.charCodeAt(p + 1)) && w(t.charCodeAt(p + 2)) && t.charCodeAt(p + 3) === 0x2e);
|
|
403
|
+
}
|
|
404
|
+
/** The rewrite of a bare host with a path, such as example.com/docs/guide#step-3 (0.2.39). Its query and its fragment
|
|
405
|
+
* are hidden only when they carry a value, an equals sign, as in example.com/cb#access_token=... and
|
|
406
|
+
* example.com/cb?t=...: an anchor, a line number and a query without a value are shown as they are, because a page
|
|
407
|
+
* that differs in one of them must show the difference. The path is never hidden. */
|
|
408
|
+
function rewriteBareHost(url, rewrite) {
|
|
409
|
+
const hashAt = url.indexOf('#');
|
|
410
|
+
const fragment = hashAt < 0 ? '' : url.slice(hashAt);
|
|
411
|
+
const head = hashAt < 0 ? url : url.slice(0, hashAt);
|
|
412
|
+
const q = head.indexOf('?');
|
|
413
|
+
const queryHasValue = q >= 0 && head.indexOf('=', q) >= 0;
|
|
414
|
+
const hideFragment = fragment.includes('=');
|
|
415
|
+
if (!queryHasValue && !hideFragment)
|
|
416
|
+
return url;
|
|
417
|
+
return (queryHasValue ? rewrite(head) : head) + (hideFragment ? '' : fragment);
|
|
418
|
+
}
|
|
289
419
|
/** Rewrites the URL-like part of every run in a piece of text and leaves the rest untouched. */
|
|
290
420
|
function rewriteUrlRuns(text, rewrite) {
|
|
291
421
|
let out = '';
|
|
@@ -299,7 +429,8 @@ function rewriteUrlRuns(text, rewrite) {
|
|
|
299
429
|
continue;
|
|
300
430
|
const t = tokenEnd(text, e);
|
|
301
431
|
const end = urlEnd(text, p, t);
|
|
302
|
-
|
|
432
|
+
const url = text.slice(p, end);
|
|
433
|
+
out += text.slice(done, p) + (isBareHostMatch(text, s, e, p) ? rewriteBareHost(url, rewrite) : rewrite(url)) + text.slice(end, t);
|
|
303
434
|
done = t;
|
|
304
435
|
runs.lastIndex = t;
|
|
305
436
|
}
|
|
@@ -453,12 +584,375 @@ export function insideHiddenUrlPart(text, at) {
|
|
|
453
584
|
}
|
|
454
585
|
return false;
|
|
455
586
|
}
|
|
587
|
+
const isSpaceCode = (c) => (c < 0x80 ? c === 0x20 || (c >= 0x09 && c <= 0x0d) : /\s/u.test(String.fromCharCode(c)));
|
|
588
|
+
/**
|
|
589
|
+
* Masks the user information of every "scheme://authority" in a piece of text, wherever the scheme sits and
|
|
590
|
+
* whatever the user information starts with (0.2.39, found by an outside review 2026-11-14). The run-based masking
|
|
591
|
+
* above reads the text in runs that end at white space and at the characters < > " ' ( ), so a URL whose user name
|
|
592
|
+
* opens with one of them, such as https://"admin":secret@host/ or https://(token)@host/, was cut in two runs, and a
|
|
593
|
+
* URL that follows "=", "," or ":" in the same run, such as redirect_uri=https://u:secret@host/cb?state=1, was read as
|
|
594
|
+
* one run that starts at its first character, where the parser finds no scheme and so no user information. Here the
|
|
595
|
+
* authority is the text after "://" up to the next slash, backslash, question mark, number sign or white space, as
|
|
596
|
+
* the URL parser reads it, and its user information ends at the last "@" in it. The scheme is the run of scheme
|
|
597
|
+
* characters before "://" that holds a letter. The user information becomes ***, so the host, path and query reach
|
|
598
|
+
* the run-based masking as before. One pass: an authority ends at a slash, so no two of them overlap.
|
|
599
|
+
*
|
|
600
|
+
* With whiteSpaceInside, a tab, a line feed or a carriage return inside the authority does not end it, as the URL
|
|
601
|
+
* parser removes them, and nor does any other white space when the text up to the last "@" holds a colon that is not
|
|
602
|
+
* followed by white space ("https://example.com: see me@x.org" is prose), so
|
|
603
|
+
* https://us er:secret@host/ and https://user:pa ss@host/ are masked whole. Without a colon the white space ends the
|
|
604
|
+
* authority, so "visit https://example.com for me@x.org" is prose. The authority does not go on past white
|
|
605
|
+
* space either when the text between its last colon and that white space is a port, one to five digits, or the end
|
|
606
|
+
* of a bracketed host, so "Use https://api.example.com:8443 or write to ops@example.org" keeps the address; a
|
|
607
|
+
* password of digits only that is followed by white space is therefore not masked. Only the report text uses it: the
|
|
608
|
+
* numbers of the comparison are read through hideUrlSecrets, which does not.
|
|
609
|
+
*/
|
|
610
|
+
export function maskAuthorityUserinfo(text, whiteSpaceInside = false) {
|
|
611
|
+
const spans = findUserinfoSpans(text, whiteSpaceInside);
|
|
612
|
+
if (spans.length === 0)
|
|
613
|
+
return text;
|
|
614
|
+
let out = '';
|
|
615
|
+
let pos = 0;
|
|
616
|
+
for (const s of spans) {
|
|
617
|
+
out += text.slice(pos, s.from) + '***@';
|
|
618
|
+
pos = s.at + 1;
|
|
619
|
+
}
|
|
620
|
+
return out + text.slice(pos);
|
|
621
|
+
}
|
|
622
|
+
/** One to five ASCII digits in text[from, to): a port. */
|
|
623
|
+
function isPortText(text, from, to) {
|
|
624
|
+
if (to - from < 1 || to - from > 5)
|
|
625
|
+
return false;
|
|
626
|
+
for (let k = from; k < to; k++)
|
|
627
|
+
if (!isDigit(text.charCodeAt(k)))
|
|
628
|
+
return false;
|
|
629
|
+
return true;
|
|
630
|
+
}
|
|
631
|
+
/** The user information of every "scheme://authority" of a text, in order and without overlap. The rules are the
|
|
632
|
+
* ones maskAuthorityUserinfo describes. */
|
|
633
|
+
export function findUserinfoSpans(text, whiteSpaceInside = false) {
|
|
634
|
+
const spans = [];
|
|
635
|
+
let pos = 0;
|
|
636
|
+
let from = 0;
|
|
637
|
+
for (;;) {
|
|
638
|
+
const colon = text.indexOf('://', from);
|
|
639
|
+
if (colon < 0)
|
|
640
|
+
break;
|
|
641
|
+
from = colon + 3;
|
|
642
|
+
let hasLetter = false;
|
|
643
|
+
let start = colon;
|
|
644
|
+
for (let k = colon; k > pos && isSchemeChar(text.charCodeAt(k - 1)); k--) {
|
|
645
|
+
start = k - 1;
|
|
646
|
+
if (isLetter(text.charCodeAt(k - 1)))
|
|
647
|
+
hasLetter = true;
|
|
648
|
+
}
|
|
649
|
+
if (!hasLetter)
|
|
650
|
+
continue;
|
|
651
|
+
let at = -1;
|
|
652
|
+
let lastAt = -1;
|
|
653
|
+
let firstSpace = -1;
|
|
654
|
+
let firstColon = -1;
|
|
655
|
+
let lastColon = -1;
|
|
656
|
+
let seenSpace = false;
|
|
657
|
+
for (let e = from; e < text.length; e++) {
|
|
658
|
+
const c = text.charCodeAt(e);
|
|
659
|
+
if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23)
|
|
660
|
+
break;
|
|
661
|
+
if (whiteSpaceInside && !seenSpace && isSpaceCode(c)) {
|
|
662
|
+
seenSpace = true;
|
|
663
|
+
// A port or the end of a bracketed host before the first white space: what follows is text, not user information.
|
|
664
|
+
if ((lastColon >= 0 && isPortText(text, lastColon + 1, e)) || text.charCodeAt(e - 1) === 0x5d)
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
if (isSpaceCode(c) && !(whiteSpaceInside && (c === 0x09 || c === 0x0a || c === 0x0d))) {
|
|
668
|
+
if (!whiteSpaceInside)
|
|
669
|
+
break;
|
|
670
|
+
if (firstSpace < 0)
|
|
671
|
+
firstSpace = e;
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
if (c === 0x3a) {
|
|
675
|
+
lastColon = e;
|
|
676
|
+
if (firstColon < 0 && !isSpaceCode(text.charCodeAt(e + 1)))
|
|
677
|
+
firstColon = e;
|
|
678
|
+
}
|
|
679
|
+
if (c === 0x40) {
|
|
680
|
+
lastAt = e;
|
|
681
|
+
if (firstSpace < 0)
|
|
682
|
+
at = e;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
if (lastAt > firstSpace && firstSpace >= 0 && firstColon >= 0 && firstColon < lastAt)
|
|
686
|
+
at = lastAt;
|
|
687
|
+
if (at < 0)
|
|
688
|
+
continue;
|
|
689
|
+
spans.push({ start, from: colon + 3, at });
|
|
690
|
+
pos = at + 1;
|
|
691
|
+
from = pos;
|
|
692
|
+
}
|
|
693
|
+
return spans;
|
|
694
|
+
}
|
|
695
|
+
/** The text as a report shows it, for a raw text whose white space the credential masking needs: undefined when
|
|
696
|
+
* dropAuthorityControls changes nothing, else the normalized text without the white space inside user information.
|
|
697
|
+
* It is for display only. The comparison keys come from strictNormalize alone, as in 0.2.38, because removing the
|
|
698
|
+
* white space there made two texts that differ only in the kind of white space compare unequal, and the reverse. */
|
|
699
|
+
export function shownNormalize(raw) {
|
|
700
|
+
const dropped = dropAuthorityControls(raw);
|
|
701
|
+
return dropped === raw ? undefined : strictNormalize(dropped);
|
|
702
|
+
}
|
|
703
|
+
/** shownNormalize as an object to spread into a block or a link: {} or { shown }. */
|
|
704
|
+
export function shownField(raw) {
|
|
705
|
+
const s = shownNormalize(raw);
|
|
706
|
+
return s === undefined ? {} : { shown: s };
|
|
707
|
+
}
|
|
708
|
+
const CONTROL_WHITE_SPACE = /[\t\n\v\f\r]/;
|
|
709
|
+
/**
|
|
710
|
+
* Removes a tab, a vertical tab, a form feed, a line feed or a carriage return from the user information of a
|
|
711
|
+
* "scheme://authority" when it is a credential, in a text that is only displayed, before the white space is
|
|
712
|
+
* collapsed into a plain space. It is never part of a comparison key. The URL
|
|
713
|
+
* parser drops a tab, a line feed and a carriage return from an address, and none of the five can stand in a user
|
|
714
|
+
* name or a password a reader sees, but a plain space after the colon of "https://user: secret@host" makes the
|
|
715
|
+
* masking read prose, so the password reached the report. Only an authority that ends at the first white space other
|
|
716
|
+
* than these five is read, and only one that holds a colon in front of the last "@", which is not followed by a plain
|
|
717
|
+
* space and does not end a port: "https://example.com:8443" and a line break and then an address is text. Without
|
|
718
|
+
* a colon only a tab, a vertical tab and a form feed count, because a line break between an address and an e-mail
|
|
719
|
+
* address is ordinary text.
|
|
720
|
+
*/
|
|
721
|
+
export function dropAuthorityControls(text) {
|
|
722
|
+
if (!CONTROL_WHITE_SPACE.test(text) || !text.includes('://'))
|
|
723
|
+
return text;
|
|
724
|
+
let out = '';
|
|
725
|
+
let pos = 0;
|
|
726
|
+
let from = 0;
|
|
727
|
+
for (;;) {
|
|
728
|
+
const colon = text.indexOf('://', from);
|
|
729
|
+
if (colon < 0)
|
|
730
|
+
break;
|
|
731
|
+
from = colon + 3;
|
|
732
|
+
let hasLetter = false;
|
|
733
|
+
for (let k = colon; k > pos && isSchemeChar(text.charCodeAt(k - 1)); k--) {
|
|
734
|
+
if (isLetter(text.charCodeAt(k - 1)))
|
|
735
|
+
hasLetter = true;
|
|
736
|
+
}
|
|
737
|
+
if (!hasLetter)
|
|
738
|
+
continue;
|
|
739
|
+
let lastAt = -1;
|
|
740
|
+
for (let e = from; e < text.length; e++) {
|
|
741
|
+
const c = text.charCodeAt(e);
|
|
742
|
+
if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23)
|
|
743
|
+
break;
|
|
744
|
+
if (isSpaceCode(c) && !isControlWhiteSpace(c))
|
|
745
|
+
break;
|
|
746
|
+
if (c === 0x40)
|
|
747
|
+
lastAt = e;
|
|
748
|
+
}
|
|
749
|
+
if (lastAt < 0)
|
|
750
|
+
continue;
|
|
751
|
+
let firstControl = -1;
|
|
752
|
+
let lastColon = -1;
|
|
753
|
+
let lineBreak = false;
|
|
754
|
+
let colonRule = false;
|
|
755
|
+
for (let e = from; e < lastAt; e++) {
|
|
756
|
+
const c = text.charCodeAt(e);
|
|
757
|
+
if (isControlWhiteSpace(c)) {
|
|
758
|
+
if (firstControl < 0)
|
|
759
|
+
firstControl = e;
|
|
760
|
+
if (c === 0x0a || c === 0x0d)
|
|
761
|
+
lineBreak = true;
|
|
762
|
+
}
|
|
763
|
+
else if (c === 0x3a) {
|
|
764
|
+
if (firstControl < 0)
|
|
765
|
+
lastColon = e;
|
|
766
|
+
const next = text.charCodeAt(e + 1);
|
|
767
|
+
if (!isSpaceCode(next) || isControlWhiteSpace(next))
|
|
768
|
+
colonRule = true;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
if (firstControl < 0)
|
|
772
|
+
continue;
|
|
773
|
+
if (lastColon >= 0 && isPortText(text, lastColon + 1, firstControl))
|
|
774
|
+
continue;
|
|
775
|
+
if (firstControl > from && text.charCodeAt(firstControl - 1) === 0x5d)
|
|
776
|
+
continue;
|
|
777
|
+
if (!colonRule && lineBreak)
|
|
778
|
+
continue;
|
|
779
|
+
out += text.slice(pos, from) + text.slice(from, lastAt).replace(/[\t\n\v\f\r]/g, '');
|
|
780
|
+
pos = lastAt;
|
|
781
|
+
from = lastAt + 1;
|
|
782
|
+
}
|
|
783
|
+
return pos === 0 ? text : out + text.slice(pos);
|
|
784
|
+
}
|
|
785
|
+
const isControlWhiteSpace = (c) => c === 0x09 || c === 0x0a || c === 0x0b || c === 0x0c || c === 0x0d;
|
|
786
|
+
/** The user information of a source text, computed once for the positions of its links. The characters that
|
|
787
|
+
* stripControlChars removes are read as a tab, which keeps every position and does not end an authority. */
|
|
788
|
+
export function userinfoSpansOf(text) {
|
|
789
|
+
return findUserinfoSpans(text.replace(CONTROL_CHARS, (m) => '\t'.repeat(m.length)), true);
|
|
790
|
+
}
|
|
791
|
+
/** Whether position at lies in a user information of spans, scheme included, the "@" included. */
|
|
792
|
+
export function insideUserinfoSpan(spans, at) {
|
|
793
|
+
let lo = 0;
|
|
794
|
+
let hi = spans.length - 1;
|
|
795
|
+
while (lo <= hi) {
|
|
796
|
+
const mid = (lo + hi) >> 1;
|
|
797
|
+
const s = spans[mid];
|
|
798
|
+
if (at < s.start)
|
|
799
|
+
hi = mid - 1;
|
|
800
|
+
else if (at > s.at)
|
|
801
|
+
lo = mid + 1;
|
|
802
|
+
else
|
|
803
|
+
return true;
|
|
804
|
+
}
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
// Schemes whose address part is an e-mail address or a user at a host by design. "mailto:jane@example.com/x" and the
|
|
808
|
+
// like are shown as written, so the masking of a name and a secret before an "@" does not reach them.
|
|
809
|
+
const ADDRESS_SCHEMES = new Set(['mailto', 'xmpp', 'sip', 'sips', 'acct', 'im']);
|
|
810
|
+
/** Whether t[from, to) is a host name followed by an optional port, by the rule bareHostStart uses: dotted labels that
|
|
811
|
+
* end in a letters-only label of two or more characters or in an xn-- label, a dotted IPv4 address or localhost. A
|
|
812
|
+
* trailing dot, an empty label and a name with no dot other than localhost are not hosts. */
|
|
813
|
+
function isBareHostText(t, from, to) {
|
|
814
|
+
let end = to;
|
|
815
|
+
let k = to;
|
|
816
|
+
while (k > from && isDigit(t.charCodeAt(k - 1)))
|
|
817
|
+
k--;
|
|
818
|
+
if (k < to && k > from + 1 && t.charCodeAt(k - 1) === 0x3a)
|
|
819
|
+
end = k - 1;
|
|
820
|
+
if (end <= from)
|
|
821
|
+
return false;
|
|
822
|
+
let labels = 1;
|
|
823
|
+
let lastStart = from;
|
|
824
|
+
for (let i = from; i < end; i++) {
|
|
825
|
+
const c = t.charCodeAt(i);
|
|
826
|
+
if (c === 0x2e) {
|
|
827
|
+
if (i === lastStart || i + 1 >= end)
|
|
828
|
+
return false;
|
|
829
|
+
labels++;
|
|
830
|
+
lastStart = i + 1;
|
|
831
|
+
}
|
|
832
|
+
else if (!(isLetter(c) || isDigit(c) || c === 0x2d || c === 0x5f))
|
|
833
|
+
return false;
|
|
834
|
+
}
|
|
835
|
+
const name = t.slice(from, end);
|
|
836
|
+
if (labels === 1)
|
|
837
|
+
return name.replace(/\u017f/g, 's').toLowerCase() === 'localhost';
|
|
838
|
+
const last = t.slice(lastStart, end);
|
|
839
|
+
if (last.length >= 2 && /^[A-Za-z\u017f\u212a]+$/u.test(last))
|
|
840
|
+
return true;
|
|
841
|
+
if (last.length >= 5 && /^xn--[^_]*$/i.test(last))
|
|
842
|
+
return true;
|
|
843
|
+
return labels === 4 && /^\d{1,3}(?:\.\d{1,3}){3}$/.test(name);
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* The credential-shaped "label:name@host" of a text that has no scheme separator, as in user:secret@host.example/p
|
|
847
|
+
* (0.2.40, found by an independent check of 0.2.39). The URL parser reads "user:" as a scheme and the rest as an opaque
|
|
848
|
+
* path, so maskHref found no user information, and GFM read "secret@host.example" as an e-mail address, so the
|
|
849
|
+
* secret reached a report as an excerpt and as the text and the target of a link. A span is found in a run (RUN) when
|
|
850
|
+
* a label, the letters, digits and the characters + . - that start with a letter and follow no scheme character,
|
|
851
|
+
* is followed by a colon and then by text that holds an "@" before the first slash, backslash, question mark or number
|
|
852
|
+
* sign, and that delimiter, which must be there, makes the address a URL-like one: a slash or a backslash, a query that
|
|
853
|
+
* starts with a name and an equals sign, or a number sign with an equals sign later in the run. The text after the
|
|
854
|
+
* last "@" up to the delimiter must be a host name (isBareHostText). The user information is the text between the
|
|
855
|
+
* colon and that "@", and it must not be empty.
|
|
856
|
+
*
|
|
857
|
+
* Not a span, so shown as written: a special scheme (https:user@host/ is masked by the other rules), a scheme of
|
|
858
|
+
* ADDRESS_SCHEMES, a label that follows a slash (the authority rules read //name:secret@host/), an address that
|
|
859
|
+
* no delimiter follows, as in "Contact:jane@example.com", and a question mark or a number sign that is not followed by
|
|
860
|
+
* a value, as in "Did you mean To:jane@example.com?". "label:jane@example.com" with no space is therefore shown
|
|
861
|
+
* unless a path, a query value or a fragment value follows, because text alone cannot tell a label from a user name.
|
|
862
|
+
* A failed candidate is scanned to its own delimiter and a found one to its "@", so the scan stays linear. The result
|
|
863
|
+
* is a display aid and is never part of a comparison key.
|
|
864
|
+
*/
|
|
865
|
+
export function findBareUserinfoSpans(text) {
|
|
866
|
+
const spans = [];
|
|
867
|
+
const runs = new RegExp(RUN.source, RUN.flags);
|
|
868
|
+
for (let run = runs.exec(text); run !== null; run = runs.exec(text)) {
|
|
869
|
+
const s = run.index;
|
|
870
|
+
const e = s + run[0].length;
|
|
871
|
+
if (!run[0].includes('@'))
|
|
872
|
+
continue;
|
|
873
|
+
let lastEquals = e - 1;
|
|
874
|
+
while (lastEquals >= s && text.charCodeAt(lastEquals) !== 0x3d)
|
|
875
|
+
lastEquals--;
|
|
876
|
+
let i = s;
|
|
877
|
+
while (i < e) {
|
|
878
|
+
if (!isLetter(text.charCodeAt(i)) || (i > s && isSchemeChar(text.charCodeAt(i - 1)))) {
|
|
879
|
+
i++;
|
|
880
|
+
continue;
|
|
881
|
+
}
|
|
882
|
+
let b = i;
|
|
883
|
+
while (b < e && isSchemeChar(text.charCodeAt(b)))
|
|
884
|
+
b++;
|
|
885
|
+
if (b + 1 >= e || text.charCodeAt(b) !== 0x3a) {
|
|
886
|
+
i = b;
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
let k = b + 1;
|
|
890
|
+
let lastAt = -1;
|
|
891
|
+
while (k < e) {
|
|
892
|
+
const c = text.charCodeAt(k);
|
|
893
|
+
if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23)
|
|
894
|
+
break;
|
|
895
|
+
if (c === 0x40)
|
|
896
|
+
lastAt = k;
|
|
897
|
+
k++;
|
|
898
|
+
}
|
|
899
|
+
let ok = k < e && lastAt > b + 1 && isBareHostText(text, lastAt + 1, k);
|
|
900
|
+
if (ok) {
|
|
901
|
+
const d = text.charCodeAt(k);
|
|
902
|
+
if (d === 0x3f) {
|
|
903
|
+
let q = k + 1;
|
|
904
|
+
while (q < e && isParamChar(text.charCodeAt(q)))
|
|
905
|
+
q++;
|
|
906
|
+
ok = q > k + 1 && q < e && text.charCodeAt(q) === 0x3d;
|
|
907
|
+
}
|
|
908
|
+
else if (d === 0x23)
|
|
909
|
+
ok = lastEquals > k;
|
|
910
|
+
}
|
|
911
|
+
if (ok) {
|
|
912
|
+
const prev = i > s ? text.charCodeAt(i - 1) : -1;
|
|
913
|
+
ok = prev !== 0x2f && prev !== 0x5c && specialSchemeLength(text, i, e) !== b - i && !ADDRESS_SCHEMES.has(text.slice(i, b).toLowerCase());
|
|
914
|
+
}
|
|
915
|
+
if (ok) {
|
|
916
|
+
spans.push({ start: i, from: b + 1, at: lastAt });
|
|
917
|
+
i = lastAt + 1;
|
|
918
|
+
}
|
|
919
|
+
else
|
|
920
|
+
i = k;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
return spans;
|
|
924
|
+
}
|
|
925
|
+
/** Masks the user information of every "label:name@host/path" that findBareUserinfoSpans finds, and leaves the label,
|
|
926
|
+
* the host and the rest as they are: user:secret@host.example/p?x=1 reads user:***@host.example/p?x=1. Display only. */
|
|
927
|
+
export function maskBareUserinfo(text) {
|
|
928
|
+
if (!text.includes('@'))
|
|
929
|
+
return text;
|
|
930
|
+
const spans = findBareUserinfoSpans(text);
|
|
931
|
+
if (spans.length === 0)
|
|
932
|
+
return text;
|
|
933
|
+
let out = '';
|
|
934
|
+
let pos = 0;
|
|
935
|
+
for (const s of spans) {
|
|
936
|
+
out += text.slice(pos, s.from) + '***@';
|
|
937
|
+
pos = s.at + 1;
|
|
938
|
+
}
|
|
939
|
+
return out + text.slice(pos);
|
|
940
|
+
}
|
|
941
|
+
/** The spans of findBareUserinfoSpans for a source text, with the characters that stripControlChars removes read as
|
|
942
|
+
* a letter, which keeps every position and does not end a run or a label. */
|
|
943
|
+
export function bareUserinfoSpansOf(text) {
|
|
944
|
+
return findBareUserinfoSpans(text.replace(CONTROL_CHARS, (m) => 'a'.repeat(m.length)));
|
|
945
|
+
}
|
|
456
946
|
/** Masks user information, query values and fragments of every URL-like run in a piece of report text.
|
|
457
|
-
* maskParenUserinfo
|
|
458
|
-
*
|
|
459
|
-
* --format text report (0.2.16, found by an
|
|
947
|
+
* maskParenUserinfo and maskAuthorityUserinfo run first, for the shapes the runs of RUN cannot see as one URL, and
|
|
948
|
+
* maskBareUserinfo for the name:secret@host shape without a scheme separator. stripControlChars runs last so a raw
|
|
949
|
+
* ANSI escape from a compared page or an argument cannot reach a plain --format text report (0.2.16, found by an
|
|
950
|
+
* outside review 2026-09-28). */
|
|
460
951
|
export function redactText(text) {
|
|
461
|
-
|
|
952
|
+
// The control characters go first as well as last: a vertical tab, a form feed, U+2028 or U+0085 inside the user
|
|
953
|
+
// information would otherwise end the authority for the masking and be removed afterwards, which left a credential
|
|
954
|
+
// that read as a whole URL.
|
|
955
|
+
return stripControlChars(rewriteUrlRuns(maskBareUserinfo(maskAuthorityUserinfo(maskParenUserinfo(stripControlChars(text)), true)), maskHref));
|
|
462
956
|
}
|
|
463
957
|
/**
|
|
464
958
|
* The text with the parts that maskHref hides taken out of every URL-like run: user information, query values
|
|
@@ -470,7 +964,7 @@ export function hideUrlSecrets(text) {
|
|
|
470
964
|
// review 2026-09-28): up to here only redactText applied it, so visibleNumbers below still read a
|
|
471
965
|
// number straight out of an unmasked "(name):secret@" that the general run-based scan cannot see as
|
|
472
966
|
// one run (see the comment on PAREN_USERINFO_START above).
|
|
473
|
-
return rewriteUrlRuns(maskParenUserinfo(text), withoutUrlSecrets);
|
|
967
|
+
return rewriteUrlRuns(maskAuthorityUserinfo(maskParenUserinfo(text)), withoutUrlSecrets);
|
|
474
968
|
}
|
|
475
969
|
/**
|
|
476
970
|
* The numeric tokens a report may show. extractNumbers reads the whole text, so a numeric query value, such as
|
|
@@ -494,8 +988,26 @@ export function trimHref(href) {
|
|
|
494
988
|
end--;
|
|
495
989
|
return href.slice(start, end);
|
|
496
990
|
}
|
|
497
|
-
/**
|
|
991
|
+
/** The longest link target the comparison accepts, in characters of the href as the page or the Markdown wrote it.
|
|
992
|
+
* Up to 0.2.38 the length had no bound, and the time of the comparison grew with the length times the number of
|
|
993
|
+
* links, blocks or references that handle it again (minutes for a few thousand links against a 100 000 character
|
|
994
|
+
* target). 8192 is the longest request line the Apache default accepts and is far above any link a page needs;
|
|
995
|
+
* a data: URI longer than this is not a link a reader follows. */
|
|
996
|
+
export const MAX_LINK_TARGET_CHARS = 8192;
|
|
997
|
+
/** A link target is longer than MAX_LINK_TARGET_CHARS. It names the length and never repeats the target. */
|
|
998
|
+
export class LinkTargetError extends Error {
|
|
999
|
+
length;
|
|
1000
|
+
constructor(length) {
|
|
1001
|
+
super(`A link target is ${length} characters long, more than the limit of ${MAX_LINK_TARGET_CHARS}; the comparison was not run.`);
|
|
1002
|
+
this.length = length;
|
|
1003
|
+
this.name = 'LinkTargetError';
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
/** Resolves a possibly relative href against a base. Returns null when it cannot be resolved. Throws a
|
|
1007
|
+
* LinkTargetError for a target longer than MAX_LINK_TARGET_CHARS. */
|
|
498
1008
|
export function resolveHref(rawHref, base) {
|
|
1009
|
+
if (rawHref.length > MAX_LINK_TARGET_CHARS)
|
|
1010
|
+
throw new LinkTargetError(rawHref.length);
|
|
499
1011
|
const href = trimHref(rawHref);
|
|
500
1012
|
if (href === '')
|
|
501
1013
|
return null;
|
|
@@ -529,7 +1041,7 @@ export function maskHref(href) {
|
|
|
529
1041
|
u.password = '';
|
|
530
1042
|
// One pass: a Set keeps the first-seen key order and merges duplicates, and the
|
|
531
1043
|
// masked query is built once with append. Calling searchParams.set for each key re-serializes the whole query
|
|
532
|
-
// every time, which is quadratic in the number of keys (
|
|
1044
|
+
// every time, which is quadratic in the number of keys (20 000 keys took tens of seconds). An empty
|
|
533
1045
|
// query is left as it is, so "?" stays "?".
|
|
534
1046
|
const maskedQuery = new URLSearchParams();
|
|
535
1047
|
for (const key of new Set(u.searchParams.keys()))
|
package/dist/src/report.js
CHANGED
|
@@ -4,9 +4,13 @@ import { redactText } from './normalize.js';
|
|
|
4
4
|
/** Reporting boundary: every string that leaves the tool goes through redactText. Internal comparison
|
|
5
5
|
* has already happened on the original values. */
|
|
6
6
|
export function redactReport(report) {
|
|
7
|
+
return mapStrings(report, redactText);
|
|
8
|
+
}
|
|
9
|
+
/** A copy of the value with fn applied to every string in it, however deep. */
|
|
10
|
+
function mapStrings(value, fn) {
|
|
7
11
|
const walk = (v) => {
|
|
8
12
|
if (typeof v === 'string')
|
|
9
|
-
return
|
|
13
|
+
return fn(v);
|
|
10
14
|
if (Array.isArray(v))
|
|
11
15
|
return v.map(walk);
|
|
12
16
|
if (v && typeof v === 'object') {
|
|
@@ -17,7 +21,13 @@ export function redactReport(report) {
|
|
|
17
21
|
}
|
|
18
22
|
return v;
|
|
19
23
|
};
|
|
20
|
-
return walk(
|
|
24
|
+
return walk(value);
|
|
25
|
+
}
|
|
26
|
+
/** A value that came from a page or from an argument stays on its own line of the text report. A line feed or a
|
|
27
|
+
* carriage return in a link target, which an attribute can write as or , started a line that looked like a
|
|
28
|
+
* finding (0.2.39, found by an outside review 2026-11-14). The break is shown as \n or \r, the way JSON shows it. */
|
|
29
|
+
function oneLine(s) {
|
|
30
|
+
return s.replace(/[\r\n]/g, (c) => (c === '\n' ? '\\n' : '\\r'));
|
|
21
31
|
}
|
|
22
32
|
export function renderJson(input) {
|
|
23
33
|
return JSON.stringify(redactReport(input), null, 2) + '\n';
|
|
@@ -49,7 +59,7 @@ function findingLines(i, f) {
|
|
|
49
59
|
return lines;
|
|
50
60
|
}
|
|
51
61
|
export function renderText(input) {
|
|
52
|
-
const report = redactReport(input);
|
|
62
|
+
const report = mapStrings(redactReport(input), oneLine);
|
|
53
63
|
const out = [];
|
|
54
64
|
const s = report.summary;
|
|
55
65
|
out.push(`${TOOL_NAME} ${report.toolVersion} (${report.mode} mode${s.strict ? ', strict' : ''})`);
|