@daybook/feedback 1.4.3 → 1.6.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/dist/redact.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { maskSecrets } from './secrets.js';
1
2
  /**
2
3
  * Redaction, applied before anything leaves the device.
3
4
  *
@@ -63,14 +64,18 @@ export function describeElement(element) {
63
64
  /**
64
65
  * Console and error text can contain interpolated user data. Long numeric runs, anything that looks
65
66
  * like an email, and bearer tokens are the three that show up in practice.
67
+ *
68
+ * Then every credential secrets.ts recognises on the page (PM-T-0682): a request logged with its
69
+ * api_key, a GitHub token in an error message. Read a little past the cap, so a key the cap would cut
70
+ * in half is still whole when it is looked for.
66
71
  */
67
72
  export function redactText(text) {
68
- return text
73
+ const replaced = text
69
74
  .replace(/[\w.+-]+@[\w-]+\.[\w.]+/g, '[email]')
70
75
  .replace(/\b[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g, '[jwt]')
71
76
  .replace(/\bBearer\s+[\w.-]+/gi, 'Bearer [redacted]')
72
- .replace(/\b\d{12,19}\b/g, '[number]')
73
- .slice(0, 500);
77
+ .replace(/\b\d{12,19}\b/g, '[number]');
78
+ return maskSecrets(replaced.slice(0, 900)).text.slice(0, 500);
74
79
  }
75
80
  /** Selector for elements the host app has marked as never-capture. */
76
81
  export const PRIVATE_ATTRIBUTE = 'data-daybook-private';
@@ -1,3 +1,4 @@
1
+ import type { Box } from './blur.js';
1
2
  /**
2
3
  * A picture of the viewport at the moment of capture.
3
4
  *
@@ -6,23 +7,33 @@
6
7
  * browser's own capture through getDisplayMedia when the host app asks for it, and otherwise records
7
8
  * a structural snapshot — which is less pretty and always correct.
8
9
  *
9
- * Either way, elements marked data-daybook-private are hidden BEFORE the frame is taken. A screenshot is
10
- * the single most likely thing in a report to carry information the reporter never meant to send.
10
+ * Either way, elements marked data-daybook-private are hidden BEFORE the frame is taken, and so are
11
+ * credentials nobody marked and whatever the reporter blurred (PM-T-0682). A screenshot is the single
12
+ * most likely thing in a report to carry information the reporter never meant to send.
11
13
  */
12
14
  export interface Screenshot {
13
15
  blob: Blob;
14
16
  filename: string;
15
17
  contentType: string;
18
+ /** How many credentials were on screen and covered in the picture. Said, so it can be counted. */
19
+ secretsMasked: number;
16
20
  }
17
21
  /**
18
22
  * Takes the picture.
19
23
  *
20
- * <paramref name="conceal"/> hides the widget's own UI and returns the undo. It matters more than it
21
- * looks: the composer is a 380px panel sitting over the page, and the reporter ticked "capture the
22
- * screen" precisely because they want what is UNDER it. Without this the screenshot is a picture of
23
- * the form they just filled in, covering the thing they were reporting.
24
+ * <paramref name="conceal"/> hides the widget's own UI, covers the boxes it is given, and returns the
25
+ * undo. It matters more than it looks: the composer is a 380px panel sitting over the page, and the
26
+ * reporter ticked "capture the screen" precisely because they want what is UNDER it. Without this the
27
+ * screenshot is a picture of the form they just filled in, covering the thing they were reporting.
28
+ *
29
+ * The covering is the widget's because the widget owns a layer over the page and this module does
30
+ * not. Drawing a box there touches nothing of the host's — no style, no node, no undo to get wrong —
31
+ * which is the reason the rings are drawn there too.
32
+ *
33
+ * <paramref name="blurred"/> are elements the reporter blurred. They are hidden the way private
34
+ * elements are, and whether or not `redact` is on: that was the developer's choice, this is theirs.
24
35
  */
25
- export declare function captureScreenshot(redact: boolean, conceal?: () => () => void): Promise<Screenshot | null>;
36
+ export declare function captureScreenshot(redact: boolean, conceal?: (cover: Box[]) => () => void, blurred?: Element[]): Promise<Screenshot | null>;
26
37
  /**
27
38
  * Encodes the frame as small as the browser can manage.
28
39
  *
@@ -38,3 +49,22 @@ export declare function captureScreenshot(redact: boolean, conceal?: () => () =>
38
49
  * need a browser, and it is the part with the trap in it.
39
50
  */
40
51
  export declare function encode(canvas: HTMLCanvasElement): Promise<Blob | null>;
52
+ /**
53
+ * Where on screen there is a credential, as boxes in viewport pixels, and how many credentials.
54
+ *
55
+ * The same recognition as the snapshot's (secrets.ts), read in document order so a label in one node
56
+ * marks the value in the next, and adjacent text nodes read as the one run of text they render as.
57
+ * Each finding is mapped back through a Range to the boxes its characters are drawn in — the key, not
58
+ * the paragraph around it, so the rest of the line stays readable in the picture.
59
+ *
60
+ * A field's value has no text node to take a range of, so a field holding one is covered whole. That
61
+ * is the case that matters most: the settings page that shows a new key once, in a read-only input
62
+ * beside a Copy button.
63
+ *
64
+ * What it cannot see, it cannot cover: text painted into a canvas, an image of text, and the inside
65
+ * of another component's shadow root. Those are what a blur pin is for.
66
+ */
67
+ export declare function findSecretsOnScreen(): {
68
+ boxes: Box[];
69
+ count: number;
70
+ };
@@ -1,19 +1,29 @@
1
1
  import { PRIVATE_ATTRIBUTE } from './redact.js';
2
+ import { findSecrets } from './secrets.js';
2
3
  /**
3
4
  * Takes the picture.
4
5
  *
5
- * <paramref name="conceal"/> hides the widget's own UI and returns the undo. It matters more than it
6
- * looks: the composer is a 380px panel sitting over the page, and the reporter ticked "capture the
7
- * screen" precisely because they want what is UNDER it. Without this the screenshot is a picture of
8
- * the form they just filled in, covering the thing they were reporting.
6
+ * <paramref name="conceal"/> hides the widget's own UI, covers the boxes it is given, and returns the
7
+ * undo. It matters more than it looks: the composer is a 380px panel sitting over the page, and the
8
+ * reporter ticked "capture the screen" precisely because they want what is UNDER it. Without this the
9
+ * screenshot is a picture of the form they just filled in, covering the thing they were reporting.
10
+ *
11
+ * The covering is the widget's because the widget owns a layer over the page and this module does
12
+ * not. Drawing a box there touches nothing of the host's — no style, no node, no undo to get wrong —
13
+ * which is the reason the rings are drawn there too.
14
+ *
15
+ * <paramref name="blurred"/> are elements the reporter blurred. They are hidden the way private
16
+ * elements are, and whether or not `redact` is on: that was the developer's choice, this is theirs.
9
17
  */
10
- export async function captureScreenshot(redact, conceal = () => () => { }) {
18
+ export async function captureScreenshot(redact, conceal = () => () => { }, blurred = []) {
11
19
  if (typeof navigator.mediaDevices?.getDisplayMedia !== 'function')
12
20
  return null;
13
- const restore = redact ? hidePrivate() : () => { };
21
+ const restore = hidePrivate(redact, blurred);
22
+ const scan = () => (redact ? findSecretsOnScreen() : { boxes: [], count: 0 });
14
23
  // Hidden before the picker opens, not after: on the browsers that skip the picker entirely for a
15
24
  // current-tab capture there is no gap between the two, and the frame would still contain us.
16
- const reveal = conceal();
25
+ let found = scan();
26
+ let reveal = conceal(found.boxes);
17
27
  let stream = null;
18
28
  try {
19
29
  // Steered as hard as the platform allows at the tab this widget is running in — that is the
@@ -36,9 +46,17 @@ export async function captureScreenshot(redact, conceal = () => () => { }) {
36
46
  const track = stream.getVideoTracks()[0];
37
47
  if (!track)
38
48
  return null;
49
+ // Looked for again and covered again now that the share is granted. The picker stays up for as
50
+ // long as the reporter takes over it, and the page can scroll or re-render beneath it in that
51
+ // time — a cover left where a key USED to be is a cover over nothing. Undone and redone in one
52
+ // synchronous step, so no frame is painted in between with the page bare.
53
+ found = scan();
54
+ reveal();
55
+ reveal = conceal(found.boxes);
39
56
  // A frame arrives a moment after the track opens; without this the capture is often blank.
40
57
  await new Promise(resolve => setTimeout(resolve, 250));
41
- return await grabFrame(stream);
58
+ const shot = await grabFrame(stream);
59
+ return shot ? { ...shot, secretsMasked: found.count } : null;
42
60
  }
43
61
  catch {
44
62
  // Declining the share prompt is a normal outcome, not an error worth surfacing — the report
@@ -130,11 +148,16 @@ function toBlob(canvas, type, quality) {
130
148
  * Blurs anything the host app marked private, and every password field whether it was marked or not.
131
149
  * Returns the undo, which the caller must run — a page left permanently blurred because a capture
132
150
  * threw would be a worse bug than the one being reported.
151
+ *
152
+ * The reporter's own blurred elements go the same way, with redaction on or off. The widget also
153
+ * covers them in its layer, which is what the reporter saw while writing; this is the belt to that
154
+ * pair of braces, for the content of an element that overflows its own box.
133
155
  */
134
- function hidePrivate() {
156
+ function hidePrivate(redact, blurred) {
135
157
  const targets = [
136
- ...document.querySelectorAll(`[${PRIVATE_ATTRIBUTE}]`),
137
- ...document.querySelectorAll('input[type="password"]'),
158
+ ...(redact ? document.querySelectorAll(`[${PRIVATE_ATTRIBUTE}]`) : []),
159
+ ...(redact ? document.querySelectorAll('input[type="password"]') : []),
160
+ ...blurred.filter((element) => 'style' in element),
138
161
  ];
139
162
  const previous = targets.map(element => element.style.filter);
140
163
  targets.forEach(element => { element.style.filter = 'blur(12px)'; });
@@ -144,3 +167,106 @@ function hidePrivate() {
144
167
  });
145
168
  };
146
169
  }
170
+ /** Whose subtrees are not page text worth reading: code, styles, and markup that never renders. */
171
+ const UNREAD = new Set(['script', 'style', 'noscript', 'template']);
172
+ /** Fields whose value is drawn on screen as text. Password fields are already blurred whole. */
173
+ const SHOWN = /^(?:text|search|url|email|tel|number|)$/;
174
+ // NodeFilter's constants, by value, so this module does not need the global to exist to load.
175
+ const SHOW_ELEMENT = 1;
176
+ const SHOW_TEXT = 4;
177
+ const FILTER_ACCEPT = 1;
178
+ const FILTER_REJECT = 2;
179
+ /**
180
+ * Where on screen there is a credential, as boxes in viewport pixels, and how many credentials.
181
+ *
182
+ * The same recognition as the snapshot's (secrets.ts), read in document order so a label in one node
183
+ * marks the value in the next, and adjacent text nodes read as the one run of text they render as.
184
+ * Each finding is mapped back through a Range to the boxes its characters are drawn in — the key, not
185
+ * the paragraph around it, so the rest of the line stays readable in the picture.
186
+ *
187
+ * A field's value has no text node to take a range of, so a field holding one is covered whole. That
188
+ * is the case that matters most: the settings page that shows a new key once, in a read-only input
189
+ * beside a Copy button.
190
+ *
191
+ * What it cannot see, it cannot cover: text painted into a canvas, an image of text, and the inside
192
+ * of another component's shadow root. Those are what a blur pin is for.
193
+ */
194
+ export function findSecretsOnScreen() {
195
+ const boxes = [];
196
+ let count = 0;
197
+ const root = document.body;
198
+ if (!root)
199
+ return { boxes, count };
200
+ const walker = document.createTreeWalker(root, SHOW_ELEMENT | SHOW_TEXT, {
201
+ acceptNode: node => (node.nodeType === 1 && UNREAD.has(node.localName)
202
+ ? FILTER_REJECT
203
+ : FILTER_ACCEPT),
204
+ });
205
+ let before = '';
206
+ let node = walker.nextNode();
207
+ while (node) {
208
+ if (node.nodeType === 3) {
209
+ const run = [node];
210
+ node = walker.nextNode();
211
+ while (node && node.nodeType === 3 && node.previousSibling === run[run.length - 1]) {
212
+ run.push(node);
213
+ node = walker.nextNode();
214
+ }
215
+ const text = run.map(part => part.data).join('');
216
+ for (const secret of findSecrets(text, before)) {
217
+ const drawn = onScreen(rangeBoxes(run, secret.start, secret.end));
218
+ if (drawn.length === 0)
219
+ continue;
220
+ boxes.push(...drawn);
221
+ count += 1;
222
+ }
223
+ if (text.trim())
224
+ before = `${text.replace(/\s+/g, ' ').slice(-48)} `;
225
+ continue;
226
+ }
227
+ const element = node;
228
+ if (element.localName === 'textarea'
229
+ || (element.localName === 'input' && SHOWN.test(element.type))) {
230
+ const field = element;
231
+ // What labels it, read as the text in front of its value: "API key" before a field that
232
+ // holds forty hex characters is the same label-and-value as it is in running text.
233
+ const label = field.getAttribute('aria-label') || field.name || field.id || '';
234
+ if (field.value && findSecrets(field.value, `${before}${label} `).length > 0) {
235
+ const drawn = onScreen([field.getBoundingClientRect()]);
236
+ if (drawn.length > 0) {
237
+ boxes.push(...drawn);
238
+ count += 1;
239
+ }
240
+ }
241
+ }
242
+ node = walker.nextNode();
243
+ }
244
+ return { boxes, count };
245
+ }
246
+ /** The boxes characters [start, end) of a run of text nodes are drawn in. */
247
+ function rangeBoxes(run, start, end) {
248
+ const range = document.createRange();
249
+ let offset = 0;
250
+ let started = false;
251
+ for (const part of run) {
252
+ const length = part.data.length;
253
+ if (!started && start < offset + length) {
254
+ range.setStart(part, start - offset);
255
+ started = true;
256
+ }
257
+ if (started && end <= offset + length) {
258
+ range.setEnd(part, end - offset);
259
+ break;
260
+ }
261
+ offset += length;
262
+ }
263
+ return Array.from(range.getClientRects(), rect => ({
264
+ left: rect.left, top: rect.top, width: rect.width, height: rect.height,
265
+ }));
266
+ }
267
+ /** Only what is in the picture: a box with no size or off the edge of the viewport covers nothing. */
268
+ function onScreen(rects) {
269
+ return rects.filter(rect => rect.width > 0 && rect.height > 0
270
+ && rect.left < window.innerWidth && rect.left + rect.width > 0
271
+ && rect.top < window.innerHeight && rect.top + rect.height > 0);
272
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Credentials on the page, found rather than marked. PM-T-0682.
3
+ *
4
+ * data-daybook-private covers what the integrating developer foresaw. What nobody foresees is the
5
+ * settings screen that shows a freshly created API key once, the admin page with a webhook URL that
6
+ * carries its token, the debug panel that prints the session's JWT — and the reporter, who is the one
7
+ * looking at it, rarely thinks of the key as part of "the page" they are reporting. So the text and
8
+ * attributes that would leave in a report are read for credentials first, and whatever is found is
9
+ * replaced in the snapshot and covered in the screenshot before anything is sent.
10
+ *
11
+ * <h3>Recognised by shape, and only by shapes that are hard to hit by accident</h3>
12
+ *
13
+ * A credential with a vendor prefix is matched on its own: pmk_, ghp_, xoxb-, AKIA and a
14
+ * PRIVATE KEY block are not things prose or markup contains for any other reason. Everything else
15
+ * needs two things at once — a label saying it is a secret (key, token, secret, password, api…)
16
+ * directly in front of it, and a value that looks generated rather than written. Either alone is
17
+ * ordinary page content: "API key" is a heading, and forty hex characters are a commit.
18
+ *
19
+ * The false positive matters as much as the miss. A report about a commit page whose hashes have all
20
+ * become [masked] is a report about nothing, and a rule that fires on product copy teaches everybody
21
+ * to turn redaction off, which is the one outcome worse than a miss. Every rule here has a test for
22
+ * what it must leave alone beside the test for what it must catch.
23
+ *
24
+ * Pure string work, so it runs the same on the snapshot, the screenshot and the console, and can be
25
+ * tested without a browser.
26
+ */
27
+ /** One credential, as a range of the text it was found in. */
28
+ export interface Secret {
29
+ start: number;
30
+ end: number;
31
+ /** What it looked like — "GitHub token" — which says what was there without saying what it was. */
32
+ kind: string;
33
+ }
34
+ /**
35
+ * Whether a name — an attribute's, a field's — is one of those labels. For the snapshot, which reads
36
+ * `<meta name="csrf-token" content="…">` as a labelled value although the label is in another
37
+ * attribute.
38
+ */
39
+ export declare const LABEL: RegExp;
40
+ /**
41
+ * Whether a labelled value looks like it was generated rather than written.
42
+ *
43
+ * Hex, from 24 characters, with both letters and digits: an md5, a sha, a 128-bit token. All digits
44
+ * is a number and all letters is a word.
45
+ *
46
+ * Otherwise base64 or similar, judged on its letters and digits alone, and on two things written
47
+ * names do and random characters do not. Words run lowercase letters together: in random base62 or
48
+ * base64 a median of 15% of the characters sit in a run of three or more, in a PascalCase name or a
49
+ * dotted path 55 to 90% do, and half is the line. And random characters change class — lower, upper,
50
+ * digit — between about six neighbours in ten, where SHOUTING_CONSTANTS and long runs of one case
51
+ * barely change at all; under 35 in a hundred is written. Both cases have to be present, and a digit:
52
+ * twenty random characters lack one about one time in thirty, and a name — `db.ApiKeys.AsNoTracking`,
53
+ * `Leaf.GetECDsaPublicKey`, whose acronyms defeat the other two measures — nearly always does.
54
+ *
55
+ * Measured, not guessed: across 200,000 random strings each, the run and change tests reject about
56
+ * one in fifty 20-character values and one in two hundred 32-character ones, and the digit roughly
57
+ * doubles that. The miss is the price of leaving code samples and names alone, and real secrets are
58
+ * mostly longer than twenty. Run over this repository's own front end, docs and backend — 300,000
59
+ * lines full of "key" and "token" — the rules found one thing, `ANALYTICS_KEY = 'dak_…'`, which is a
60
+ * key.
61
+ *
62
+ * Never a UUID, even after "key". UUIDs are identifiers — a request id, an idempotency key, the id in
63
+ * a URL — and masking every one of them would blank out half of what a triager reads. The cost is
64
+ * the occasional service whose API keys are UUIDs, and that cost is accepted.
65
+ */
66
+ export declare function looksGenerated(value: string, minimum?: number): boolean;
67
+ /**
68
+ * Every credential in `text`.
69
+ *
70
+ * `before` is what came just before it — the end of the previous text node, or the attribute's name —
71
+ * and is read but never part of the result. It is how `<dt>API key</dt><dd>3f9a…</dd>` is caught:
72
+ * the label and the value are in different nodes, and neither is a secret on its own. A match that
73
+ * starts in `before` and runs into `text` is cut at the boundary, so a key split across two nodes
74
+ * has its second half masked even though the first half has already been written.
75
+ */
76
+ export declare function findSecrets(text: string, before?: string): Secret[];
77
+ /**
78
+ * `text` with every credential replaced by a marker naming what it was, and how many there were.
79
+ *
80
+ * The marker says the kind and nothing else: "[masked: GitHub token]" tells a triager that the page
81
+ * was showing a token, which may be the bug, without handing them the token.
82
+ */
83
+ export declare function maskSecrets(text: string, before?: string): {
84
+ text: string;
85
+ count: number;
86
+ };
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Credentials on the page, found rather than marked. PM-T-0682.
3
+ *
4
+ * data-daybook-private covers what the integrating developer foresaw. What nobody foresees is the
5
+ * settings screen that shows a freshly created API key once, the admin page with a webhook URL that
6
+ * carries its token, the debug panel that prints the session's JWT — and the reporter, who is the one
7
+ * looking at it, rarely thinks of the key as part of "the page" they are reporting. So the text and
8
+ * attributes that would leave in a report are read for credentials first, and whatever is found is
9
+ * replaced in the snapshot and covered in the screenshot before anything is sent.
10
+ *
11
+ * <h3>Recognised by shape, and only by shapes that are hard to hit by accident</h3>
12
+ *
13
+ * A credential with a vendor prefix is matched on its own: pmk_, ghp_, xoxb-, AKIA and a
14
+ * PRIVATE KEY block are not things prose or markup contains for any other reason. Everything else
15
+ * needs two things at once — a label saying it is a secret (key, token, secret, password, api…)
16
+ * directly in front of it, and a value that looks generated rather than written. Either alone is
17
+ * ordinary page content: "API key" is a heading, and forty hex characters are a commit.
18
+ *
19
+ * The false positive matters as much as the miss. A report about a commit page whose hashes have all
20
+ * become [masked] is a report about nothing, and a rule that fires on product copy teaches everybody
21
+ * to turn redaction off, which is the one outcome worse than a miss. Every rule here has a test for
22
+ * what it must leave alone beside the test for what it must catch.
23
+ *
24
+ * Pure string work, so it runs the same on the snapshot, the screenshot and the console, and can be
25
+ * tested without a browser.
26
+ */
27
+ /** The words that say the value beside them is a credential. Part of a longer name counts. */
28
+ const LABELS = 'key|token|secret|passw(?:or)?d|pwd|api|auth|credential|signature';
29
+ /**
30
+ * Whether a name — an attribute's, a field's — is one of those labels. For the snapshot, which reads
31
+ * `<meta name="csrf-token" content="…">` as a labelled value although the label is in another
32
+ * attribute.
33
+ */
34
+ export const LABEL = new RegExp(LABELS, 'i');
35
+ /**
36
+ * The rules, most specific first. Where two find the same characters — a JWT after "Bearer" — the
37
+ * earlier one names it.
38
+ *
39
+ * No lookbehind anywhere: a regex literal Safari before 16.4 cannot parse is a SyntaxError when the
40
+ * bundle loads, which would take the whole widget down on those browsers rather than one rule.
41
+ */
42
+ const RULES = [
43
+ // Daybook's own API key: the marker and 64 hex characters (ApiKeyCommands), recognisable on purpose.
44
+ //
45
+ // From sixteen, not from 64, here and in the other prefixed rules below: text is sometimes clipped
46
+ // before it is read — a pin's text is eighty characters — and a key cut to sixty hex characters no
47
+ // longer has its full shape but is still almost all of a key. The prefix is what makes these
48
+ // specific, not the length.
49
+ { kind: 'Daybook API key', pattern: /\bpmk_[0-9a-f]{16,}/gi },
50
+ // Stripe secret and restricted keys. The publishable pk_ is meant to be on a page and is left alone.
51
+ { kind: 'Stripe key', pattern: /\b[sr]k_(?:live|test)_[A-Za-z0-9]{16,}/g },
52
+ // OpenAI's and Anthropic's shape: sk- and a long generated body, sometimes split by a project or
53
+ // version segment (sk-proj-…, sk-ant-api03-…). The body has to contain a long run of mixed letters
54
+ // and digits, because sk- is also a CSS prefix: SpinKit's spinners are sk-fading-circle and
55
+ // sk-chase-dot, and nothing in those is generated.
56
+ { kind: 'secret key', pattern: /\bsk-[A-Za-z0-9_-]{20,}/g, accept: hasGeneratedRun },
57
+ // GitHub: classic tokens (ghp_, and gho_, ghu_, ghs_, ghr_ for the other grants) and fine-grained ones.
58
+ { kind: 'GitHub token', pattern: /\bgh[pousr]_[A-Za-z0-9]{20,}/g },
59
+ { kind: 'GitHub token', pattern: /\bgithub_pat_[A-Za-z0-9_]{22,}/g },
60
+ // Slack bot, user, app-level, refresh and legacy tokens.
61
+ { kind: 'Slack token', pattern: /\bxox[baprs]-[A-Za-z0-9-]{10,}/g },
62
+ // AWS access key ids, long-lived (AKIA) and temporary (ASIA). Exactly twenty, bounded both sides,
63
+ // because a longer run of capitals that happens to contain AKIA is something else.
64
+ { kind: 'AWS access key', pattern: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g },
65
+ // A PEM private key, through to its END line — or to the end of the text, when the text was cut
66
+ // before it got there.
67
+ {
68
+ kind: 'private key',
69
+ pattern: /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
70
+ },
71
+ // A JWT: base64url JSON header and payload, both of which therefore begin "eyJ". The signature is
72
+ // optional — empty for an unsigned token, and missing from one whose text was clipped — because two
73
+ // dotted segments that both start "eyJ" are a token's whether or not the third survived.
74
+ { kind: 'JWT', pattern: /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}(?:\.[A-Za-z0-9_-]*)?/g },
75
+ // An Authorization value. "Bearer token" and "Basic authentication" are how documentation talks
76
+ // about these, so the value has to look generated, not merely follow the word.
77
+ {
78
+ kind: 'token',
79
+ pattern: /\b(?:Bearer|Basic)\s+([A-Za-z0-9+/_.~-]{16,}=*)/gi,
80
+ value: 1,
81
+ accept: value => looksGenerated(value, 16),
82
+ },
83
+ // A generated value right after a label that says what it is: api_key=…, "token": "…", Secret: …,
84
+ // X-Api-Key: …, "your password is …". The label can be part of a longer name (access_token,
85
+ // client_secret, apiKey). A separator is required — a colon, an equals sign, whitespace, or "is" —
86
+ // so a name with a long tail never has its own tail mistaken for the value, and a value is always
87
+ // masked whole rather than from wherever a label happened to end.
88
+ {
89
+ kind: 'secret',
90
+ pattern: new RegExp(`(?:${LABELS})[A-Za-z0-9_.-]*`
91
+ + `["']?(?:\\s*(?:[:=]>?|\\s(?:is|was)\\s)\\s*|\\s+)["']?`
92
+ + '([A-Za-z0-9+/_.~-]{20,}=*)', 'gi'),
93
+ value: 1,
94
+ accept: value => looksGenerated(value, 20),
95
+ },
96
+ ];
97
+ /**
98
+ * A cheap first look: every rule needs a run of at least fifteen token characters somewhere, and
99
+ * most text on a page — words, numbers, short labels — has none.
100
+ */
101
+ const CANDIDATE = /[A-Za-z0-9_+/=.~-]{15,}/;
102
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
103
+ const HEX = /^[0-9a-f]+$/i;
104
+ /**
105
+ * Whether a labelled value looks like it was generated rather than written.
106
+ *
107
+ * Hex, from 24 characters, with both letters and digits: an md5, a sha, a 128-bit token. All digits
108
+ * is a number and all letters is a word.
109
+ *
110
+ * Otherwise base64 or similar, judged on its letters and digits alone, and on two things written
111
+ * names do and random characters do not. Words run lowercase letters together: in random base62 or
112
+ * base64 a median of 15% of the characters sit in a run of three or more, in a PascalCase name or a
113
+ * dotted path 55 to 90% do, and half is the line. And random characters change class — lower, upper,
114
+ * digit — between about six neighbours in ten, where SHOUTING_CONSTANTS and long runs of one case
115
+ * barely change at all; under 35 in a hundred is written. Both cases have to be present, and a digit:
116
+ * twenty random characters lack one about one time in thirty, and a name — `db.ApiKeys.AsNoTracking`,
117
+ * `Leaf.GetECDsaPublicKey`, whose acronyms defeat the other two measures — nearly always does.
118
+ *
119
+ * Measured, not guessed: across 200,000 random strings each, the run and change tests reject about
120
+ * one in fifty 20-character values and one in two hundred 32-character ones, and the digit roughly
121
+ * doubles that. The miss is the price of leaving code samples and names alone, and real secrets are
122
+ * mostly longer than twenty. Run over this repository's own front end, docs and backend — 300,000
123
+ * lines full of "key" and "token" — the rules found one thing, `ANALYTICS_KEY = 'dak_…'`, which is a
124
+ * key.
125
+ *
126
+ * Never a UUID, even after "key". UUIDs are identifiers — a request id, an idempotency key, the id in
127
+ * a URL — and masking every one of them would blank out half of what a triager reads. The cost is
128
+ * the occasional service whose API keys are UUIDs, and that cost is accepted.
129
+ */
130
+ export function looksGenerated(value, minimum = 20) {
131
+ const body = value.replace(/=+$/, '');
132
+ if (body.length < minimum)
133
+ return false;
134
+ if (UUID.test(body))
135
+ return false;
136
+ if (HEX.test(body)) {
137
+ return body.length >= 24 && /[0-9]/.test(body) && /[a-f]/i.test(body);
138
+ }
139
+ // Mostly letters and digits: a value that is mostly punctuation is a path or an expression.
140
+ const alnum = body.replace(/[^A-Za-z0-9]/g, '');
141
+ if (alnum.length < minimum - 4)
142
+ return false;
143
+ if (!/[a-z]/.test(alnum) || !/[A-Z]/.test(alnum) || !/[0-9]/.test(alnum))
144
+ return false;
145
+ const wordy = (alnum.match(/[a-z]{3,}/g) ?? []).reduce((total, run) => total + run.length, 0);
146
+ if (wordy / alnum.length >= 0.5)
147
+ return false;
148
+ let changes = 0;
149
+ for (let i = 1; i < alnum.length; i += 1) {
150
+ if (classOf(alnum.charCodeAt(i)) !== classOf(alnum.charCodeAt(i - 1)))
151
+ changes += 1;
152
+ }
153
+ return changes / (alnum.length - 1) >= 0.35;
154
+ }
155
+ function classOf(code) {
156
+ if (code >= 48 && code <= 57)
157
+ return 0;
158
+ if (code >= 65 && code <= 90)
159
+ return 1;
160
+ return 2;
161
+ }
162
+ /** A run of sixteen or more letters and digits with at least one of each. What an sk- body has. */
163
+ function hasGeneratedRun(value) {
164
+ return (value.match(/[A-Za-z0-9]{16,}/g) ?? []).some(run => /[0-9]/.test(run) && /[A-Za-z]/.test(run));
165
+ }
166
+ /**
167
+ * Every credential in `text`.
168
+ *
169
+ * `before` is what came just before it — the end of the previous text node, or the attribute's name —
170
+ * and is read but never part of the result. It is how `<dt>API key</dt><dd>3f9a…</dd>` is caught:
171
+ * the label and the value are in different nodes, and neither is a secret on its own. A match that
172
+ * starts in `before` and runs into `text` is cut at the boundary, so a key split across two nodes
173
+ * has its second half masked even though the first half has already been written.
174
+ */
175
+ export function findSecrets(text, before = '') {
176
+ const joined = before + text;
177
+ if (!CANDIDATE.test(joined))
178
+ return [];
179
+ const found = [];
180
+ RULES.forEach((rule, index) => {
181
+ rule.pattern.lastIndex = 0;
182
+ for (let match = rule.pattern.exec(joined); match; match = rule.pattern.exec(joined)) {
183
+ const value = rule.value ? match[rule.value] ?? match[0] : match[0];
184
+ const end = match.index + match[0].length;
185
+ const start = end - value.length;
186
+ if (rule.accept && !rule.accept(value)) {
187
+ // Rejected, so look again one character on: a rejected label can sit right in front of the
188
+ // real one, and the match just found has already consumed it.
189
+ rule.pattern.lastIndex = match.index + 1;
190
+ continue;
191
+ }
192
+ if (end > before.length) {
193
+ found.push({
194
+ start: Math.max(start, before.length) - before.length,
195
+ end: end - before.length,
196
+ kind: rule.kind,
197
+ rule: index,
198
+ });
199
+ }
200
+ }
201
+ });
202
+ // Overlaps become one range, named by the most specific rule among them.
203
+ found.sort((a, b) => a.start - b.start || a.rule - b.rule);
204
+ const merged = [];
205
+ for (const secret of found) {
206
+ const last = merged[merged.length - 1];
207
+ if (last && secret.start < last.end) {
208
+ last.end = Math.max(last.end, secret.end);
209
+ if (secret.rule < last.rule) {
210
+ last.kind = secret.kind;
211
+ last.rule = secret.rule;
212
+ }
213
+ }
214
+ else {
215
+ merged.push({ ...secret });
216
+ }
217
+ }
218
+ return merged.map(({ start, end, kind }) => ({ start, end, kind }));
219
+ }
220
+ /**
221
+ * `text` with every credential replaced by a marker naming what it was, and how many there were.
222
+ *
223
+ * The marker says the kind and nothing else: "[masked: GitHub token]" tells a triager that the page
224
+ * was showing a token, which may be the bug, without handing them the token.
225
+ */
226
+ export function maskSecrets(text, before = '') {
227
+ const found = findSecrets(text, before);
228
+ if (found.length === 0)
229
+ return { text, count: 0 };
230
+ let out = '';
231
+ let at = 0;
232
+ for (const secret of found) {
233
+ out += `${text.slice(at, secret.start)}[masked: ${secret.kind}]`;
234
+ at = secret.end;
235
+ }
236
+ return { text: out + text.slice(at), count: found.length };
237
+ }
@@ -1,4 +1,5 @@
1
1
  import type { Anchor } from './types.js';
2
+ import { type Blurred } from './blur.js';
2
3
  /**
3
4
  * The snapshot, and the ORDER OF THESE FIELDS IS LOAD-BEARING.
4
5
  *
@@ -19,6 +20,13 @@ export interface DomSnapshot {
19
20
  title: string;
20
21
  /** Whether values were described rather than reproduced. Says how to read the snapshot. */
21
22
  redacted: boolean;
23
+ /**
24
+ * How many things the reporter blurred, each withheld as data-daybook-blurred. Counts, never
25
+ * content: a reader who meets a marker should know somebody put it there on purpose.
26
+ */
27
+ blurredByReporter: number;
28
+ /** How many credentials were found and replaced with a `[masked: …]` marker. */
29
+ secretsMasked: number;
22
30
  /** True when the markup hit the cap. What is there is still the top of the document. */
23
31
  truncated: boolean;
24
32
  scroll: {
@@ -62,5 +70,9 @@ export interface DomSnapshot {
62
70
  /** Last, deliberately. See the note above the interface. */
63
71
  html: string;
64
72
  }
65
- /** The whole snapshot. Never throws — a report without one beats no report. */
66
- export declare function captureSnapshot(pinned: Anchor[] | Anchor | null, redacted: boolean): DomSnapshot | null;
73
+ /**
74
+ * The whole snapshot. Never throws — a report without one beats no report.
75
+ *
76
+ * `blurred` is what the reporter blurred, and it is applied whatever `redacted` says: see blur.ts.
77
+ */
78
+ export declare function captureSnapshot(pinned: Anchor[] | Anchor | null, redacted: boolean, blurred?: Blurred): DomSnapshot | null;