@navikt/apm 0.0.0-stage → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +527 -2
  3. package/dist/api.d.ts +108 -0
  4. package/dist/api.d.ts.map +1 -0
  5. package/dist/api.js +220 -0
  6. package/dist/api.js.map +1 -0
  7. package/dist/config.d.ts +150 -0
  8. package/dist/config.d.ts.map +1 -0
  9. package/dist/config.js +268 -0
  10. package/dist/config.js.map +1 -0
  11. package/dist/console.d.ts +28 -0
  12. package/dist/console.d.ts.map +1 -0
  13. package/dist/console.js +71 -0
  14. package/dist/console.js.map +1 -0
  15. package/dist/errors.d.ts +40 -0
  16. package/dist/errors.d.ts.map +1 -0
  17. package/dist/errors.js +177 -0
  18. package/dist/errors.js.map +1 -0
  19. package/dist/feedback.d.ts +42 -0
  20. package/dist/feedback.d.ts.map +1 -0
  21. package/dist/feedback.js +72 -0
  22. package/dist/feedback.js.map +1 -0
  23. package/dist/index.d.ts +144 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +249 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/internal.d.ts +47 -0
  28. package/dist/internal.d.ts.map +1 -0
  29. package/dist/internal.js +129 -0
  30. package/dist/internal.js.map +1 -0
  31. package/dist/metaTags.d.ts +34 -0
  32. package/dist/metaTags.d.ts.map +1 -0
  33. package/dist/metaTags.js +76 -0
  34. package/dist/metaTags.js.map +1 -0
  35. package/dist/react/ErrorBoundary.d.ts +59 -0
  36. package/dist/react/ErrorBoundary.d.ts.map +1 -0
  37. package/dist/react/ErrorBoundary.js +92 -0
  38. package/dist/react/ErrorBoundary.js.map +1 -0
  39. package/dist/react/NaisMetaTags.d.ts +25 -0
  40. package/dist/react/NaisMetaTags.d.ts.map +1 -0
  41. package/dist/react/NaisMetaTags.js +6 -0
  42. package/dist/react/NaisMetaTags.js.map +1 -0
  43. package/dist/react/index.d.ts +17 -0
  44. package/dist/react/index.d.ts.map +1 -0
  45. package/dist/react/index.js +14 -0
  46. package/dist/react/index.js.map +1 -0
  47. package/dist/react/nextClient.d.ts +25 -0
  48. package/dist/react/nextClient.d.ts.map +1 -0
  49. package/dist/react/nextClient.js +29 -0
  50. package/dist/react/nextClient.js.map +1 -0
  51. package/dist/react/routing.d.ts +56 -0
  52. package/dist/react/routing.d.ts.map +1 -0
  53. package/dist/react/routing.js +89 -0
  54. package/dist/react/routing.js.map +1 -0
  55. package/dist/replay/constants.d.ts +26 -0
  56. package/dist/replay/constants.d.ts.map +1 -0
  57. package/dist/replay/constants.js +24 -0
  58. package/dist/replay/constants.js.map +1 -0
  59. package/dist/replay/events.d.ts +74 -0
  60. package/dist/replay/events.d.ts.map +1 -0
  61. package/dist/replay/events.js +258 -0
  62. package/dist/replay/events.js.map +1 -0
  63. package/dist/replay/hash.d.ts +8 -0
  64. package/dist/replay/hash.d.ts.map +1 -0
  65. package/dist/replay/hash.js +14 -0
  66. package/dist/replay/hash.js.map +1 -0
  67. package/dist/replay/masking.d.ts +89 -0
  68. package/dist/replay/masking.d.ts.map +1 -0
  69. package/dist/replay/masking.js +199 -0
  70. package/dist/replay/masking.js.map +1 -0
  71. package/dist/replay/options.d.ts +52 -0
  72. package/dist/replay/options.d.ts.map +1 -0
  73. package/dist/replay/options.js +66 -0
  74. package/dist/replay/options.js.map +1 -0
  75. package/dist/replay/recording.d.ts +60 -0
  76. package/dist/replay/recording.d.ts.map +1 -0
  77. package/dist/replay/recording.js +120 -0
  78. package/dist/replay/recording.js.map +1 -0
  79. package/dist/replay/snapshot.d.ts +41 -0
  80. package/dist/replay/snapshot.d.ts.map +1 -0
  81. package/dist/replay/snapshot.js +97 -0
  82. package/dist/replay/snapshot.js.map +1 -0
  83. package/dist/replay/transport.d.ts +82 -0
  84. package/dist/replay/transport.d.ts.map +1 -0
  85. package/dist/replay/transport.js +188 -0
  86. package/dist/replay/transport.js.map +1 -0
  87. package/dist/scrub.d.ts +69 -0
  88. package/dist/scrub.d.ts.map +1 -0
  89. package/dist/scrub.js +317 -0
  90. package/dist/scrub.js.map +1 -0
  91. package/dist/serialize.d.ts +15 -0
  92. package/dist/serialize.d.ts.map +1 -0
  93. package/dist/serialize.js +68 -0
  94. package/dist/serialize.js.map +1 -0
  95. package/dist/tracing.d.ts +37 -0
  96. package/dist/tracing.d.ts.map +1 -0
  97. package/dist/tracing.js +70 -0
  98. package/dist/tracing.js.map +1 -0
  99. package/dist/version.d.ts +3 -0
  100. package/dist/version.d.ts.map +1 -0
  101. package/dist/version.js +3 -0
  102. package/dist/version.js.map +1 -0
  103. package/package.json +94 -4
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Events tier — the safe-default session-replay privacy tier
3
+ * (nais/grafana-apm-app#82).
4
+ *
5
+ * Derives a lightweight INTERACTION TIMELINE from DOM events and ships it as
6
+ * ordinary Faro events (`kind=event`, the A2 approach) — there is deliberately
7
+ * NO rrweb import on this path, so no rrweb ever lands in the safe-path bundle,
8
+ * and NO FullSnapshot / DOM node tree is ever produced. Structurally there is
9
+ * nothing to leak beyond URLs, which are run through `scrubUrl` first.
10
+ *
11
+ * Each event carries ONLY:
12
+ * - the target element's tag name and ARIA `role` (never id/class/text),
13
+ * - click coordinates (rounded viewport px),
14
+ * - a timestamp.
15
+ *
16
+ * It NEVER captures text content, input/form values, element ids, or a node
17
+ * tree. The one negative guarantee the events tier makes is the absence of the
18
+ * DOM.
19
+ *
20
+ * Trigger semantics mirror `recording.ts`:
21
+ * - `mode: 'on-error'` (default): the timeline is buffered in a bounded
22
+ * in-memory ring; nothing is sent until the first captured error, at which
23
+ * point the buffer is flushed and the collector switches to streaming.
24
+ * - `mode: 'always'`: every event is pushed as it happens.
25
+ */
26
+ import { scrubUrl } from '../scrub.js';
27
+ import { hashString } from './hash.js';
28
+ /** Faro event names for the events-tier timeline. The plugin reader keys off these. */
29
+ export const EVENTS_NAVIGATION = 'faro.session_events.navigation';
30
+ export const EVENTS_CLICK = 'faro.session_events.click';
31
+ export const EVENTS_RAGE_CLICK = 'faro.session_events.rage_click';
32
+ export const EVENTS_SCROLL = 'faro.session_events.scroll';
33
+ export const EVENTS_ERROR = 'faro.session_events.error';
34
+ /** Rage-click heuristic: N clicks within a small radius and time window. */
35
+ const RAGE_CLICK_COUNT = 3;
36
+ const RAGE_CLICK_RADIUS_PX = 30;
37
+ const RAGE_CLICK_WINDOW_MS = 1000;
38
+ /** Coarse scroll sampling: at most one scroll event this often. */
39
+ const SCROLL_THROTTLE_MS = 500;
40
+ /** Bounded ring buffer for on-error mode (oldest dropped past the cap). */
41
+ const MAX_BUFFERED_EVENTS = 200;
42
+ /**
43
+ * Deterministic per-session sampling: the same session id always lands on the
44
+ * same side of the rate. Mirrors `recording.ts` so a session is either fully
45
+ * captured or not at all. (Kept local to avoid pulling the rrweb-adjacent
46
+ * recording module onto the events path.)
47
+ */
48
+ export function isSessionSampled(sessionId, sampleRate) {
49
+ if (!(sampleRate > 0)) {
50
+ return false;
51
+ }
52
+ if (sampleRate >= 1) {
53
+ return true;
54
+ }
55
+ if (sessionId === undefined || sessionId === '') {
56
+ return Math.random() < sampleRate;
57
+ }
58
+ return hashString(sessionId) / 4294967296 < sampleRate;
59
+ }
60
+ /** Extract the privacy-safe descriptor of an event target: tag + role only. */
61
+ function describeTarget(target) {
62
+ try {
63
+ const element = target;
64
+ if (!element || typeof element.tagName !== 'string') {
65
+ return { tag: 'unknown' };
66
+ }
67
+ const out = { tag: element.tagName.toLowerCase() };
68
+ const role = element.getAttribute?.('role');
69
+ if (role) {
70
+ out.role = role;
71
+ }
72
+ return out;
73
+ }
74
+ catch {
75
+ return { tag: 'unknown' };
76
+ }
77
+ }
78
+ /**
79
+ * Attach an events-tier error breadcrumb to the timeline WITHOUT any DOM node
80
+ * tree: a text-free marker carrying the current (scrubbed) URL and viewport.
81
+ * Used by `screenshotOnError` when the resolved tier is not `dom`.
82
+ */
83
+ export function pushErrorBreadcrumb(push) {
84
+ try {
85
+ if (typeof window === 'undefined') {
86
+ return;
87
+ }
88
+ push(EVENTS_ERROR, {
89
+ url: scrubUrl(window.location?.href ?? ''),
90
+ w: String(window.innerWidth || 0),
91
+ h: String(window.innerHeight || 0),
92
+ t: String(Date.now()),
93
+ });
94
+ }
95
+ catch {
96
+ // A breadcrumb must never break error reporting.
97
+ }
98
+ }
99
+ /**
100
+ * Start the events-tier collector. Returns `undefined` when running outside a
101
+ * browser or the session is sampled out; never throws.
102
+ */
103
+ export function startEventsCollection(options) {
104
+ try {
105
+ if (typeof window === 'undefined' || typeof document === 'undefined') {
106
+ return undefined;
107
+ }
108
+ if (!isSessionSampled(options.sessionId, options.sampleRate ?? 1)) {
109
+ return undefined;
110
+ }
111
+ const now = options.now ?? (() => Date.now());
112
+ const { push } = options;
113
+ let streaming = options.mode === 'always';
114
+ let stopped = false;
115
+ const buffer = [];
116
+ const record = (name, attributes) => {
117
+ if (stopped) {
118
+ return;
119
+ }
120
+ if (streaming) {
121
+ push(name, attributes);
122
+ return;
123
+ }
124
+ buffer.push({ name, attributes });
125
+ if (buffer.length > MAX_BUFFERED_EVENTS) {
126
+ buffer.shift();
127
+ }
128
+ };
129
+ const emitNavigation = () => {
130
+ record(EVENTS_NAVIGATION, {
131
+ url: scrubUrl(window.location?.href ?? ''),
132
+ t: String(now()),
133
+ });
134
+ };
135
+ // Rage-click detector: clicks kept only while within the radius + window of
136
+ // the latest click; once the streak reaches the threshold, emit one derived
137
+ // event and reset.
138
+ let clickStreak = [];
139
+ let lastClickTs = 0;
140
+ const onClick = (event) => {
141
+ try {
142
+ const mouse = event;
143
+ const x = Math.round(mouse.clientX ?? 0);
144
+ const y = Math.round(mouse.clientY ?? 0);
145
+ const t = now();
146
+ const target = describeTarget(event.target);
147
+ const base = { tag: target.tag, x: String(x), y: String(y), t: String(t) };
148
+ if (target.role) {
149
+ base['role'] = target.role;
150
+ }
151
+ record(EVENTS_CLICK, base);
152
+ if (t - lastClickTs > RAGE_CLICK_WINDOW_MS) {
153
+ clickStreak = [];
154
+ }
155
+ lastClickTs = t;
156
+ clickStreak = clickStreak.filter((c) => Math.hypot(c.x - x, c.y - y) <= RAGE_CLICK_RADIUS_PX);
157
+ clickStreak.push({ x, y });
158
+ if (clickStreak.length >= RAGE_CLICK_COUNT) {
159
+ const rage = { ...base, count: String(clickStreak.length) };
160
+ record(EVENTS_RAGE_CLICK, rage);
161
+ clickStreak = [];
162
+ }
163
+ }
164
+ catch {
165
+ // A single bad event must never break the host app.
166
+ }
167
+ };
168
+ let lastScrollTs = 0;
169
+ const onScroll = () => {
170
+ try {
171
+ const t = now();
172
+ if (t - lastScrollTs < SCROLL_THROTTLE_MS) {
173
+ return;
174
+ }
175
+ lastScrollTs = t;
176
+ record(EVENTS_SCROLL, {
177
+ x: String(Math.round(window.scrollX || 0)),
178
+ y: String(Math.round(window.scrollY || 0)),
179
+ t: String(t),
180
+ });
181
+ }
182
+ catch {
183
+ // ignore
184
+ }
185
+ };
186
+ // Navigation: SPA route changes go through history.pushState/replaceState;
187
+ // back/forward and hash routing fire popstate/hashchange.
188
+ const history = window.history;
189
+ const originalPushState = history?.pushState;
190
+ const originalReplaceState = history?.replaceState;
191
+ if (history && typeof originalPushState === 'function') {
192
+ history.pushState = function patchedPushState(...args) {
193
+ const result = originalPushState.apply(this, args);
194
+ emitNavigation();
195
+ return result;
196
+ };
197
+ }
198
+ if (history && typeof originalReplaceState === 'function') {
199
+ history.replaceState = function patchedReplaceState(...args) {
200
+ const result = originalReplaceState.apply(this, args);
201
+ emitNavigation();
202
+ return result;
203
+ };
204
+ }
205
+ document.addEventListener('click', onClick, true);
206
+ window.addEventListener('popstate', emitNavigation);
207
+ window.addEventListener('hashchange', emitNavigation);
208
+ window.addEventListener('scroll', onScroll, { passive: true });
209
+ // Seed the timeline with the entry URL.
210
+ emitNavigation();
211
+ return {
212
+ get isStreaming() {
213
+ return streaming;
214
+ },
215
+ notifyError() {
216
+ try {
217
+ if (!streaming) {
218
+ streaming = true;
219
+ const buffered = buffer.splice(0, buffer.length);
220
+ for (const item of buffered) {
221
+ push(item.name, item.attributes);
222
+ }
223
+ }
224
+ // Mark the error position in the timeline (text-free).
225
+ pushErrorBreadcrumb(push);
226
+ }
227
+ catch {
228
+ // ignore
229
+ }
230
+ },
231
+ stop() {
232
+ if (stopped) {
233
+ return;
234
+ }
235
+ stopped = true;
236
+ try {
237
+ document.removeEventListener('click', onClick, true);
238
+ window.removeEventListener('popstate', emitNavigation);
239
+ window.removeEventListener('hashchange', emitNavigation);
240
+ window.removeEventListener('scroll', onScroll);
241
+ if (history && typeof originalPushState === 'function') {
242
+ history.pushState = originalPushState;
243
+ }
244
+ if (history && typeof originalReplaceState === 'function') {
245
+ history.replaceState = originalReplaceState;
246
+ }
247
+ }
248
+ catch {
249
+ // ignore
250
+ }
251
+ },
252
+ };
253
+ }
254
+ catch {
255
+ return undefined;
256
+ }
257
+ }
258
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/replay/events.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEvC,uFAAuF;AACvF,MAAM,CAAC,MAAM,iBAAiB,GAAG,gCAAgC,CAAC;AAClE,MAAM,CAAC,MAAM,YAAY,GAAG,2BAA2B,CAAC;AACxD,MAAM,CAAC,MAAM,iBAAiB,GAAG,gCAAgC,CAAC;AAClE,MAAM,CAAC,MAAM,aAAa,GAAG,4BAA4B,CAAC;AAC1D,MAAM,CAAC,MAAM,YAAY,GAAG,2BAA2B,CAAC;AAExD,4EAA4E;AAC5E,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAC3B,MAAM,oBAAoB,GAAG,EAAE,CAAC;AAChC,MAAM,oBAAoB,GAAG,IAAK,CAAC;AAEnC,mEAAmE;A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AC1D,OAAO,CAAC,YAAY,GAAG,oBAAoB,CAAC;oBAC9C,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,SAAS;gBACX,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** Tiny dependency-free string hashing shared by throttling and sampling. */
2
+ /**
3
+ * djb2 (xor variant) over UTF-16 code units, returned as an unsigned 32-bit
4
+ * integer. Not cryptographic — used only for snapshot throttling keys and
5
+ * deterministic per-session sampling.
6
+ */
7
+ export declare function hashString(input: string): number;
8
+ //# sourceMappingURL=hash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../../src/replay/hash.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAE7E;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMhD"}
@@ -0,0 +1,14 @@
1
+ /** Tiny dependency-free string hashing shared by throttling and sampling. */
2
+ /**
3
+ * djb2 (xor variant) over UTF-16 code units, returned as an unsigned 32-bit
4
+ * integer. Not cryptographic — used only for snapshot throttling keys and
5
+ * deterministic per-session sampling.
6
+ */
7
+ export function hashString(input) {
8
+ let hash = 5381;
9
+ for (let i = 0; i < input.length; i++) {
10
+ hash = ((hash << 5) + hash) ^ input.charCodeAt(i);
11
+ }
12
+ return hash >>> 0;
13
+ }
14
+ //# sourceMappingURL=hash.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/replay/hash.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAE7E;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,IAAI,GAAG,IAAI,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,IAAI,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAI,KAAK,CAAC,CAAC;AACpB,CAAC"}
@@ -0,0 +1,89 @@
1
+ /**
2
+ * The NON-OVERRIDABLE privacy floor for all replay capture
3
+ * (nais/grafana-apm-app#58, #67, #82).
4
+ *
5
+ * Everything is masked at serialization time, in the browser, before any byte
6
+ * leaves the user's machine:
7
+ * - every input value is masked (`maskAllInputs`, no exceptions — inputs can
8
+ * never be unmasked, not even with `data-apm-unmask`),
9
+ * - all text is masked (`maskTextSelector: '*'`) except elements inside an
10
+ * explicit `data-apm-unmask` allowlist attribute,
11
+ * - masked text and input values collapse to a FIXED token, not a
12
+ * per-character map — masking is deliberately NOT length- or word-shape
13
+ * preserving (an 11-`*` run reads as a fødselsnummer; see #82 Part 2.4),
14
+ * - media, canvas, iframes, embeds, file inputs and `[data-apm-block]` are
15
+ * always blocked,
16
+ * - images and stylesheets are never inlined.
17
+ *
18
+ * There is deliberately no options path to weaken any of this: the builders
19
+ * only accept extra `block` selectors (privacy can only be tightened) and the
20
+ * returned option fragments are deep-frozen — mutation throws in strict mode.
21
+ *
22
+ * Attribute values are NOT masked here — the pinned fork
23
+ * `@grafana/rrweb@2.0.0-grafana.2` exposes `maskInputFn` but NOT
24
+ * `maskAttributeFn`. The attribute backstop is the payload scrub in
25
+ * `transport.ts` (`scrubReplayEvents`), which runs over the serialized events
26
+ * before gzip. Do not rely on this module for attribute PII.
27
+ */
28
+ export declare const UNMASK_ATTRIBUTE = "data-apm-unmask";
29
+ export declare const BLOCK_ATTRIBUTE = "data-apm-block";
30
+ /**
31
+ * The single token every masked text node and input value collapses to.
32
+ *
33
+ * A fixed constant (not a per-character `*` map) so masking leaks neither the
34
+ * length nor the word/line shape of the original — an 11-char run and a 5-char
35
+ * run mask to the exact same string. The trade-off is a slight replay layout
36
+ * shift (masked runs no longer match the original width); acceptable for a
37
+ * privacy tier.
38
+ */
39
+ export declare const MASKED_TOKEN = "\u2022\u2022\u2022";
40
+ /** Compose the base block list with app-supplied extra selectors (tighten-only). */
41
+ export declare function buildBlockSelector(extraBlock?: readonly string[]): string;
42
+ /**
43
+ * rrweb `maskTextFn`: masks all text unless the owning element carries (or is
44
+ * inside an element carrying) the `data-apm-unmask` allowlist attribute.
45
+ *
46
+ * Masked text collapses to the fixed {@link MASKED_TOKEN} (not a per-character
47
+ * map) so length/word-shape does not leak.
48
+ */
49
+ export declare function maskText(text: string, element: HTMLElement | null): string;
50
+ /**
51
+ * rrweb `maskInputFn`: input values ALWAYS mask (no unmask path — inputs are
52
+ * never allowlisted) and collapse to the fixed {@link MASKED_TOKEN}, so input
53
+ * masking is not length-preserving either.
54
+ */
55
+ export declare function maskInput(value: string, _element: HTMLElement): string;
56
+ /** Masking fragment for `rrweb.record()`. Frozen: attempts to override throw. */
57
+ export interface RecordMaskingOptions {
58
+ readonly maskAllInputs: true;
59
+ readonly maskTextSelector: '*';
60
+ readonly maskTextFn: typeof maskText;
61
+ readonly maskInputFn: typeof maskInput;
62
+ readonly blockSelector: string;
63
+ readonly inlineStylesheet: false;
64
+ readonly inlineImages: false;
65
+ readonly recordCanvas: false;
66
+ readonly collectFonts: false;
67
+ readonly slimDOMOptions: true;
68
+ }
69
+ /** Masking fragment for `rrweb-snapshot`'s `snapshot()`. Frozen. */
70
+ export interface SnapshotMaskingOptions {
71
+ readonly maskAllInputs: true;
72
+ readonly maskTextSelector: '*';
73
+ readonly maskTextFn: typeof maskText;
74
+ readonly maskInputFn: typeof maskInput;
75
+ readonly blockSelector: string;
76
+ readonly inlineStylesheet: false;
77
+ readonly inlineImages: false;
78
+ readonly recordCanvas: false;
79
+ readonly slimDOM: true;
80
+ }
81
+ /**
82
+ * Build the record-time masking floor. `extraBlock` selectors are appended to
83
+ * the base block list; nothing can be relaxed. Spread this LAST into rrweb
84
+ * `record()` options so it always wins.
85
+ */
86
+ export declare function buildRecordMaskingOptions(extraBlock?: readonly string[]): RecordMaskingOptions;
87
+ /** Build the snapshot-time masking floor (same rules, snapshot option names). */
88
+ export declare function buildSnapshotMaskingOptions(extraBlock?: readonly string[]): SnapshotMaskingOptions;
89
+ //# sourceMappingURL=masking.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"masking.d.ts","sourceRoot":"","sources":["../../src/replay/masking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,eAAO,MAAM,gBAAgB,oBAAoB,CAAC;AAClD,eAAO,MAAM,eAAe,mBAAmB,CAAC;AAIhD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,uBAAQ,CAAC;AAuClC,oFAAoF;AACpF,wBAAgB,kBAAkB,CAAC,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAKzE;AAkED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,WAAW,GAAG,IAAI,GAAG,MAAM,CAa1E;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,GAAG,MAAM,CAEtE;AAED,iFAAiF;AACjF,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAC;IAC7B,QAAQ,CAAC,gBAAgB,EAAE,GAAG,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,OAAO,QAAQ,CAAC;IACrC,QAAQ,CAAC,WAAW,EAAE,OAAO,SAAS,CAAC;IACvC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,gBAAgB,EAAE,KAAK,CAAC;IACjC,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;IAC7B,QAAQ,CAAC,cAAc,EAAE,IAAI,CAAC;CAC/B;AAED,oEAAoE;AACpE,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,aAAa,EAAE,IAAI,CAAC;IAC7B,QAAQ,CAAC,gBAAgB,EAAE,GAAG,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,OAAO,QAAQ,CAAC;IACrC,QAAQ,CAAC,WAAW,EAAE,OAAO,SAAS,CAAC;IACvC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,gBAAgB,EAAE,KAAK,CAAC;IACjC,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;IAC7B,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;IAC7B,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;CACxB;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,oBAAoB,CAa9F;AAED,iFAAiF;AACjF,wBAAgB,2BAA2B,CAAC,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,sBAAsB,CAYlG"}
@@ -0,0 +1,199 @@
1
+ /**
2
+ * The NON-OVERRIDABLE privacy floor for all replay capture
3
+ * (nais/grafana-apm-app#58, #67, #82).
4
+ *
5
+ * Everything is masked at serialization time, in the browser, before any byte
6
+ * leaves the user's machine:
7
+ * - every input value is masked (`maskAllInputs`, no exceptions — inputs can
8
+ * never be unmasked, not even with `data-apm-unmask`),
9
+ * - all text is masked (`maskTextSelector: '*'`) except elements inside an
10
+ * explicit `data-apm-unmask` allowlist attribute,
11
+ * - masked text and input values collapse to a FIXED token, not a
12
+ * per-character map — masking is deliberately NOT length- or word-shape
13
+ * preserving (an 11-`*` run reads as a fødselsnummer; see #82 Part 2.4),
14
+ * - media, canvas, iframes, embeds, file inputs and `[data-apm-block]` are
15
+ * always blocked,
16
+ * - images and stylesheets are never inlined.
17
+ *
18
+ * There is deliberately no options path to weaken any of this: the builders
19
+ * only accept extra `block` selectors (privacy can only be tightened) and the
20
+ * returned option fragments are deep-frozen — mutation throws in strict mode.
21
+ *
22
+ * Attribute values are NOT masked here — the pinned fork
23
+ * `@grafana/rrweb@2.0.0-grafana.2` exposes `maskInputFn` but NOT
24
+ * `maskAttributeFn`. The attribute backstop is the payload scrub in
25
+ * `transport.ts` (`scrubReplayEvents`), which runs over the serialized events
26
+ * before gzip. Do not rely on this module for attribute PII.
27
+ */
28
+ export const UNMASK_ATTRIBUTE = 'data-apm-unmask';
29
+ export const BLOCK_ATTRIBUTE = 'data-apm-block';
30
+ const UNMASK_SELECTOR = `[${UNMASK_ATTRIBUTE}]`;
31
+ /**
32
+ * The single token every masked text node and input value collapses to.
33
+ *
34
+ * A fixed constant (not a per-character `*` map) so masking leaks neither the
35
+ * length nor the word/line shape of the original — an 11-char run and a 5-char
36
+ * run mask to the exact same string. The trade-off is a slight replay layout
37
+ * shift (masked runs no longer match the original width); acceptable for a
38
+ * privacy tier.
39
+ */
40
+ export const MASKED_TOKEN = '•••';
41
+ /** Always-blocked elements. Apps can add selectors; these can never be removed. */
42
+ const BASE_BLOCK_SELECTORS = [
43
+ 'img',
44
+ 'picture',
45
+ 'svg image',
46
+ 'video',
47
+ 'audio',
48
+ 'canvas',
49
+ 'iframe',
50
+ 'embed',
51
+ 'object',
52
+ // A chosen filename is PII and is NOT an input *value* rrweb masks, so block
53
+ // the whole control rather than trying to mask it (#82 Part 2.4/2c).
54
+ 'input[type=file]',
55
+ `[${BLOCK_ATTRIBUTE}]`,
56
+ ];
57
+ /**
58
+ * Dev-mode guardrail: element subtrees at/above this many descendant elements
59
+ * are considered "large" for the `data-apm-unmask` blast-radius warning.
60
+ */
61
+ const UNMASK_SUBTREE_WARN_ELEMENTS = 24;
62
+ /** Layout containers whose presence in the unmask ancestry always warrants a warning. */
63
+ const LAYOUT_CONTAINER_TAGS = new Set([
64
+ 'HTML',
65
+ 'BODY',
66
+ 'MAIN',
67
+ 'SECTION',
68
+ 'ARTICLE',
69
+ 'ASIDE',
70
+ 'NAV',
71
+ 'HEADER',
72
+ 'FOOTER',
73
+ 'FORM',
74
+ ]);
75
+ /** Compose the base block list with app-supplied extra selectors (tighten-only). */
76
+ export function buildBlockSelector(extraBlock) {
77
+ const extras = (extraBlock ?? [])
78
+ .map((selector) => (typeof selector === 'string' ? selector.trim() : ''))
79
+ .filter((selector) => selector.length > 0);
80
+ return [...BASE_BLOCK_SELECTORS, ...extras].join(',');
81
+ }
82
+ /**
83
+ * Collapse any content-bearing value to the fixed {@link MASKED_TOKEN}.
84
+ * Whitespace-only / empty values pass through unchanged so masking never
85
+ * fabricates visible content where there was none.
86
+ */
87
+ function maskToToken(value) {
88
+ return /\S/.test(value) ? MASKED_TOKEN : value;
89
+ }
90
+ function isDevMode() {
91
+ try {
92
+ return typeof process !== 'undefined' && process?.env?.NODE_ENV !== 'production';
93
+ }
94
+ catch {
95
+ return false;
96
+ }
97
+ }
98
+ // One warning per offending unmask element per session — never spam.
99
+ const warnedUnmaskElements = new WeakSet();
100
+ /**
101
+ * ⚠️ BLAST-RADIUS GUARDRAIL for `data-apm-unmask`.
102
+ *
103
+ * `maskText` uses `element.closest(UNMASK_SELECTOR)`, so `data-apm-unmask` on
104
+ * ANY ancestor unmasks EVERY text node in that ancestor's subtree. A single
105
+ * attribute on `<main>` or a layout wrapper therefore exposes all rendered text
106
+ * in the app. This emits a dev-mode `console.warn` when the unmask sits on a
107
+ * large subtree or a layout container.
108
+ *
109
+ * WARN-ONLY, by design: we never fail closed here (that would re-mask a region
110
+ * a pilot deliberately unmasked and silently break their intent). The warning
111
+ * is a nudge, not an enforcement.
112
+ */
113
+ function warnOnLargeUnmask(unmaskEl) {
114
+ if (!isDevMode()) {
115
+ return;
116
+ }
117
+ try {
118
+ if (warnedUnmaskElements.has(unmaskEl)) {
119
+ return;
120
+ }
121
+ const descendantCount = typeof unmaskEl.querySelectorAll === 'function' ? unmaskEl.querySelectorAll('*').length : 0;
122
+ const isLayoutContainer = LAYOUT_CONTAINER_TAGS.has(unmaskEl.tagName);
123
+ if (descendantCount < UNMASK_SUBTREE_WARN_ELEMENTS && !isLayoutContainer) {
124
+ return;
125
+ }
126
+ warnedUnmaskElements.add(unmaskEl);
127
+ // eslint-disable-next-line no-console
128
+ console.warn(`[@nais/apm] data-apm-unmask on <${unmaskEl.tagName.toLowerCase()}> unmasks its entire subtree ` +
129
+ `(${descendantCount} descendant element(s)). Every text node inside is captured UNMASKED into ` +
130
+ `session replay. Move the attribute to the smallest leaf element that needs to be visible.`);
131
+ }
132
+ catch {
133
+ // The guardrail must never break masking — fail silent (still masks).
134
+ }
135
+ }
136
+ /**
137
+ * rrweb `maskTextFn`: masks all text unless the owning element carries (or is
138
+ * inside an element carrying) the `data-apm-unmask` allowlist attribute.
139
+ *
140
+ * Masked text collapses to the fixed {@link MASKED_TOKEN} (not a per-character
141
+ * map) so length/word-shape does not leak.
142
+ */
143
+ export function maskText(text, element) {
144
+ try {
145
+ if (element && typeof element.closest === 'function') {
146
+ const unmaskEl = element.closest(UNMASK_SELECTOR);
147
+ if (unmaskEl !== null) {
148
+ warnOnLargeUnmask(unmaskEl);
149
+ return text;
150
+ }
151
+ }
152
+ }
153
+ catch {
154
+ // Any DOM weirdness falls through to masking — fail closed.
155
+ }
156
+ return maskToToken(text);
157
+ }
158
+ /**
159
+ * rrweb `maskInputFn`: input values ALWAYS mask (no unmask path — inputs are
160
+ * never allowlisted) and collapse to the fixed {@link MASKED_TOKEN}, so input
161
+ * masking is not length-preserving either.
162
+ */
163
+ export function maskInput(value, _element) {
164
+ return maskToToken(value);
165
+ }
166
+ /**
167
+ * Build the record-time masking floor. `extraBlock` selectors are appended to
168
+ * the base block list; nothing can be relaxed. Spread this LAST into rrweb
169
+ * `record()` options so it always wins.
170
+ */
171
+ export function buildRecordMaskingOptions(extraBlock) {
172
+ return Object.freeze({
173
+ maskAllInputs: true,
174
+ maskTextSelector: '*',
175
+ maskTextFn: maskText,
176
+ maskInputFn: maskInput,
177
+ blockSelector: buildBlockSelector(extraBlock),
178
+ inlineStylesheet: false,
179
+ inlineImages: false,
180
+ recordCanvas: false,
181
+ collectFonts: false,
182
+ slimDOMOptions: true,
183
+ });
184
+ }
185
+ /** Build the snapshot-time masking floor (same rules, snapshot option names). */
186
+ export function buildSnapshotMaskingOptions(extraBlock) {
187
+ return Object.freeze({
188
+ maskAllInputs: true,
189
+ maskTextSelector: '*',
190
+ maskTextFn: maskText,
191
+ maskInputFn: maskInput,
192
+ blockSelector: buildBlockSelector(extraBlock),
193
+ inlineStylesheet: false,
194
+ inlineImages: false,
195
+ recordCanvas: false,
196
+ slimDOM: true,
197
+ });
198
+ }
199
+ //# sourceMappingURL=masking.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"masking.js","sourceRoot":"","sources":["../../src/replay/masking.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAClD,MAAM,CAAC,MAAM,eAAe,GAAG,gBAAgB,CAAC;AAEhD,MAAM,eAAe,GAAG,IAAI,gBAAgB,GAAG,CAAC;AAEhD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAC;AAElC,mFAAmF;AACnF,MAAM,oBAAoB,GAAsB;IAC9C,KAAK;IACL,SAAS;IACT,WAAW;IACX,OAAO;IACP,OAAO;IACP,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,6EAA6E;IAC7E,qEAAqE;IACrE,kBAAkB;IAClB,IAAI,eAAe,GAAG;CACvB,CAAC;AAEF;;;GAGG;AACH,MAAM,4BAA4B,GAAG,EAAE,CAAC;AAExC,yFAAyF;AACzF,MAAM,qBAAqB,GAAwB,IAAI,GAAG,CAAC;IACzD,MAAM;IACN,MAAM;IACN,MAAM;IACN,SAAS;IACT,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AAC,MAAM,CAAC;QACnB,aAAa,EAAE,IAAI;QACnB,gBAAgB,EAAE,GAAG;QACrB,UAAU,EAAE,QAAQ;QACpB,WAAW,EAAE,SAAS;QACtB,aAAa,EAAE,kBAAkB,CAAC,UAAU,CAAC;QAC7C,gBAAgB,EAAE,KAAK;QACvB,YAAY,EAAE,KAAK;QACnB,YAAY,EAAE,KAAK;QACnB,YAAY,EAAE,KAAK;QACnB,cAAc,EAAE,IAAI;KACZ,CAAC,CAAC;AACd,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,2BAA2B,CAAC,UAA8B;IACxE,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,aAAa,EAAE,IAAI;QACnB,gBAAgB,EAAE,GAAG;QACrB,UAAU,EAAE,QAAQ;QACpB,WAAW,EAAE,SAAS;QACtB,aAAa,EAAE,kBAAkB,CAAC,UAAU,CAAC;QAC7C,gBAAgB,EAAE,KAAK;QACvB,YAAY,EAAE,KAAK;QACnB,YAAY,EAAE,KAAK;QACnB,OAAO,EAAE,IAAI;KACL,CAAC,CAAC;AACd,CAAC"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Session-replay option normalization (nais/grafana-apm-app#82 — replay privacy).
3
+ *
4
+ * Resolves the privacy *tier* independently of the capture *trigger* (`mode`):
5
+ *
6
+ * - `mode` ('on-error' | 'always') is the CAPTURE TRIGGER — unchanged. It
7
+ * decides *when* the timeline is shipped, not *what* is captured.
8
+ * - `tier` ('events' | 'wireframe' | 'dom') is the PRIVACY TIER — new. It
9
+ * decides *what* is captured: an interaction timeline with no DOM (`events`,
10
+ * the safe default), a coarse wireframe (`wireframe`, Phase 3), or the full
11
+ * masked DOM recording (`dom`, personvernombud-gated).
12
+ *
13
+ * `enabled:true` with `tier` omitted resolves to `events` — a BREAKING (preview)
14
+ * change from the old masked-DOM default, so the affected pilots get a one-time
15
+ * deprecation warning telling them how to keep DOM capture.
16
+ *
17
+ * Kept out of the eager `config.ts`/`index.ts` bundle path proper: it is only
18
+ * reached from init()'s replay branch and carries no heavy imports.
19
+ */
20
+ /** Privacy tier: what is captured. */
21
+ export type ReplayTier = 'events' | 'wireframe' | 'dom';
22
+ /** Capture trigger: when the timeline is shipped. Unchanged semantics. */
23
+ export type ReplayTrigger = 'on-error' | 'always';
24
+ /** PREVIEW `sessionReplay` option shape accepted by `init()`. */
25
+ export interface SessionReplayOptions {
26
+ enabled?: boolean;
27
+ /** Privacy tier (default `events`). Distinct from `mode`. */
28
+ tier?: ReplayTier;
29
+ /** Capture trigger: 'on-error' (default) buffers until an error; 'always' streams. */
30
+ mode?: ReplayTrigger;
31
+ /** Fraction of sessions recorded, 0..1 (default 1). */
32
+ sampleRate?: number;
33
+ /** Extra CSS selectors to block entirely (tighten-only; DOM tier). */
34
+ block?: string[];
35
+ }
36
+ export interface NormalizedSessionReplay {
37
+ enabled: boolean;
38
+ tier: ReplayTier;
39
+ mode: ReplayTrigger;
40
+ sampleRate?: number;
41
+ block?: string[];
42
+ }
43
+ /** @internal test helper — resets the one-time default-tier deprecation warning. */
44
+ export declare function _resetSessionReplayWarningForTesting(): void;
45
+ /**
46
+ * Normalize the raw `sessionReplay` option into a resolved shape: apply the
47
+ * default tier, validate the tier enum (unknown → `events` + warn), and emit a
48
+ * one-time deprecation warning when an ENABLED replay relies on the new events
49
+ * default (the masked-DOM behavior change). Never throws.
50
+ */
51
+ export declare function normalizeSessionReplay(options: SessionReplayOptions | undefined): NormalizedSessionReplay;
52
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/replay/options.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,sCAAsC;AACtC,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,CAAC;AACxD,0EAA0E;AAC1E,MAAM,MAAM,aAAa,GAAG,UAAU,GAAG,QAAQ,CAAC;AAElD,iEAAiE;AACjE,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,6DAA6D;IAC7D,IAAI,CAAC,EAAE,UAAU,CAAC;IAClB,sFAAsF;IACtF,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,uDAAuD;IACvD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,aAAa,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAWD,oFAAoF;AACpF,wBAAgB,oCAAoC,IAAI,IAAI,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,oBAAoB,GAAG,SAAS,GACxC,uBAAuB,CAgCzB"}