@daybook/feedback 1.4.3 → 1.5.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/snapshot.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { redactValue, PRIVATE_ATTRIBUTE } from './redact.js';
2
+ import { LABEL, maskSecrets } from './secrets.js';
3
+ import { BLURRED_ATTRIBUTE, inside, overlaps } from './blur.js';
2
4
  /**
3
5
  * The page as a machine can read it: markup, classes, computed style, and what is floating on top.
4
6
  *
@@ -24,6 +26,14 @@ import { redactValue, PRIVATE_ATTRIBUTE } from './redact.js';
24
26
  * Anything under data-daybook-private, which is stripped to a marker rather than emptied, so a
25
27
  * reader can see that something was there and was withheld.
26
28
  *
29
+ * Anything the reporter blurred (PM-T-0682), marked data-daybook-blurred the same way, whether or
30
+ * not redaction is on — it was their decision, made while looking at it.
31
+ *
32
+ * Credentials found in text and attributes, which nobody had to mark: a key on a settings screen, a
33
+ * token in a link's query string, a JWT in a data attribute. They become `[masked: GitHub token]`,
34
+ * which says what kind of thing was there and nothing more. See secrets.ts for what is recognised and,
35
+ * as importantly, what is deliberately left alone.
36
+ *
27
37
  * <h3>What it costs the page</h3>
28
38
  *
29
39
  * One pass over the tree and a getComputedStyle on the anchor's ancestors — microseconds, and it
@@ -39,6 +49,17 @@ const MAX_ATTRIBUTE = 300;
39
49
  const MAX_TEXT = 600;
40
50
  /** Inline stylesheets are worth having; an entire framework inlined into the head is not. */
41
51
  const MAX_STYLE = 8000;
52
+ /**
53
+ * How far past a clip the text is still read for credentials.
54
+ *
55
+ * Masking happens before clipping, not after: a key cut in half by the clip no longer has the shape
56
+ * that finds it, and half a key is still a leak. Reading the whole of a megabyte text node to protect
57
+ * the six hundred characters that are kept would be waste, so the read stops this far past them —
58
+ * further than any credential that could straddle the cut is long.
59
+ */
60
+ const SCAN_PAST = 400;
61
+ /** How much of the text before a node is kept, so a label in one node can mark a value in the next. */
62
+ const CONTEXT = 48;
42
63
  /** Nothing inside these tells a reader anything about the page's state. */
43
64
  const SKIPPED = new Set(['SCRIPT', 'NOSCRIPT', 'TEMPLATE']);
44
65
  /** Style properties that explain how something looks, without dumping all three hundred of them. */
@@ -51,9 +72,10 @@ const INTERESTING = [
51
72
  'transform', 'transition', 'pointer-events', 'cursor',
52
73
  ];
53
74
  const NOTE = 'The page at the moment this was reported. Field values are described rather than '
54
- + 'reproduced when redacted is true, and data-daybook-private subtrees are withheld. html is last '
55
- + 'because only the head of this file is inlined beside the report — download the attachment for '
56
- + 'the rest of it.';
75
+ + 'reproduced when redacted is true, data-daybook-private subtrees are withheld, and credentials '
76
+ + 'found on the page read [masked: kind]. data-daybook-blurred marks what the reporter chose to '
77
+ + 'blur. html is last because only the head of this file is inlined beside the report — download '
78
+ + 'the attachment for the rest of it.';
57
79
  /** A CSS-ish path to an element, good enough to find it again and short enough to read. */
58
80
  function selectorFor(element) {
59
81
  const parts = [];
@@ -72,7 +94,27 @@ function selectorFor(element) {
72
94
  }
73
95
  return parts.join(' > ');
74
96
  }
75
- const attributeValue = (name, value) => value.length > MAX_ATTRIBUTE ? `${value.slice(0, MAX_ATTRIBUTE)}…[${value.length}]` : value;
97
+ /**
98
+ * An attribute's value as it goes into the snapshot: credentials masked, then clipped.
99
+ *
100
+ * The attribute's name is the context a value is read in, so `data-api-key="3f9a…"` and
101
+ * `href="/callback?access_token=…"` are both caught. So is the element's own name or id when that is
102
+ * what labels it — `<meta name="csrf-token" content="…">` says what its content is in the other
103
+ * attribute.
104
+ */
105
+ function attributeValue(walk, element, name, value) {
106
+ let shown = value;
107
+ if (walk.redacted) {
108
+ const label = [element.getAttribute('name'), element.getAttribute('id')]
109
+ .find(candidate => candidate && LABEL.test(candidate)) ?? name;
110
+ const masked = maskSecrets(value.slice(0, MAX_ATTRIBUTE + SCAN_PAST), `${label}=`);
111
+ walk.secrets += masked.count;
112
+ shown = masked.text;
113
+ }
114
+ return value.length > MAX_ATTRIBUTE || shown.length > MAX_ATTRIBUTE
115
+ ? `${shown.slice(0, MAX_ATTRIBUTE)}…[${value.length}]`
116
+ : shown;
117
+ }
76
118
  const escape = (text) => text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
77
119
  /**
78
120
  * The value of a field, as the report is allowed to describe it.
@@ -90,17 +132,75 @@ function fieldValue(element, redacted) {
90
132
  return null;
91
133
  return redacted ? redactValue(value, input.name || input.id || input.type) : value;
92
134
  }
93
- /** Serialises one node, appending to `out`, and answers whether there is still room. */
94
- function write(node, out, size, redacted) {
95
- if (size.used > MAX_HTML)
135
+ /**
136
+ * Text, as the snapshot carries it: whitespace collapsed, credentials masked, then clipped.
137
+ *
138
+ * Adjacent text nodes arrive here together, as one. They render as one run of text, and a framework
139
+ * writes a template and a value as two — `pmk_` and the key, `Bearer ` and the token — so reading
140
+ * them one at a time would hand each half to the reader with nothing to recognise in either.
141
+ */
142
+ function writeText(nodes, walk) {
143
+ const text = nodes.map(node => node.textContent ?? '').join('').replace(/\s+/g, ' ');
144
+ if (!text.trim())
145
+ return;
146
+ if (walk.regions.length > 0 && nodes.some(node => drawnInside(node, walk.regions))) {
147
+ walk.out.push('[withheld]');
148
+ walk.used += 10;
149
+ walk.before = '';
150
+ return;
151
+ }
152
+ let shown = text.length > MAX_TEXT + SCAN_PAST ? text.slice(0, MAX_TEXT + SCAN_PAST) : text;
153
+ if (walk.redacted) {
154
+ const masked = maskSecrets(shown, walk.before);
155
+ walk.secrets += masked.count;
156
+ shown = masked.text;
157
+ }
158
+ // With a space after it, because whatever text comes next is past an element boundary, and that is
159
+ // usually a visual one too: "API key" in a <dt> and the key in the <dd> after it are a label and
160
+ // its value, and should be read that way.
161
+ walk.before = `${text.slice(-CONTEXT)} `;
162
+ const clipped = text.length > MAX_TEXT || shown.length > MAX_TEXT
163
+ ? `${shown.slice(0, MAX_TEXT)}…`
164
+ : shown;
165
+ walk.out.push(escape(clipped));
166
+ walk.used += clipped.length;
167
+ }
168
+ /**
169
+ * Whether a text node is drawn inside an area the reporter blurred.
170
+ *
171
+ * Any part of it, by any of its lines: a paragraph with one word under the blur is withheld whole.
172
+ * The reporter drew a box over what they saw, and text that wraps or runs on past the box is still
173
+ * the text they were hiding.
174
+ */
175
+ function drawnInside(node, regions) {
176
+ const range = document.createRange();
177
+ range.selectNodeContents(node);
178
+ return Array.from(range.getClientRects()).some(rect => regions.some(region => overlaps(rect, region)));
179
+ }
180
+ /**
181
+ * Whether an element is withheld because of an area the reporter blurred.
182
+ *
183
+ * Entirely inside one, it goes whole. Partly inside one, it stays — a card the blur only clipped the
184
+ * corner of is not what was hidden — and the walk goes on into its children to find what was. Unless
185
+ * there is nothing inside it to find: an image, a canvas, or a field, whose value is not a child the
186
+ * walk could withhold on its own. Then the element itself goes. Something that draws no box at all,
187
+ * display:none, was not under anybody's blur.
188
+ */
189
+ function blurredByArea(element, regions) {
190
+ const rect = element.getBoundingClientRect();
191
+ if (rect.width === 0 && rect.height === 0)
192
+ return false;
193
+ const whole = element.childNodes.length === 0 || FIELDS.has(element.localName);
194
+ return regions.some(region => inside(rect, region) || (whole && overlaps(rect, region)));
195
+ }
196
+ /** Elements whose content is a value rather than children. */
197
+ const FIELDS = new Set(['input', 'textarea', 'select']);
198
+ /** Serialises one node, appending to the walk's output, and answers whether there is still room. */
199
+ function write(node, walk) {
200
+ if (walk.used > MAX_HTML)
96
201
  return false;
97
202
  if (node.nodeType === 3) {
98
- const text = (node.textContent ?? '').replace(/\s+/g, ' ');
99
- if (!text.trim())
100
- return true;
101
- const clipped = text.length > MAX_TEXT ? `${text.slice(0, MAX_TEXT)}…` : text;
102
- out.push(escape(clipped));
103
- size.used += clipped.length;
203
+ writeText([node], walk);
104
204
  return true;
105
205
  }
106
206
  if (node.nodeType !== 1)
@@ -114,38 +214,67 @@ function write(node, out, size, redacted) {
114
214
  if (SKIPPED.has(tag))
115
215
  return true;
116
216
  const name = element.localName;
117
- if (redacted && element.hasAttribute(PRIVATE_ATTRIBUTE)) {
118
- out.push(`<${name} ${PRIVATE_ATTRIBUTE}>[withheld]</${name}>`);
119
- size.used += 32;
217
+ if (walk.redacted && element.hasAttribute(PRIVATE_ATTRIBUTE)) {
218
+ walk.out.push(`<${name} ${PRIVATE_ATTRIBUTE}>[withheld]</${name}>`);
219
+ walk.used += 32;
220
+ walk.before = '';
221
+ return true;
222
+ }
223
+ if (walk.blurred.has(element) || (walk.regions.length > 0 && blurredByArea(element, walk.regions))) {
224
+ walk.out.push(`<${name} ${BLURRED_ATTRIBUTE}>[withheld]</${name}>`);
225
+ walk.used += 32;
226
+ walk.before = '';
120
227
  return true;
121
228
  }
122
229
  const attributes = [];
123
230
  for (const attribute of Array.from(element.attributes)) {
124
231
  if (attribute.name === 'value')
125
232
  continue;
126
- attributes.push(`${attribute.name}="${escape(attributeValue(attribute.name, attribute.value))}"`);
233
+ const shown = attributeValue(walk, element, attribute.name, attribute.value);
234
+ attributes.push(`${attribute.name}="${escape(shown)}"`);
127
235
  }
128
- const value = fieldValue(element, redacted);
236
+ const value = fieldValue(element, walk.redacted);
129
237
  if (value !== null)
130
238
  attributes.push(`value="${escape(value)}"`);
131
239
  const open = attributes.length > 0 ? `<${name} ${attributes.join(' ')}>` : `<${name}>`;
132
- out.push(open);
133
- size.used += open.length;
240
+ walk.out.push(open);
241
+ walk.used += open.length;
134
242
  if (name === 'style') {
135
243
  const css = element.textContent ?? '';
136
- const clipped = css.length > MAX_STYLE ? `${css.slice(0, MAX_STYLE)}\n/* …${css.length} chars */` : css;
137
- out.push(clipped);
138
- size.used += clipped.length;
244
+ let shown = css.slice(0, MAX_STYLE + SCAN_PAST);
245
+ // A url() in a stylesheet can be a signed link as easily as an href can.
246
+ if (walk.redacted) {
247
+ const masked = maskSecrets(shown);
248
+ walk.secrets += masked.count;
249
+ shown = masked.text;
250
+ }
251
+ const clipped = css.length > MAX_STYLE || shown.length > MAX_STYLE
252
+ ? `${shown.slice(0, MAX_STYLE)}\n/* …${css.length} chars */`
253
+ : shown;
254
+ walk.out.push(clipped);
255
+ walk.used += clipped.length;
139
256
  }
140
257
  else {
141
- for (const child of Array.from(element.childNodes)) {
142
- if (!write(child, out, size, redacted))
258
+ const children = Array.from(element.childNodes);
259
+ for (let index = 0; index < children.length; index += 1) {
260
+ const child = children[index];
261
+ if (child.nodeType === 3) {
262
+ // The whole run of adjacent text, as one — see writeText.
263
+ const run = [child];
264
+ while (children[index + 1]?.nodeType === 3)
265
+ run.push(children[++index]);
266
+ writeText(run, walk);
267
+ if (walk.used > MAX_HTML)
268
+ break;
269
+ continue;
270
+ }
271
+ if (!write(child, walk))
143
272
  break;
144
273
  }
145
274
  }
146
- out.push(`</${name}>`);
147
- size.used += name.length + 3;
148
- return size.used <= MAX_HTML;
275
+ walk.out.push(`</${name}>`);
276
+ walk.used += name.length + 3;
277
+ return walk.used <= MAX_HTML;
149
278
  }
150
279
  /** Whatever is floating over the page: our own furniture excluded, since we know about ours. */
151
280
  function layers() {
@@ -209,21 +338,47 @@ function stylesheets() {
209
338
  }
210
339
  });
211
340
  }
212
- /** The whole snapshot. Never throws — a report without one beats no report. */
213
- export function captureSnapshot(pinned, redacted) {
341
+ /**
342
+ * The whole snapshot. Never throws — a report without one beats no report.
343
+ *
344
+ * `blurred` is what the reporter blurred, and it is applied whatever `redacted` says: see blur.ts.
345
+ */
346
+ export function captureSnapshot(pinned, redacted, blurred = { elements: [], regions: [] }) {
214
347
  // One anchor or none is what every caller before multi-pin passed, and still means the same.
215
348
  const anchors = pinned == null ? [] : Array.isArray(pinned) ? pinned : [pinned];
216
349
  try {
217
- const out = [];
218
- const size = { used: 0 };
219
- write(document.documentElement, out, size, redacted);
350
+ const walk = {
351
+ out: [],
352
+ used: 0,
353
+ redacted,
354
+ blurred: new Set(blurred.elements),
355
+ regions: blurred.regions,
356
+ before: '',
357
+ secrets: 0,
358
+ };
359
+ write(document.documentElement, walk);
360
+ // The address and the title are page text too, and an address is where a token is most often
361
+ // put on purpose — a magic link, an OAuth callback that has not been cleaned up yet.
362
+ const mask = (text) => {
363
+ if (!redacted)
364
+ return text;
365
+ const masked = maskSecrets(text);
366
+ walk.secrets += masked.count;
367
+ return masked.text;
368
+ };
369
+ const url = mask(location.href);
370
+ const title = mask(document.title);
371
+ const sheets = stylesheets()
372
+ .map(sheet => (sheet.href ? { ...sheet, href: mask(sheet.href) } : sheet));
220
373
  return {
221
374
  note: NOTE,
222
375
  capturedAtUtc: new Date().toISOString(),
223
- url: location.href,
224
- title: document.title,
376
+ url,
377
+ title,
225
378
  redacted,
226
- truncated: size.used > MAX_HTML,
379
+ blurredByReporter: blurred.elements.length + blurred.regions.length,
380
+ secretsMasked: walk.secrets,
381
+ truncated: walk.used > MAX_HTML,
227
382
  scroll: { x: Math.round(window.scrollX), y: Math.round(window.scrollY) },
228
383
  focus: document.activeElement && document.activeElement !== document.body
229
384
  ? selectorFor(document.activeElement)
@@ -233,8 +388,8 @@ export function captureSnapshot(pinned, redacted) {
233
388
  ? anchors.map(a => anchorPath(a)).filter((a) => !!a)
234
389
  : undefined,
235
390
  layers: layers(),
236
- stylesheets: stylesheets(),
237
- html: out.join(''),
391
+ stylesheets: sheets,
392
+ html: walk.out.join(''),
238
393
  };
239
394
  }
240
395
  catch {
package/dist/types.d.ts CHANGED
@@ -188,6 +188,15 @@ export interface FeedbackOptions {
188
188
  * Mask input values, strip query strings, and blur elements marked data-daybook-private before any
189
189
  * capture leaves the device. On by default: a screenshot contains whatever was on screen, and the
190
190
  * server cannot undo a leak that has already crossed the wire.
191
+ *
192
+ * It also finds credentials nobody marked (1.5): API keys with a vendor prefix (Daybook, OpenAI and
193
+ * Anthropic sk-, Stripe, GitHub, Slack, AWS), JWTs, bearer tokens, private keys, and generated
194
+ * values right after a label such as key, token, secret or password. They are replaced in the page
195
+ * snapshot, the console lines and a pin's text, and covered in the screenshot. Commit hashes, UUIDs
196
+ * and ordinary words are deliberately left alone.
197
+ *
198
+ * What the reporter blurs is not governed by this. `false` turns off what happens automatically; it
199
+ * does not overrule somebody who pointed at a thing while reporting and said "not this".
191
200
  */
192
201
  redact?: boolean;
193
202
  /** Console errors, failed requests and navigation. On by default, capped and redacted. */
@@ -197,10 +206,11 @@ export interface FeedbackOptions {
197
206
  * and what is floating over the page. On by default, and it needs no permission: it is the page's
198
207
  * own state, read by script already running on it.
199
208
  *
200
- * It obeys `redact`, so field values are described rather than reproduced and anything marked
201
- * data-daybook-private is withheld. What it does carry is the page's visible text, because that is
202
- * what the page is. On a screen showing somebody's personal data, decide whether that belongs in a
203
- * report before leaving this on.
209
+ * It obeys `redact`, so field values are described rather than reproduced, anything marked
210
+ * data-daybook-private is withheld and credentials found on the page are masked. Whatever the
211
+ * reporter blurred is withheld either way. What it does carry is the page's visible text, because
212
+ * that is what the page is. On a screen showing somebody's personal data, decide whether that
213
+ * belongs in a report before leaving this on.
204
214
  */
205
215
  dom?: boolean;
206
216
  /** Keyboard shortcut to enter pick mode. Defaults to "f". Set null to disable. */
package/dist/version.d.ts CHANGED
@@ -6,5 +6,5 @@
6
6
  * npm build is plain tsc and has no bundler to inline it; test/version.test.js and publish-sdk.sh
7
7
  * both refuse a build where this and package.json disagree.
8
8
  */
9
- export declare const SDK_VERSION = "1.4.3";
9
+ export declare const SDK_VERSION = "1.5.0";
10
10
  export declare const SDK_NAME = "js";
package/dist/version.js CHANGED
@@ -6,5 +6,5 @@
6
6
  * npm build is plain tsc and has no bundler to inline it; test/version.test.js and publish-sdk.sh
7
7
  * both refuse a build where this and package.json disagree.
8
8
  */
9
- export const SDK_VERSION = '1.4.3';
9
+ export const SDK_VERSION = '1.5.0';
10
10
  export const SDK_NAME = 'js';
package/dist/widget.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Anchor, FeedbackTheme, FeedbackOptions } from './types.js';
2
2
  import { type StagedFile } from './files.js';
3
+ import { type Blurred, type Box } from './blur.js';
3
4
  /**
4
5
  * The reporter-facing UI, rendered inside a shadow root.
5
6
  *
@@ -20,6 +21,16 @@ export declare class FeedbackWidget {
20
21
  * more complex feedbacks". One stays the normal answer; the composer offers another.
21
22
  */
22
23
  private pins;
24
+ /**
25
+ * What the reporter has blurred. PM-T-0682.
26
+ *
27
+ * Picked with the pin's own gesture, so there is nothing new to learn: click an element, or — the
28
+ * one thing a pin does not need — drag across an area. Held as pins because they need what a pin
29
+ * needs, an element to follow and a box to fall back on, but kept apart from them: a blur is not
30
+ * what the report is about, is never numbered among the pins, and never goes on the wire. Not even
31
+ * its text — a blur's anchor is recorded without it.
32
+ */
33
+ private blurs;
23
34
  /** Set while the composer is hidden for "Add another pin"; calling it puts the panel back. */
24
35
  private cancelAdding;
25
36
  /** Takes the page markers down when the composer goes. */
@@ -146,8 +157,36 @@ export declare class FeedbackWidget {
146
157
  * the whole reason to do it here rather than on the page: a report used to arrive as a selector,
147
158
  * a text snapshot and a picture of a whole screen, leaving whoever read it to work out which of
148
159
  * forty things on that screen the sentence was about. Now the picture says.
160
+ *
161
+ * The same layer covers what must not be in the picture (PM-T-0682): the reporter's blurs, exactly
162
+ * as they saw them while writing, and `cover` — credentials the capture found on screen, in
163
+ * viewport pixels. Covers go in first and rings after, so a pin on something blurred still shows.
164
+ */
165
+ conceal(cover?: Box[]): () => void;
166
+ /**
167
+ * Where the reporter's blurs are now, for the captures.
168
+ *
169
+ * An element blur hands over the element, so the snapshot can withhold its subtree and the picture
170
+ * can blur it as it does a private one. If the element cannot be found any more — re-rendered,
171
+ * its selector stale — the box it was picked in goes over as an area instead. A blur that quietly
172
+ * stopped applying because the page re-rendered would be the worst way for this to fail.
149
173
  */
150
- conceal(): () => void;
174
+ blurred(): Blurred;
175
+ /** A pin's element on this page now: the one picked, or what its selector finds. */
176
+ private elementOf;
177
+ /** The reporter's blurs, drawn into `into`. For the page while composing and for the picture. */
178
+ private veilBlurs;
179
+ /**
180
+ * A pin as it may leave the page.
181
+ *
182
+ * Its text is dropped when it is something the report must not describe: a pin on an element the
183
+ * reporter blurred, or on one around it, or on a spot inside an area they blurred — they pointed at
184
+ * the balance and blurred the balance, and the pin's text snapshot would otherwise carry the
185
+ * balance. With redaction on, the same goes for a pin on or around a data-daybook-private element,
186
+ * which the snapshot and the picture already withhold and the anchor did not. The selector stays:
187
+ * it says which element, not what it says.
188
+ */
189
+ private sendable;
151
190
  /**
152
191
  * A ring over the pinned element, drawn in our own shadow root.
153
192
  *
@@ -194,10 +233,16 @@ export declare class FeedbackWidget {
194
233
  * three pins the reporter cannot tell which number is which without them. Drawn in the layer, so
195
234
  * the capture's conceal takes them away and the rings, which are the picture's version of the
196
235
  * same thing, go in instead.
236
+ *
237
+ * The blurs are drawn here as well, and drawn the way the picture will have them: the reporter
238
+ * decides what is safe to send by looking at it, so what they look at has to be what is sent.
197
239
  */
198
240
  private markPins;
199
241
  private redrawMarks;
200
- /** The picker over a hidden composer, for one more pin. Esc or a click puts the panel back. */
242
+ /**
243
+ * The picker over a hidden composer, for one more pin — or, with `blur`, for something to blur.
244
+ * Esc or a pick puts the panel back.
245
+ */
201
246
  private pickAnother;
202
247
  /**
203
248
  * Where the reporter dragged the panel to, if they did — for this report only.
@@ -279,6 +324,11 @@ export declare class FeedbackWidget {
279
324
  /**
280
325
  * The crosshair over the page. The first pin opens the composer; "Add another pin" passes
281
326
  * `onPin` and gets the new pin back instead, with the composer left exactly as it was.
327
+ *
328
+ * With `blur` it picks something to blur (PM-T-0682), and takes a drag as well as a click: an
329
+ * element is the right unit for a card or a field, and the wrong one for half a table, a corner of
330
+ * a chart or a canvas that has no elements inside it at all. What a click would blur is shown
331
+ * blurred under the pointer before the click, and a drag shows its area blurred as it is drawn.
282
332
  */
283
333
  private picker;
284
334
  private composer;