@nagoorkani/session-capture-core 0.1.4 → 0.1.5
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/core.d.ts +1 -1
- package/dist/core.js +1 -1
- package/dist/network.js +61 -13
- package/dist/redact.d.ts +16 -0
- package/dist/redact.js +110 -0
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ import { type CaptureEvent, type EventBatch, type EventType, type InitOptions, t
|
|
|
17
17
|
* still showed a bug fixed in 0.1.1 could not be told apart from one that had
|
|
18
18
|
* never been updated, from the dashboard or from the logs.
|
|
19
19
|
*/
|
|
20
|
-
export declare const SDK_VERSION = "0.1.
|
|
20
|
+
export declare const SDK_VERSION = "0.1.5";
|
|
21
21
|
/**
|
|
22
22
|
* Idle bound. Nothing for this long and the next event opens a new session.
|
|
23
23
|
*
|
package/dist/core.js
CHANGED
|
@@ -21,7 +21,7 @@ import { DEFAULT_CONFIG, SCHEMA_VERSION, } from "./types.js";
|
|
|
21
21
|
* still showed a bug fixed in 0.1.1 could not be told apart from one that had
|
|
22
22
|
* never been updated, from the dashboard or from the logs.
|
|
23
23
|
*/
|
|
24
|
-
export const SDK_VERSION = '0.1.
|
|
24
|
+
export const SDK_VERSION = '0.1.5';
|
|
25
25
|
/**
|
|
26
26
|
* Idle bound. Nothing for this long and the next event opens a new session.
|
|
27
27
|
*
|
package/dist/network.js
CHANGED
|
@@ -36,7 +36,16 @@
|
|
|
36
36
|
* counted twice. `whatwg-fetch` does not, and it is the only XHR-backed fetch
|
|
37
37
|
* in play.
|
|
38
38
|
*/
|
|
39
|
-
import { redact } from "./redact.js";
|
|
39
|
+
import { maskValues, redact } from "./redact.js";
|
|
40
|
+
function asRequest(input) {
|
|
41
|
+
const candidate = input;
|
|
42
|
+
return candidate !== null &&
|
|
43
|
+
typeof candidate === 'object' &&
|
|
44
|
+
typeof candidate.url === 'string' &&
|
|
45
|
+
typeof candidate.method === 'string'
|
|
46
|
+
? candidate
|
|
47
|
+
: null;
|
|
48
|
+
}
|
|
40
49
|
const PATCH_MARKER = '__sessionCapturePatched';
|
|
41
50
|
/** Strip query strings before capture: they routinely carry tokens. */
|
|
42
51
|
export function sanitizeUrl(url) {
|
|
@@ -298,7 +307,8 @@ function captureBody(raw, contentType, maskedKeys) {
|
|
|
298
307
|
const type = mediaType(contentType);
|
|
299
308
|
if (type === 'application/json' || type.endsWith('+json')) {
|
|
300
309
|
try {
|
|
301
|
-
|
|
310
|
+
// Masked before truncating, so a cut cannot leave half an email behind unmasked.
|
|
311
|
+
return truncate(maskValues(JSON.stringify(redact(JSON.parse(raw), maskedKeys))));
|
|
302
312
|
}
|
|
303
313
|
catch {
|
|
304
314
|
// Never fall back to the raw text: a body that does not parse is a body
|
|
@@ -316,7 +326,8 @@ function captureBody(raw, contentType, maskedKeys) {
|
|
|
316
326
|
});
|
|
317
327
|
const out = new URLSearchParams();
|
|
318
328
|
for (const [key, value] of Object.entries(redact(fields, maskedKeys))) {
|
|
319
|
-
|
|
329
|
+
// Per value: encoding turns `@` into `%40`, which the scan would miss.
|
|
330
|
+
out.append(key, maskValues(value));
|
|
320
331
|
}
|
|
321
332
|
return truncate(out.toString());
|
|
322
333
|
}
|
|
@@ -437,6 +448,20 @@ function readResponseBody(request, contentType, maskedKeys) {
|
|
|
437
448
|
// A host XHR that does not implement responseType at all reads as text,
|
|
438
449
|
// rather than as `[not captured: undefined response]`.
|
|
439
450
|
const type = request.responseType ?? '';
|
|
451
|
+
if (type === 'json') {
|
|
452
|
+
// Already parsed by the browser, so `responseText` throws. Re-serialised
|
|
453
|
+
// and sent through the same redaction as a text JSON body; null is a
|
|
454
|
+
// body that did not parse.
|
|
455
|
+
try {
|
|
456
|
+
const parsed = request.response;
|
|
457
|
+
if (parsed === null || parsed === undefined)
|
|
458
|
+
return '[not captured: malformed json]';
|
|
459
|
+
return captureBody(JSON.stringify(parsed), 'application/json', maskedKeys);
|
|
460
|
+
}
|
|
461
|
+
catch {
|
|
462
|
+
return undefined;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
440
465
|
if (type !== '' && type !== 'text') {
|
|
441
466
|
return `[not captured: ${type} response]`;
|
|
442
467
|
}
|
|
@@ -477,12 +502,30 @@ function installFetchCapture(options) {
|
|
|
477
502
|
return () => { };
|
|
478
503
|
}
|
|
479
504
|
const patched = async function patchedFetch(input, init) {
|
|
480
|
-
|
|
481
|
-
|
|
505
|
+
// `String(request)` is "[object Request]", which is what this used to
|
|
506
|
+
// record — with GET and no headers — for every call made through one.
|
|
507
|
+
const request = asRequest(input);
|
|
508
|
+
const url = request ? request.url : typeof input === 'string' ? input : String(input);
|
|
509
|
+
const method = (init?.method ?? request?.method ?? 'GET').toUpperCase();
|
|
510
|
+
// `init.headers` replaces a Request's headers rather than merging, as in fetch itself.
|
|
511
|
+
const headers = init?.headers ?? request?.headers;
|
|
482
512
|
if (!shouldCapture(url, options.ingestEndpoint, options.urlDenyList)) {
|
|
483
513
|
return original(input, init);
|
|
484
514
|
}
|
|
485
515
|
const started = now();
|
|
516
|
+
// A Request's body is a stream the call below consumes, so a copy has to
|
|
517
|
+
// be taken first. Read asynchronously and attached at report time; the
|
|
518
|
+
// app's response is never held for it.
|
|
519
|
+
let requestBodyRead = null;
|
|
520
|
+
if (options.captureBodies && init?.body === undefined && request?.body && typeof request.clone === 'function') {
|
|
521
|
+
try {
|
|
522
|
+
requestBodyRead = request.clone().text();
|
|
523
|
+
}
|
|
524
|
+
catch {
|
|
525
|
+
requestBodyRead = null;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const requestType = headerValue(headersToObject(headers) ?? {}, 'content-type');
|
|
486
529
|
// An XHR-backed fetch does its open/send inside this call, synchronously,
|
|
487
530
|
// so `servedByXhr` is settled by the time the promise comes back.
|
|
488
531
|
let pending;
|
|
@@ -496,7 +539,7 @@ function installFetchCapture(options) {
|
|
|
496
539
|
}
|
|
497
540
|
const claimed = servedByXhr;
|
|
498
541
|
const requestBody = options.captureBodies
|
|
499
|
-
? captureBody(init?.body,
|
|
542
|
+
? captureBody(init?.body, requestType, options.maskedKeys)
|
|
500
543
|
: undefined;
|
|
501
544
|
try {
|
|
502
545
|
const response = await pending;
|
|
@@ -507,7 +550,7 @@ function installFetchCapture(options) {
|
|
|
507
550
|
method,
|
|
508
551
|
status: response.status,
|
|
509
552
|
durationMs: now() - started,
|
|
510
|
-
requestHeaders: redactHeaders(
|
|
553
|
+
requestHeaders: redactHeaders(headers, options.maskedKeys),
|
|
511
554
|
responseHeaders: redactHeaders(readHeaders(response), options.maskedKeys),
|
|
512
555
|
};
|
|
513
556
|
if (requestBody !== undefined)
|
|
@@ -516,11 +559,16 @@ function installFetchCapture(options) {
|
|
|
516
559
|
// that goes through a clone and is reported when it resolves. The app
|
|
517
560
|
// gets its response either way, now, untouched.
|
|
518
561
|
const copy = options.captureBodies ? cloneForReading(response) : null;
|
|
519
|
-
if (copy) {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
562
|
+
if (copy || requestBodyRead) {
|
|
563
|
+
const responseType = headerValue(readHeaders(response) ?? {}, 'content-type');
|
|
564
|
+
void Promise.all([
|
|
565
|
+
requestBodyRead?.then((text) => {
|
|
566
|
+
event.requestBody = captureBody(text, requestType, options.maskedKeys);
|
|
567
|
+
}, () => { }),
|
|
568
|
+
copy?.then((text) => {
|
|
569
|
+
event.responseBody = captureBody(text, responseType, options.maskedKeys);
|
|
570
|
+
}, () => { }),
|
|
571
|
+
]).then(() => report(options, event));
|
|
524
572
|
}
|
|
525
573
|
else {
|
|
526
574
|
report(options, event);
|
|
@@ -534,7 +582,7 @@ function installFetchCapture(options) {
|
|
|
534
582
|
method,
|
|
535
583
|
durationMs: now() - started,
|
|
536
584
|
error: error instanceof Error ? error.message : 'network error',
|
|
537
|
-
requestHeaders: redactHeaders(
|
|
585
|
+
requestHeaders: redactHeaders(headers, options.maskedKeys),
|
|
538
586
|
};
|
|
539
587
|
if (requestBody !== undefined)
|
|
540
588
|
event.requestBody = requestBody;
|
package/dist/redact.d.ts
CHANGED
|
@@ -17,3 +17,19 @@ export declare const REDACTED = "[redacted]";
|
|
|
17
17
|
* shipping a new app build.
|
|
18
18
|
*/
|
|
19
19
|
export declare function redact(value: unknown, maskedKeys?: readonly string[]): unknown;
|
|
20
|
+
/**
|
|
21
|
+
* Mask personal data by *value*, inside captured request and response bodies.
|
|
22
|
+
*
|
|
23
|
+
* Key redaction cannot see `{"note": "mail me at ada@example.com"}`. Bodies
|
|
24
|
+
* are captured by default, so this pass runs on every body before it is
|
|
25
|
+
* stored: emails, JWTs and card numbers (15–19 digits passing Luhn) become
|
|
26
|
+
* `*`, one per character, so the shape of the payload stays readable.
|
|
27
|
+
*
|
|
28
|
+
* Bodies only. Session start carries `appUserEmail` on purpose, and running
|
|
29
|
+
* this over all event data would erase it.
|
|
30
|
+
*
|
|
31
|
+
* Scanned by hand rather than by regex so `crates/event-schema/src/redact.rs`
|
|
32
|
+
* can run the identical algorithm without a regex dependency. The two must
|
|
33
|
+
* agree: change one, change the other, and both test suites restate the cases.
|
|
34
|
+
*/
|
|
35
|
+
export declare function maskValues(text: string): string;
|
package/dist/redact.js
CHANGED
|
@@ -59,3 +59,113 @@ function walk(value, extra, depth, seen) {
|
|
|
59
59
|
}
|
|
60
60
|
return out;
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Mask personal data by *value*, inside captured request and response bodies.
|
|
64
|
+
*
|
|
65
|
+
* Key redaction cannot see `{"note": "mail me at ada@example.com"}`. Bodies
|
|
66
|
+
* are captured by default, so this pass runs on every body before it is
|
|
67
|
+
* stored: emails, JWTs and card numbers (15–19 digits passing Luhn) become
|
|
68
|
+
* `*`, one per character, so the shape of the payload stays readable.
|
|
69
|
+
*
|
|
70
|
+
* Bodies only. Session start carries `appUserEmail` on purpose, and running
|
|
71
|
+
* this over all event data would erase it.
|
|
72
|
+
*
|
|
73
|
+
* Scanned by hand rather than by regex so `crates/event-schema/src/redact.rs`
|
|
74
|
+
* can run the identical algorithm without a regex dependency. The two must
|
|
75
|
+
* agree: change one, change the other, and both test suites restate the cases.
|
|
76
|
+
*/
|
|
77
|
+
export function maskValues(text) {
|
|
78
|
+
const chars = text.split('');
|
|
79
|
+
maskEmails(chars);
|
|
80
|
+
maskJwts(chars);
|
|
81
|
+
maskCards(chars);
|
|
82
|
+
return chars.join('');
|
|
83
|
+
}
|
|
84
|
+
const isDigit = (c) => c !== undefined && c >= '0' && c <= '9';
|
|
85
|
+
const isAlpha = (c) => c !== undefined && ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
|
|
86
|
+
const isAlnum = (c) => isDigit(c) || isAlpha(c);
|
|
87
|
+
const isLocal = (c) => isAlnum(c) || (c !== undefined && '._%+-'.includes(c));
|
|
88
|
+
const isDomain = (c) => isAlnum(c) || c === '.' || c === '-';
|
|
89
|
+
const isJwt = (c) => isAlnum(c) || c === '_' || c === '-' || c === '.';
|
|
90
|
+
function star(chars, from, to) {
|
|
91
|
+
for (let i = from; i < to; i++)
|
|
92
|
+
chars[i] = '*';
|
|
93
|
+
}
|
|
94
|
+
function maskEmails(chars) {
|
|
95
|
+
for (let at = 0; at < chars.length; at++) {
|
|
96
|
+
if (chars[at] !== '@')
|
|
97
|
+
continue;
|
|
98
|
+
let start = at;
|
|
99
|
+
while (start > 0 && isLocal(chars[start - 1]))
|
|
100
|
+
start--;
|
|
101
|
+
let end = at + 1;
|
|
102
|
+
while (end < chars.length && isDomain(chars[end]))
|
|
103
|
+
end++;
|
|
104
|
+
while (end > at + 1 && (chars[end - 1] === '.' || chars[end - 1] === '-'))
|
|
105
|
+
end--;
|
|
106
|
+
const domain = chars.slice(at + 1, end).join('');
|
|
107
|
+
const dot = domain.lastIndexOf('.');
|
|
108
|
+
const tld = dot === -1 ? '' : domain.slice(dot + 1);
|
|
109
|
+
if (start < at && dot > 0 && tld.length >= 2 && tld.split('').every(isAlpha)) {
|
|
110
|
+
star(chars, start, end);
|
|
111
|
+
at = end - 1;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function maskJwts(chars) {
|
|
116
|
+
for (let i = 0; i + 3 <= chars.length; i++) {
|
|
117
|
+
if (chars[i] !== 'e' || chars[i + 1] !== 'y' || chars[i + 2] !== 'J')
|
|
118
|
+
continue;
|
|
119
|
+
if (i > 0 && isJwt(chars[i - 1]))
|
|
120
|
+
continue;
|
|
121
|
+
let end = i;
|
|
122
|
+
while (end < chars.length && isJwt(chars[end]))
|
|
123
|
+
end++;
|
|
124
|
+
while (end > i && chars[end - 1] === '.')
|
|
125
|
+
end--;
|
|
126
|
+
const parts = chars.slice(i, end).join('').split('.');
|
|
127
|
+
if (parts.length === 3 && parts.every((part) => part.length > 0)) {
|
|
128
|
+
star(chars, i, end);
|
|
129
|
+
i = end - 1;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function maskCards(chars) {
|
|
134
|
+
for (let i = 0; i < chars.length; i++) {
|
|
135
|
+
if (!isDigit(chars[i]) || (i > 0 && isAlnum(chars[i - 1])))
|
|
136
|
+
continue;
|
|
137
|
+
const digits = [];
|
|
138
|
+
let j = i;
|
|
139
|
+
let end = i;
|
|
140
|
+
while (j < chars.length) {
|
|
141
|
+
if (isDigit(chars[j])) {
|
|
142
|
+
digits.push(Number(chars[j]));
|
|
143
|
+
j++;
|
|
144
|
+
end = j;
|
|
145
|
+
}
|
|
146
|
+
else if ((chars[j] === ' ' || chars[j] === '-') && isDigit(chars[j - 1]) && isDigit(chars[j + 1])) {
|
|
147
|
+
j++;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (digits.length >= 15 && digits.length <= 19 && !isAlnum(chars[end]) && luhn(digits)) {
|
|
154
|
+
star(chars, i, end);
|
|
155
|
+
}
|
|
156
|
+
i = end - 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function luhn(digits) {
|
|
160
|
+
let sum = 0;
|
|
161
|
+
for (let k = 0; k < digits.length; k++) {
|
|
162
|
+
let d = digits[digits.length - 1 - k];
|
|
163
|
+
if (k % 2 === 1) {
|
|
164
|
+
d *= 2;
|
|
165
|
+
if (d > 9)
|
|
166
|
+
d -= 9;
|
|
167
|
+
}
|
|
168
|
+
sum += d;
|
|
169
|
+
}
|
|
170
|
+
return sum % 10 === 0;
|
|
171
|
+
}
|