Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/lens/js/redact.js

27.3 KiB, 38 runs

created by r1870400018:65604, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1// redact.js -- what keeps a secret out of a lens row. Pure functions, no host globals, so the
2// browser writer (`writer.js`) and the node reader (`reader.mjs`) import one copy.
3//
4// Extracted from Daimond's `www/js/debugshare.js` and `dev/lens.mjs` (2026-10-02). Three guards,
5// in the order a value meets them:
6//
7// 1. By NAME: a field called `apiKey`, `token`, `passphrase` ... is replaced by a fingerprint
8// whatever it holds (`SECRET_RE`, and `SECRET_RE_LOOSE` for camelCase names).
9// 2. By the caller: a deny-list of names and paths, and a string test, both supplied by the
10// app, which alone knows its own secrets (Oxegen's eight-word passphrase is one).
11// 3. By CONTENT: the scrubber below finds the shapes a credential wears inside free text.
12//
13// The name rule, the loose rule and the fingerprint are the ones `fe2o3_net::lens::redact` mirrors
14// in Rust; `test/crosscheck.test.mjs` runs the JS over that crate's oracle tables.
15
16// ── By name ──────────────────────────────────────────────────────────
17
18// Field names whose VALUE is a secret. Matched on the key name, case-insensitively, at any depth.
19export const SECRET_RE = /(?:^|[_.-])(?:apikey|api_key|key|token|secret|passphrase|password|salt|wrapped|wrappedpriv|sealed|seal|privatekey|priv|mnemonic|seed|masterkey)(?:$|[_.-]|enc\b)/i;
20
21// `SECRET_RE` only fires on a separator or the start of the name, so a camelCase-joined field
22// (`pushToken`, `refreshSecret`) slides past it. This companion fires anywhere the word appears,
23// with `key` anchored to the end of the name. It is opt-in, because a bare "token" match would
24// also take a field such as `tokenStats`.
25export const SECRET_RE_LOOSE = /token|secret|passphrase|password|key$/i;
26
27// The fields whose value is something a person typed and remembers: a passphrase, a password, a
28// mailbox's app password (Daimond's `mail.js` posts it as `password` and keeps it sealed as `pass`), a
29// recovery phrase. Matched at the END of the name, so `smtpPassword` and `newPassphrase` go while
30// `passAt` and `passwordHint` stay, and `pass` only whole, so `bypass` and `compass` stay. Such a value
31// is marked with no head (`typedMark`): the six characters `fingerprint` keeps to tell two keys apart
32// are the first word of a passphrase. SCRUB-ONE, D-20261006-24.
33export const SECRET_KEY_TYPED = /(?:pass(?:word|wd|phrase)|mnemonic|seed[_-]?phrase)$|^pass$/i;
34
35/// Is this field name one whose value a person typed (`SECRET_KEY_TYPED`)?
36export function typedName(name) {
37 return SECRET_KEY_TYPED.test(String(name));
38}
39
40/// Does this field name look like a secret? `loose` adds `SECRET_RE_LOOSE`. A typed name
41/// (`typedName`) is covered by the walks as well, and is not counted here, which is `SECRET_RE` alone.
42export function secretName(name, loose) {
43 var n = String(name);
44 return SECRET_RE.test(n) || (!!loose && SECRET_RE_LOOSE.test(n));
45}
46
47/// A short, non-reversible rendering of a secret for a person's eye: its first `head` characters
48/// (six by default; none is safer for a passphrase), a stable hash and its length. Enough to tell
49/// two values apart and nothing an attacker could run with. A non-string is rendered as text
50/// first, and an absent one is a typed marker.
51export function fingerprint(v, head) {
52 if (v == null) return '[redacted:absent]';
53 if (typeof v !== 'string') {
54 try { v = String(v); } catch (e) { return '[redacted:' + typeof v + ']'; }
55 }
56 if (v === '') return '[redacted:empty]';
57 var shown = v.slice(0, head == null ? 6 : head);
58 // djb2, as unsigned hex. It only has to separate two values stably; the raw one never travels.
59 var h = 5381;
60 for (var i = 0; i < v.length; i++) { h = ((h << 5) + h + v.charCodeAt(i)) >>> 0; }
61 return '[redacted ' + shown + '…#' + h.toString(16) + '/' + v.length + ']';
62}
63
64/// The UTF-8 byte length of a string, which is what a transport pays and so what a cap counts.
65export function byteLen(s) {
66 try { return new TextEncoder().encode(s).length; }
67 catch (e) {
68 try { return unescape(encodeURIComponent(s)).length; }
69 catch (e2) { return s.length; }
70 }
71}
72
73// ── By content ───────────────────────────────────────────────────────
74
75// ── THE CONTENT SCRUBBER (SHARED BLOCK) ──────────────────────
76//
77// Lifted from Daimond's `debugshare.js` and `dev/lens.mjs`, where it stood twice and a test held
78// the copies together; here it is the one copy, imported by the writer and the reader alike.
79// It stays in the `var` and `function` dialect of the classic scripts it came from, so a caller
80// that still wants a text copy can cut it at the two sentinel lines.
81//
82// WHY IT EXISTS. `redact`/`redactConfig` match a field NAME. That is the whole
83// guarantee for `config`, and it is worth nothing for a credential sitting in FREE
84// TEXT: a console line that printed one, a tile of the daimon's own answer quoting
85// one back, a failed fetch whose URL carries `?token=`, a tool argument, a stack
86// frame. So this matches by CONTENT. Every string in a payload is searched for the
87// shapes a credential actually wears, and each hit is replaced by a marker naming
88// the shape, a stable hash and the length -- so a reader still sees that a key was
89// there, and can tell two occurrences apart, while the value never leaves the
90// device.
91//
92// AND A CATCH-ALL BEHIND THEM. A shape nobody has enumerated is still a long,
93// unbroken, high-entropy run of characters, and nothing the feed is meant to carry
94// looks like that: a digest, a build id and a device id are hex, which tops out at
95// exactly 4 bits a character, and ordinary prose and identifiers are far below it.
96// So a run of `SCRUB_MIN_RUN` or more `[A-Za-z0-9_-]` whose Shannon entropy is
97// above `SCRUB_MIN_BITS` bits a character, and which is not one of the shapes
98// `scrubSafeRun` names, goes the same way.
99
100var SCRUB_MIN_RUN = 32; // shortest unbroken run the entropy catch considers
101var SCRUB_MIN_BITS = 4.0; // ... and the bits per character above which it fires
102var SCRUB_MAX_DEPTH = 40; // the walk's bound, as in `redact` beside it
103
104// The shapes, each matched whole and replaced whole.
105var SCRUB_SHAPES = [
106 // A PEM private key -- whole, or clipped by elision, in which case everything
107 // from the header to the end of the string goes.
108 { k: 'pem', re: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$)/g },
109 { k: 'gh', re: /\bgh[pousr]_[A-Za-z0-9]{16,}/g },
110 { k: 'ghpat', re: /\bgithub_pat_[A-Za-z0-9_]{20,}/g },
111 { k: 'stripe', re: /\b[sr]k_(?:live|test)_[A-Za-z0-9]{10,}/g },
112 { k: 'whsec', re: /\bwhsec_[A-Za-z0-9]{16,}/g },
113 // `sk-`, `sk-or-v1-`, `sk-ant-api03-`: one rule, because every provider that
114 // took the prefix kept the same alphabet after it.
115 { k: 'sk', re: /\bsk-[A-Za-z0-9_-]{16,}/g },
116 { k: 'aws', re: /\b(?:AKIA|ASIA|ABIA|ACCA|AGPA|AIDA|AIPA|ANPA|ANVA|AROA|APKA)[0-9A-Z]{12,}/g },
117 { k: 'gcp', re: /\bAIza[0-9A-Za-z_-]{30,}/g },
118 { k: 'slack', re: /\bxox[abeprs]-[A-Za-z0-9-]{10,}/g },
119 { k: 'jwt', re: /\beyJ[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{6,}\.[A-Za-z0-9_-]{4,}/g },
120 // The tune relay mints one of these per run and the page carries it as its
121 // provider key (`dev/tune/run.mjs`), so it reaches the feed by exactly the path
122 // a real provider key would.
123 { k: 'tune', re: /\btune-[0-9a-f]{32}/g },
124];
125
126// The secret NAMES the `named` rules look for against a value, as regex source:
127// every one, and the typed ones whose value runs to the end of the line.
128var SCRUB_NAME_TYPED = '(?:passphrase|password|passwd|mnemonic|seed[_-]?phrase)';
129var SCRUB_NAME = '(?:api[_-]?key|apikey|auth[_-]?token|access[_-]?token|token|secret|'
130 + 'master[_-]?key|private[_-]?key|salt|sealed|wrapped|' + SCRUB_NAME_TYPED.slice(3, -1) + ')';
131
132// The shapes where the NAME must survive and only the value goes. A scrubbed
133// `?token=` is still a legible URL, and a console line with the value cut out of it
134// still says what the code was doing -- which fingerprinting the whole line, as
135// `looksSecret` did, did not.
136var SCRUB_PAIRS = [
137 // An `Authorization` header, however it was spelled into the text. `bearer` in
138 // any case, since a header name and its scheme are case-free on the wire;
139 // `Basic` and `Token` capitalised only, as prose uses both words lower case.
140 { k: 'bearer', g: 3, re: /\b([Bb][Ee][Aa][Rr][Ee][Rr]|Basic|Token)(\s+)([A-Za-z0-9._~+/=-]{8,})/g },
141 // A credential in a URL's query or fragment. `[` and `]` are excluded from the
142 // value so a marker already placed here is not taken for a value and marked a
143 // second time: the lens runs this block AGAIN on ingest, over output the client
144 // has already scrubbed, and a rule that is not idempotent nests its own markers.
145 { k: 'urlarg', g: 2, re: /([?&#](?:access_token|refresh_token|id_token|token|api[_-]?key|apikey|key|auth|secret|password|passwd|sig|signature)=)([^&\s"'#<>\[\]]{4,})/gi },
146 // A secret NAME sitting against a value, with the value cut out so the message
147 // still reads. Quoted first, single or double, whole up to the closing quote
148 // (an escaped one does not close it) and at any length. Then, for the names
149 // whose value a person types (a passphrase, a password, a recovery phrase), the
150 // rest of the line: a passphrase is words, and the run-of-eight rule that once
151 // stood alone here let `passphrase: correct horse battery staple` through whole
152 // (SCRUB-ONE, D-20261006-24). Last, the run-of-eight for the names whose value is
153 // a token. A value that opens on a quote or on a marker is left to the quoted
154 // rule or as it is, so a second pass over scrubbed text changes nothing.
155 { k: 'named', g: 2, re: new RegExp('(' + SCRUB_NAME + '["\']?\\s{0,3}[:=]\\s{0,3}")((?!\\[redacted )(?:[^"\\\\\\r\\n]|\\\\.)+)(?=")', 'gi') },
156 { k: 'named', g: 2, re: new RegExp("(" + SCRUB_NAME + "[\"']?\\s{0,3}[:=]\\s{0,3}')((?!\\[redacted )(?:[^'\\\\\\r\\n]|\\\\.)+)(?=')", 'gi') },
157 { k: 'named', g: 2, re: new RegExp('(' + SCRUB_NAME_TYPED + '["\']?[ \\t]{0,3}[:=][ \\t]{0,3})((?!["\']|\\[redacted )\\S(?:[^\\r\\n]*\\S)?)', 'gi') },
158 { k: 'named', g: 2, re: new RegExp('(' + SCRUB_NAME + '["\'\\s]{0,3}[:=]["\'\\s]{0,3})([A-Za-z0-9_\\-./+=]{8,})', 'gi') },
159 // An AWS secret is forty characters of base64 wearing no prefix at all, so it
160 // is only recognisable NEXT TO the access-key id or the name it belongs to.
161 { k: 'awssec', g: 2, re: /((?:AKIA|ASIA)[0-9A-Z]{12,}[\s\S]{0,200}?["'\s:=,])([A-Za-z0-9/+=]{40})(?![A-Za-z0-9/+=])/g },
162 { k: 'awssec', g: 2, re: /((?:aws)?_?secret_?access_?key["'\s]{0,3}[:=]["'\s]{0,3})([A-Za-z0-9/+=]{40})(?![A-Za-z0-9/+=])/gi },
163];
164
165// Built from the constant rather than repeating it, so the two cannot drift.
166var SCRUB_RUN_RE = new RegExp('[A-Za-z0-9_-]{' + SCRUB_MIN_RUN + ',}', 'g');
167
168/// A short, non-reversible marker for one scrubbed value: the shape it wore, a
169/// stable hash and its length. The hash is what lets a reader say "the same thing
170/// again" across two events without the value travelling; it is djb2, the one
171/// `fingerprint` already uses, because the two markers sit side by side in a log
172/// and a reader should not have to learn both.
173function scrubMark(kind, v) {
174 var h = 5381;
175 for (var i = 0; i < v.length; i++) { h = ((h << 5) + h + v.charCodeAt(i)) >>> 0; }
176 return '[redacted ' + kind + ' #' + h.toString(16) + '/' + v.length + ']';
177}
178
179/// Shannon entropy of a string, in bits per character. Hex tops out at 4.0 by
180/// construction and base64url at 6.0, so the threshold sits exactly where a digest
181/// stops and a key begins.
182function scrubEntropy(s) {
183 var counts = {}, i, c;
184 for (i = 0; i < s.length; i++) {
185 c = s.charAt(i);
186 counts[c] = (counts[c] || 0) + 1;
187 }
188 var keys = Object.keys(counts), h = 0, p;
189 for (i = 0; i < keys.length; i++) {
190 p = counts[keys[i]] / s.length;
191 h -= p * (Math.log(p) / Math.LN2);
192 }
193 return h;
194}
195
196/// Is this unbroken run one of the shapes the feed is SUPPOSED to carry? Named
197/// explicitly rather than left to the entropy threshold, so the feed stays legible
198/// by rule and not by luck. Measured against the real archive rather than guessed
199/// at: a sha256 digest, a build id and a device id are hex; a tool call's
200/// correlation id and a message id are hex joined by `-` or `_`, which is why the
201/// separators are stripped before the hex test rather than tested around; a count
202/// or a stamp is decimal; and an identifier built out of words and a number
203/// (`worker_pool_seat_000412`) is every segment a word or a number and nothing else.
204function scrubSafeRun(s) {
205 var bare = s.replace(/[-_]/g, '');
206 if (/^[0-9a-fA-F]+$/.test(bare)) return true; // digests, build ids, device ids, UUIDs
207 if (/^[0-9]+$/.test(bare)) return true; // counts, stamps, sequence numbers
208 if (/^[A-Za-z]+$/.test(bare)) return true; // identifiers and runs of prose
209 // Every `-`/`_` separated segment a word or a number: an identifier a person
210 // wrote, not a value a generator produced.
211 var parts = s.split(/[-_]/), i;
212 if (parts.length > 1) {
213 for (i = 0; i < parts.length; i++) {
214 if (!/^(?:[A-Za-z]{2,}|[0-9]+)$/.test(parts[i])) return false;
215 }
216 return true;
217 }
218 return false;
219}
220
221// The fields whose VALUE is a correlation id the reader navigates by -- a tool
222// call matched to its result, a turn to its rounds, a device to its events. A
223// provider mints some of them out of an alphabet indistinguishable from a key's,
224// so the entropy catch alone would take the feed's whole index with it. They are
225// exempt from THAT catch and from nothing else: an `id` holding an `sk-` key is
226// still scrubbed by the shape rules, which run first and do not consult this.
227var SCRUB_ID_KEYS = {
228 id: 1, mid: 1, callId: 1, call_id: 1, tool_call_id: 1, toolCallId: 1,
229 turn: 1, chat: 1, chatId: 1, device: 1, deviceId: 1, build: 1, d: 1, b: 1, n: 1, w: 1,
230 // Daimond added `holder`, `self`, `nominated` and `body` (its archive's snapshot filename);
231 // a caller adds its own through `createScrubber({ idKeys })`.
232};
233
234/// One string, with every credential shape in it replaced by a marker. Returns the
235/// string unchanged when it holds none -- which is the overwhelmingly common case,
236/// and the one the per-tick cost is measured on. `isId` suppresses the entropy
237/// catch alone, for the correlation-id fields `SCRUB_ID_KEYS` names.
238function scrubText(s, isId, cfg) {
239 if (!s || s.length < 8) return s;
240 var i, r;
241 var shapes = (cfg && cfg.shapes) ? SCRUB_SHAPES.concat(cfg.shapes) : SCRUB_SHAPES;
242 var pairs = (cfg && cfg.pairs) ? SCRUB_PAIRS.concat(cfg.pairs) : SCRUB_PAIRS;
243 for (i = 0; i < shapes.length; i++) {
244 r = shapes[i];
245 s = s.replace(r.re, (function (kind) {
246 return function (m) { return scrubMark(kind, m); };
247 })(r.k));
248 }
249 for (i = 0; i < pairs.length; i++) {
250 r = pairs[i];
251 s = s.replace(r.re, (function (rule) {
252 return function () {
253 var args = arguments, keep = '', j;
254 for (j = 1; j < rule.g; j++) keep += (args[j] == null ? '' : args[j]);
255 return keep + scrubMark(rule.k, String(args[rule.g]));
256 };
257 })(r));
258 }
259 // The catch-all runs LAST, so a run already turned into a marker above is not
260 // weighed a second time.
261 if (isId) return s;
262 s = s.replace(SCRUB_RUN_RE, function (m) {
263 if (scrubSafeRun(m)) return m;
264 if (scrubEntropy(m) <= SCRUB_MIN_BITS) return m;
265 return scrubMark('hi', m);
266 });
267 return s;
268}
269
270/// `fingerprint`, except for a field `SECRET_KEY_TYPED` names, whose non-empty string value is
271/// marked with no head at all (`[redacted named #<hash>/<length>]`). A value already marked passes
272/// as it is, so a second pass over what a client sent changes nothing.
273function typedMark(k, v, head) {
274 if (!SECRET_KEY_TYPED.test(k) || typeof v !== 'string' || v === '') return fingerprint(v, head);
275 if (v.indexOf('[redacted') === 0) return v;
276 return scrubMark('named', v);
277}
278
279/// A deep copy of a payload with `scrubText` applied to every string in it, keys
280/// included, and a field `SECRET_KEY_TYPED` names marked whole -- a key can be a chat
281/// id or a model name, and nothing stops a future one being a token. Cycle-safe and depth-bounded like the redactors beside it,
282/// since what it walks is arbitrary state.
283function scrubDeep(obj, seen, depth, isId, cfg) {
284 seen = seen || [];
285 depth = depth || 0;
286 if (typeof obj === 'string') return scrubText(obj, isId, cfg);
287 if (obj == null || typeof obj !== 'object') return obj;
288 if (depth > SCRUB_MAX_DEPTH || seen.indexOf(obj) !== -1) return '[scrubbed:cycle-or-deep]';
289 seen = seen.concat([obj]);
290 var i;
291 if (Array.isArray(obj)) {
292 var arr = [];
293 // An array inherits its parent's field name: `callIds: [...]` is still ids.
294 for (i = 0; i < obj.length; i++) arr[i] = scrubDeep(obj[i], seen, depth + 1, isId, cfg);
295 return arr;
296 }
297 var out = {}, keys = Object.keys(obj), k, v;
298 for (i = 0; i < keys.length; i++) {
299 k = keys[i];
300 v = obj[k];
301 // A typed secret held as a field, a mailbox's app password in a posted body,
302 // has no name beside it in its own string for `named` to find.
303 out[scrubText(k, false, cfg)] = (typeof v === 'string' && SECRET_KEY_TYPED.test(k))
304 ? typedMark(k, v)
305 : scrubDeep(v, seen, depth + 1,
306 SCRUB_ID_KEYS[k] === 1 || !!(cfg && cfg.idKeys && cfg.idKeys[k] === 1), cfg);
307 }
308 return out;
309}
310// ── END OF THE SHARED SCRUBBER BLOCK ─────────────────────────
311
312/// A scrubber with the caller's additions: `shapes` and `pairs` (rules in the form of
313/// `SCRUB_SHAPES` and `SCRUB_PAIRS`, where a pair's `g` is the group holding the value) and
314/// `idKeys` (field names whose values are correlation ids, exempt from the entropy catch).
315/// Everything the base carries still applies. Returns `{ text(s, isId), deep(obj) }`.
316export function createScrubber(cfg) {
317 cfg = cfg || {};
318 var c = { shapes: cfg.shapes || null, pairs: cfg.pairs || null, idKeys: cfg.idKeys || null };
319 return {
320 text: function (s, isId) { return scrubText(s, isId, c); },
321 deep: function (obj) { return scrubDeep(obj, undefined, undefined, undefined, c); },
322 };
323}
324
325export {
326 SCRUB_ID_KEYS,
327 SCRUB_PAIRS,
328 SCRUB_SHAPES,
329 scrubDeep,
330 scrubEntropy,
331 scrubMark,
332 scrubSafeRun,
333 scrubText,
334 typedMark,
335};
336
337// ── The word-run test ────────────────────────────────────────────────
338
339/// A test over a caller's word list: `n` of its words in a row (eight unless told) are a secret.
340///
341/// An app whose passphrases are drawn from a list of words (Oxegen's are eight words of the EFF large
342/// list) cannot name the secret by shape, since nothing about a word is secret, but a string in which
343/// `n` of the list's words stand together is a passphrase or near enough to one that no row may keep
344/// it. A word is a run of ASCII letters, whatever separates it from the next (a space, a hyphen, a
345/// comma, a line break, a digit, a letter outside ASCII) and in any case; a run of letters that is not
346/// on the list breaks the run. A list entry may hold hyphens (the EFF list has `drop-down`, `felt-tip`,
347/// `t-shirt` and `yo-yo`): where the text spells such an entry, hyphens and all, it is the one word it
348/// is, and the longest entry wins. Splitting on every non-letter cannot see those four, and breaks a
349/// passphrase that holds one into runs shorter than eight. An entry whose every part is a list word
350/// counts for its parts, since a dashed passphrase joins its words with the same hyphen.
351///
352/// `words` is an iterable of words, or a function that answers one (or null while the list is not
353/// there yet, when the guard finds nothing). Returns `{ spans, has, mask, deep }`:
354///
355/// spans(s) the `[start, end)` of each stretch of `n` words or more, from the first word's
356/// first letter to the last word's last, with what stood between the words;
357/// has(s) is there one;
358/// mask(s, mark) `s` with each such stretch replaced by `mark(stretch)`, which must show nothing of it;
359/// deep(v, mark) a deep copy of any JSON-shaped value with every string, and every key, masked.
360///
361/// The Rust half is `fe2o3_net::lens::Phrase`; `tests/data/lens_phrase.tsv` holds the two to the same
362/// answers.
363export function createRunGuard(words, n) {
364 var RUN = n == null ? 8 : Math.max(1, n | 0);
365 var set = null, parts = 1;
366
367 function list() {
368 if (set) return set;
369 var w = null;
370 try { w = typeof words === 'function' ? words() : words; } catch (e) { w = null; }
371 // An array has a length, a Set does not: either may serve, and a list of nothing is no list.
372 if (w && (w.length === undefined || w.length > 0)) {
373 var s = new Set();
374 w.forEach(function (x) {
375 x = String(x).trim().toLowerCase();
376 if (x) { s.add(x); parts = Math.max(parts, x.split('-').length); }
377 });
378 set = s;
379 }
380 return set;
381 }
382
383 // The word starting at token `i`, as [tokens it takes up, words it counts for], or [0, 0]. The longest
384 // hyphen-joined entry the text spells wins; it counts for one word, or for as many as it has parts
385 // where each part is itself a word, since hyphens also join the words of a dashed passphrase and the
386 // safer count is the larger.
387 function wordAt(s, t, i) {
388 var chain = 1, k, j, each;
389 while (chain < parts && i + chain < t.length && s.slice(t[i + chain - 1][1], t[i + chain][0]) === '-') chain++;
390 for (k = chain; k >= 2; k--) {
391 if (set.has(s.slice(t[i][0], t[i + k - 1][1]).toLowerCase())) {
392 each = true;
393 for (j = i; j < i + k; j++) if (!set.has(s.slice(t[j][0], t[j][1]).toLowerCase())) { each = false; break; }
394 return [k, each ? k : 1];
395 }
396 }
397 return set.has(s.slice(t[i][0], t[i][1]).toLowerCase()) ? [1, 1] : [0, 0];
398 }
399
400 function spans(s) {
401 if (!list() || typeof s !== 'string' || s.length < RUN * 2 - 1) return [];
402 var t = [], re = /[A-Za-z]+/g, m;
403 while ((m = re.exec(s)) !== null) t.push([m.index, m.index + m[0].length]);
404 var out = [], from = 0, to = 0, run = 0, i = 0, w, took;
405 while (i < t.length) {
406 w = wordAt(s, t, i);
407 took = w[0];
408 if (!took) {
409 if (run >= RUN) out.push([from, to]);
410 run = 0; i += 1;
411 continue;
412 }
413 if (!run) from = t[i][0];
414 to = t[i + took - 1][1];
415 run += w[1]; i += took;
416 }
417 if (run >= RUN) out.push([from, to]);
418 return out;
419 }
420
421 function has(s) { return spans(s).length > 0; }
422
423 function mask(s, mark) {
424 var sp = spans(s);
425 if (!sp.length) return s;
426 var out = '', at = 0;
427 sp.forEach(function (x) { out += s.slice(at, x[0]) + mark(s.slice(x[0], x[1])); at = x[1]; });
428 return out + s.slice(at);
429 }
430
431 function deep(v, mark, d) {
432 d = d || 0;
433 if (typeof v === 'string') return mask(v, mark);
434 if (v == null || typeof v !== 'object' || d > 40) return v;
435 if (Array.isArray(v)) return v.map(function (x) { return deep(x, mark, d + 1); });
436 var o = {};
437 Object.keys(v).forEach(function (k) { o[mask(k, mark)] = deep(v[k], mark, d + 1); });
438 return o;
439 }
440
441 return { spans: spans, has: has, mask: mask, deep: deep };
442}
443
444// ── Cutting a string ─────────────────────────────────────────────────
445
446// JSON.stringify writes half of a surrogate pair as a `\udXXX` escape, which the peer's strict reader
447// (`fe2o3_net::lens::chunk`) refuses, covering the whole bundle. So no row may hold half a pair: a
448// page cuts with `clipChars` and mends what reaches it broken with `soundText`.
449
450/// The first `n` UTF-16 units of `s`, or one fewer where the cut falls inside a pair. It cuts a string
451/// that is already sound without breaking it, and mends nothing: `soundText` first for one that may not be.
452export function clipChars(s, n) {
453 if (s.length <= n) return s;
454 var c = s.charCodeAt(n - 1);
455 return s.slice(0, c >= 0xD800 && c <= 0xDBFF ? n - 1 : n);
456}
457
458/// `s` with each half of a surrogate pair that has no other half replaced by U+FFFD, which is what a lone
459/// half is when it is read as text. A whole pair is left as it is.
460export function soundText(s) {
461 return s.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDFFF]/g, function (m) { return m.length === 2 ? m : '\uFFFD'; });
462}
463
464// ── The redactor: names, the caller's hooks, and the walk ────────────
465
466var MAX_DEPTH = 40;
467var DEEP = '[redacted:cycle-or-deep]';
468var OBJECT = '[redacted:object]';
469
470// Is this already a fingerprint, so that fingerprinting it again would be a fingerprint of one?
471function isMarker(s) {
472 return typeof s === 'string' && s.indexOf('[redacted') === 0 && s.charAt(s.length - 1) === ']';
473}
474
475/// A redactor over any JSON-shaped value. Options, all optional:
476///
477/// deny an array of names (a string, matched at any depth) and paths (an array of names,
478/// where `*` stands for any one name, matched from the root);
479/// test a function `string -> bool`; a string it calls secret is replaced whole, as are keys;
480/// head the characters of a value a fingerprint shows (six, or 0 for a passphrase);
481/// loose also apply `SECRET_RE_LOOSE` (for a free-form config object).
482///
483/// A field `SECRET_KEY_TYPED` names (`password`, `pass`, `newPassphrase`) is covered too, and its
484/// value marked with no head, as `[redacted named #<hash>/<length>]`.
485///
486/// `value(x)` returns a deep copy and never mutates; `text(s)` answers a fingerprint when the
487/// caller's test calls `s` secret and null otherwise. The walk is cycle-safe and stops at forty
488/// levels, as the Rust `lens::Redact` does.
489export function createRedactor(opts) {
490 opts = opts || {};
491 var head = opts.head == null ? 6 : opts.head;
492 var loose = !!opts.loose;
493 var test = typeof opts.test === 'function' ? opts.test : null;
494 var names = [], paths = [];
495 (opts.deny || []).forEach(function (d) {
496 if (Array.isArray(d)) paths.push(d.map(String)); else names.push(String(d));
497 });
498
499 function denied(path) {
500 if (names.length && names.indexOf(path[path.length - 1]) !== -1) return true;
501 for (var i = 0; i < paths.length; i++) {
502 var p = paths[i];
503 if (p.length !== path.length) continue;
504 var all = true;
505 for (var j = 0; j < p.length; j++) { if (p[j] !== '*' && p[j] !== path[j]) { all = false; break; } }
506 if (all) return true;
507 }
508 return false;
509 }
510
511 function fp(v) { return fingerprint(v, head); }
512
513 // A secret-named or denied field: fingerprint a scalar, note a structured value as present. A
514 // typed field's scalar (`SECRET_KEY_TYPED`) is marked with no head, whatever `head` says.
515 function cover(v, k) {
516 if (v != null && typeof v === 'object') return OBJECT;
517 if (isMarker(v)) return v;
518 if (k != null && typedName(k) && v != null && v !== '') return scrubMark('named', String(v));
519 return fp(v);
520 }
521
522 function key(k) { return (test && !isMarker(k) && test(k)) ? fp(k) : k; }
523
524 function value(v, path, seen, depth) {
525 if (typeof v === 'string') return (test && !isMarker(v) && test(v)) ? fp(v) : v;
526 if (v == null || typeof v !== 'object') return v;
527 if (depth > MAX_DEPTH || seen.indexOf(v) !== -1) return DEEP;
528 seen = seen.concat([v]);
529 if (Array.isArray(v)) return v.map(function (x) { return value(x, path, seen, depth + 1); });
530 var out = {};
531 Object.keys(v).forEach(function (k) {
532 var nk = key(k);
533 while (Object.prototype.hasOwnProperty.call(out, nk)) nk += '~';
534 path.push(k);
535 out[nk] = (secretName(k, loose) || typedName(k) || denied(path)) ? cover(v[k], k) : value(v[k], path, seen, depth + 1);
536 path.pop();
537 });
538 return out;
539 }
540
541 return {
542 value: function (x) { return value(x, [], [], 0); },
543 text: function (s) { return (test && typeof s === 'string' && !isMarker(s) && test(s)) ? fp(s) : null; },
544 fingerprint: fp,
545 secret: function (name) { return secretName(name, loose); },
546 typed: typedName,
547 };
548}