aissue 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +390 -2
- package/dist/cjs/browser.d.ts +16 -0
- package/dist/cjs/browser.js +80 -0
- package/dist/cjs/core/breadcrumbs.d.ts +48 -0
- package/dist/cjs/core/breadcrumbs.js +313 -0
- package/dist/cjs/core/client.d.ts +54 -0
- package/dist/cjs/core/client.js +293 -0
- package/dist/cjs/core/env.d.ts +19 -0
- package/dist/cjs/core/env.js +71 -0
- package/dist/cjs/core/event.d.ts +35 -0
- package/dist/cjs/core/event.js +187 -0
- package/dist/cjs/core/hub.d.ts +20 -0
- package/dist/cjs/core/hub.js +21 -0
- package/dist/cjs/core/instrument-browser.d.ts +16 -0
- package/dist/cjs/core/instrument-browser.js +208 -0
- package/dist/cjs/core/instrument-node.d.ts +5 -0
- package/dist/cjs/core/instrument-node.js +250 -0
- package/dist/cjs/core/instrument.d.ts +12 -0
- package/dist/cjs/core/instrument.js +137 -0
- package/dist/cjs/core/privacy.d.ts +25 -0
- package/dist/cjs/core/privacy.js +269 -0
- package/dist/cjs/core/stack.d.ts +13 -0
- package/dist/cjs/core/stack.js +147 -0
- package/dist/cjs/core/transport.d.ts +31 -0
- package/dist/cjs/core/transport.js +154 -0
- package/dist/cjs/core/types.d.ts +149 -0
- package/dist/cjs/core/types.js +2 -0
- package/dist/cjs/core/version.d.ts +2 -0
- package/dist/cjs/core/version.js +6 -0
- package/dist/cjs/index.d.ts +27 -0
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/nextjs/client.d.ts +10 -0
- package/dist/cjs/nextjs/client.js +99 -0
- package/dist/cjs/nextjs/index.d.ts +31 -0
- package/dist/cjs/nextjs/index.js +99 -0
- package/dist/cjs/node.d.ts +48 -0
- package/dist/cjs/node.js +223 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/react.d.ts +40 -0
- package/dist/cjs/react.js +90 -0
- package/dist/esm/browser.d.ts +16 -0
- package/dist/esm/browser.js +71 -0
- package/dist/esm/cli.d.ts +2 -0
- package/dist/esm/cli.js +386 -0
- package/dist/esm/core/breadcrumbs.d.ts +48 -0
- package/dist/esm/core/breadcrumbs.js +296 -0
- package/dist/esm/core/client.d.ts +54 -0
- package/dist/esm/core/client.js +285 -0
- package/dist/esm/core/env.d.ts +19 -0
- package/dist/esm/core/env.js +62 -0
- package/dist/esm/core/event.d.ts +35 -0
- package/dist/esm/core/event.js +179 -0
- package/dist/esm/core/hub.d.ts +20 -0
- package/dist/esm/core/hub.js +17 -0
- package/dist/esm/core/instrument-browser.d.ts +16 -0
- package/dist/esm/core/instrument-browser.js +203 -0
- package/dist/esm/core/instrument-node.d.ts +5 -0
- package/dist/esm/core/instrument-node.js +214 -0
- package/dist/esm/core/instrument.d.ts +12 -0
- package/dist/esm/core/instrument.js +132 -0
- package/dist/esm/core/privacy.d.ts +25 -0
- package/dist/esm/core/privacy.js +257 -0
- package/dist/esm/core/stack.d.ts +13 -0
- package/dist/esm/core/stack.js +140 -0
- package/dist/esm/core/transport.d.ts +31 -0
- package/dist/esm/core/transport.js +149 -0
- package/dist/esm/core/types.d.ts +149 -0
- package/dist/esm/core/types.js +1 -0
- package/dist/esm/core/version.d.ts +2 -0
- package/dist/esm/core/version.js +3 -0
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +72 -0
- package/dist/esm/nextjs/client.d.ts +10 -0
- package/dist/esm/nextjs/client.js +90 -0
- package/dist/esm/nextjs/index.d.ts +31 -0
- package/dist/esm/nextjs/index.js +88 -0
- package/dist/esm/node.d.ts +48 -0
- package/dist/esm/node.js +210 -0
- package/dist/esm/react.d.ts +40 -0
- package/dist/esm/react.js +47 -0
- package/package.json +141 -4
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-only automatic breadcrumbs: console, fetch (undici), http/https client requests, and a
|
|
3
|
+
* per-request breadcrumb buffer (AsyncLocalStorage) for every incoming http/https server request,
|
|
4
|
+
* so concurrent requests never mix their breadcrumbs.
|
|
5
|
+
*
|
|
6
|
+
* Node built-ins are looked up at runtime (process.getBuiltinModule, else a dynamic import) so
|
|
7
|
+
* that edge bundles that include aissue/nextjs never import them statically.
|
|
8
|
+
*/
|
|
9
|
+
import { breadcrumbsActive, currentBuffer, isAIssueUrl, wrapMethod, } from './breadcrumbs.js';
|
|
10
|
+
import { getHub } from './hub.js';
|
|
11
|
+
import { httpBreadcrumb, instrumentConsole, instrumentFetch } from './instrument.js';
|
|
12
|
+
import { sanitizeUrl } from './privacy.js';
|
|
13
|
+
function builtinSync(name) {
|
|
14
|
+
try {
|
|
15
|
+
const p = globalThis.process;
|
|
16
|
+
if (p && typeof p.getBuiltinModule === 'function')
|
|
17
|
+
return p.getBuiltinModule(name) ?? undefined;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// ignore
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
async function builtinAsync(name) {
|
|
25
|
+
const sync = builtinSync(name);
|
|
26
|
+
if (sync)
|
|
27
|
+
return sync;
|
|
28
|
+
try {
|
|
29
|
+
const ns = await import(/* webpackIgnore: true */ name);
|
|
30
|
+
return ns && ns.default ? ns.default : ns;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function setupStorage(asyncHooks) {
|
|
37
|
+
const hub = getHub();
|
|
38
|
+
if (hub.breadcrumbStorage)
|
|
39
|
+
return;
|
|
40
|
+
const ALS = (asyncHooks && asyncHooks.AsyncLocalStorage) || globalThis.AsyncLocalStorage;
|
|
41
|
+
if (typeof ALS !== 'function')
|
|
42
|
+
return;
|
|
43
|
+
const als = new ALS();
|
|
44
|
+
hub.breadcrumbStorage = {
|
|
45
|
+
getStore: () => als.getStore(),
|
|
46
|
+
run: (store, fn) => als.run(store, fn),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Every incoming request on an http(s) server gets its own breadcrumb buffer. */
|
|
50
|
+
function instrumentServer(serverProto) {
|
|
51
|
+
if (!serverProto)
|
|
52
|
+
return;
|
|
53
|
+
wrapMethod(serverProto, 'emit', (original) => function aissueServerEmit(...args) {
|
|
54
|
+
let storage;
|
|
55
|
+
try {
|
|
56
|
+
storage = args[0] === 'request' && breadcrumbsActive() ? getHub().breadcrumbStorage : undefined;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
storage = undefined;
|
|
60
|
+
}
|
|
61
|
+
if (!storage)
|
|
62
|
+
return original.apply(this, args);
|
|
63
|
+
return storage.run([], () => original.apply(this, args));
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function requestInfo(args, defaultProtocol) {
|
|
67
|
+
let url;
|
|
68
|
+
let options;
|
|
69
|
+
const [a, b] = args;
|
|
70
|
+
if (typeof a === 'string')
|
|
71
|
+
url = new URL(a);
|
|
72
|
+
else if (a && typeof a === 'object' && typeof a.href === 'string' && typeof a.host === 'string') {
|
|
73
|
+
url = new URL(a.href);
|
|
74
|
+
}
|
|
75
|
+
else if (a && typeof a === 'object')
|
|
76
|
+
options = a;
|
|
77
|
+
if (url && b && typeof b === 'object')
|
|
78
|
+
options = b;
|
|
79
|
+
let full;
|
|
80
|
+
if (url) {
|
|
81
|
+
full = `${url.protocol}//${url.host}${url.pathname}`;
|
|
82
|
+
}
|
|
83
|
+
else if (options) {
|
|
84
|
+
const protocol = typeof options.protocol === 'string' ? options.protocol : defaultProtocol;
|
|
85
|
+
let host = String(options.hostname || options.host || 'localhost');
|
|
86
|
+
const colons = host.split(':').length - 1;
|
|
87
|
+
if (colons === 1)
|
|
88
|
+
host = host.replace(/:\d*$/, ''); // "example.com:8080"
|
|
89
|
+
else if (colons > 1 && !host.startsWith('['))
|
|
90
|
+
host = `[${host}]`; // IPv6
|
|
91
|
+
const port = options.port ? `:${options.port}` : '';
|
|
92
|
+
const defaultPort = (protocol === 'https:' && port === ':443') || (protocol === 'http:' && port === ':80');
|
|
93
|
+
const path = typeof options.path === 'string' ? options.path : '/';
|
|
94
|
+
full = `${protocol}//${host}${defaultPort ? '' : port}${path}`;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
full = sanitizeUrl(full);
|
|
100
|
+
if (isAIssueUrl(full))
|
|
101
|
+
return undefined;
|
|
102
|
+
const method = (options && typeof options.method === 'string' && options.method) || 'GET';
|
|
103
|
+
return { method: method.toUpperCase().slice(0, 16), url: full, start: Date.now() };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Watch a ClientRequest by wrapping its emit: adding 'response' or 'error' listeners would change
|
|
107
|
+
* Node's behaviour (unread responses are no longer dumped; unhandled errors no longer throw).
|
|
108
|
+
*/
|
|
109
|
+
function observeRequest(req, info, buffer) {
|
|
110
|
+
if (!req || typeof req.emit !== 'function')
|
|
111
|
+
return;
|
|
112
|
+
const originalEmit = req.emit;
|
|
113
|
+
let done = false;
|
|
114
|
+
req.emit = function aissueRequestEmit(...args) {
|
|
115
|
+
if (!done && (args[0] === 'response' || args[0] === 'error')) {
|
|
116
|
+
done = true;
|
|
117
|
+
try {
|
|
118
|
+
if (args[0] === 'response') {
|
|
119
|
+
const status = args[1]?.statusCode;
|
|
120
|
+
httpBreadcrumb('http', info, typeof status === 'number' ? status : undefined, buffer);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
httpBreadcrumb('http', info, undefined, buffer, args[1] ?? 'Error');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// ignore
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return originalEmit.apply(this, args);
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function instrumentHttpModule(mod, protocol) {
|
|
134
|
+
if (!mod)
|
|
135
|
+
return false;
|
|
136
|
+
let changed = false;
|
|
137
|
+
for (const name of ['request', 'get']) {
|
|
138
|
+
changed =
|
|
139
|
+
wrapMethod(mod, name, (original) => function aissueHttpRequest(...args) {
|
|
140
|
+
let info;
|
|
141
|
+
try {
|
|
142
|
+
if (breadcrumbsActive('http'))
|
|
143
|
+
info = requestInfo(args, protocol);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
info = undefined;
|
|
147
|
+
}
|
|
148
|
+
const req = original.apply(this, args);
|
|
149
|
+
if (info) {
|
|
150
|
+
try {
|
|
151
|
+
observeRequest(req, info, currentBuffer());
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
// ignore
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return req;
|
|
158
|
+
}) || changed;
|
|
159
|
+
}
|
|
160
|
+
return changed;
|
|
161
|
+
}
|
|
162
|
+
function install(mods) {
|
|
163
|
+
const steps = [
|
|
164
|
+
() => setupStorage(mods.asyncHooks),
|
|
165
|
+
instrumentConsole,
|
|
166
|
+
instrumentFetch,
|
|
167
|
+
() => instrumentServer(mods.http?.Server?.prototype),
|
|
168
|
+
() => instrumentServer(mods.https?.Server?.prototype),
|
|
169
|
+
() => {
|
|
170
|
+
const changedHttp = instrumentHttpModule(mods.http, 'http:');
|
|
171
|
+
const changedHttps = instrumentHttpModule(mods.https, 'https:');
|
|
172
|
+
// Make `import { request } from 'node:http'` see the wrappers too.
|
|
173
|
+
if ((changedHttp || changedHttps) && typeof mods.module?.syncBuiltinESMExports === 'function') {
|
|
174
|
+
mods.module.syncBuiltinESMExports();
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
for (const step of steps) {
|
|
179
|
+
try {
|
|
180
|
+
step();
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
// one failing source must not stop the others
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Install Node breadcrumbs (idempotent). Synchronous on Node 20.16+/22.3+ (process.getBuiltinModule);
|
|
189
|
+
* on older versions the built-ins are imported first, so installation completes a moment later.
|
|
190
|
+
*/
|
|
191
|
+
export function instrumentNode() {
|
|
192
|
+
try {
|
|
193
|
+
const names = { http: 'node:http', https: 'node:https', asyncHooks: 'node:async_hooks', module: 'node:module' };
|
|
194
|
+
const http = builtinSync(names.http);
|
|
195
|
+
if (http) {
|
|
196
|
+
install({
|
|
197
|
+
http,
|
|
198
|
+
https: builtinSync(names.https),
|
|
199
|
+
asyncHooks: builtinSync(names.asyncHooks),
|
|
200
|
+
module: builtinSync(names.module),
|
|
201
|
+
});
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
Promise.all([
|
|
205
|
+
builtinAsync(names.http),
|
|
206
|
+
builtinAsync(names.https),
|
|
207
|
+
builtinAsync(names.asyncHooks),
|
|
208
|
+
builtinAsync(names.module),
|
|
209
|
+
]).then(([h, hs, ah, m]) => install({ http: h, https: hs, asyncHooks: ah, module: m }), () => install({}));
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
// never throw into the host app
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Breadcrumb } from './types.js';
|
|
2
|
+
/** console.log / info / warn / error become `log` breadcrumbs. */
|
|
3
|
+
export declare function instrumentConsole(): void;
|
|
4
|
+
export interface HttpInfo {
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
start: number;
|
|
8
|
+
}
|
|
9
|
+
/** Record an http breadcrumb: "GET https://api.example.com/users [200]". */
|
|
10
|
+
export declare function httpBreadcrumb(category: string, info: HttpInfo, status: number | undefined, buffer: Breadcrumb[], error?: unknown): void;
|
|
11
|
+
/** globalThis.fetch: method, URL without the query string, status. Requests to AIssue are skipped. */
|
|
12
|
+
export declare function instrumentFetch(): void;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatic breadcrumbs shared by the browser and Node: console and fetch.
|
|
3
|
+
* Every wrapper is idempotent (an already wrapped function is never wrapped again), never throws
|
|
4
|
+
* into the host, keeps the original behaviour (arguments, `this`, return value, exceptions) and
|
|
5
|
+
* records nothing while the SDK is disabled.
|
|
6
|
+
*/
|
|
7
|
+
import { breadcrumbsActive, currentBuffer, describeValue, isAIssueUrl, levelForStatus, recordBreadcrumb, wrapMethod, } from './breadcrumbs.js';
|
|
8
|
+
import { sanitizeUrl } from './privacy.js';
|
|
9
|
+
const CONSOLE_LEVELS = {
|
|
10
|
+
log: 'info',
|
|
11
|
+
info: 'info',
|
|
12
|
+
warn: 'warning',
|
|
13
|
+
error: 'error',
|
|
14
|
+
};
|
|
15
|
+
function consoleBreadcrumb(method, args) {
|
|
16
|
+
if (!args.length)
|
|
17
|
+
return;
|
|
18
|
+
const first = args[0];
|
|
19
|
+
if (typeof first === 'string' && first.startsWith('[aissue]'))
|
|
20
|
+
return; // the SDK's own logs
|
|
21
|
+
const parts = [];
|
|
22
|
+
let length = 0;
|
|
23
|
+
for (const arg of args) {
|
|
24
|
+
if (length > 2000)
|
|
25
|
+
break;
|
|
26
|
+
const text = describeValue(arg, 2000);
|
|
27
|
+
parts.push(text);
|
|
28
|
+
length += text.length + 1;
|
|
29
|
+
}
|
|
30
|
+
recordBreadcrumb({ type: 'log', category: 'console', level: CONSOLE_LEVELS[method], message: parts.join(' ') });
|
|
31
|
+
}
|
|
32
|
+
/** console.log / info / warn / error become `log` breadcrumbs. */
|
|
33
|
+
export function instrumentConsole() {
|
|
34
|
+
const c = globalThis.console;
|
|
35
|
+
if (!c)
|
|
36
|
+
return;
|
|
37
|
+
for (const method of Object.keys(CONSOLE_LEVELS)) {
|
|
38
|
+
wrapMethod(c, method, (original) => function aissueConsole(...args) {
|
|
39
|
+
try {
|
|
40
|
+
if (breadcrumbsActive('console'))
|
|
41
|
+
consoleBreadcrumb(method, args);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// never throw into the host app
|
|
45
|
+
}
|
|
46
|
+
return original.apply(this, args);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Record an http breadcrumb: "GET https://api.example.com/users [200]". */
|
|
51
|
+
export function httpBreadcrumb(category, info, status, buffer, error) {
|
|
52
|
+
try {
|
|
53
|
+
recordHttp(category, info, status, buffer, error);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// never throw into the host app
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function recordHttp(category, info, status, buffer, error) {
|
|
60
|
+
const data = { method: info.method, url: info.url };
|
|
61
|
+
if (status)
|
|
62
|
+
data.status_code = status;
|
|
63
|
+
data.duration_ms = Math.max(0, Date.now() - info.start);
|
|
64
|
+
if (error !== undefined) {
|
|
65
|
+
const e = error;
|
|
66
|
+
data.error = typeof e?.code === 'string' ? e.code : typeof e?.name === 'string' ? e.name : 'Error';
|
|
67
|
+
}
|
|
68
|
+
recordBreadcrumb({
|
|
69
|
+
type: 'http',
|
|
70
|
+
category,
|
|
71
|
+
level: error !== undefined ? 'error' : levelForStatus(status),
|
|
72
|
+
message: `${info.method} ${info.url}${status ? ` [${status}]` : error !== undefined ? ' [failed]' : ''}`,
|
|
73
|
+
data,
|
|
74
|
+
}, buffer);
|
|
75
|
+
}
|
|
76
|
+
function fetchInfo(input, init) {
|
|
77
|
+
let url;
|
|
78
|
+
let method;
|
|
79
|
+
if (typeof input === 'string')
|
|
80
|
+
url = input;
|
|
81
|
+
else if (input && typeof input === 'object') {
|
|
82
|
+
const i = input;
|
|
83
|
+
if (typeof i.href === 'string')
|
|
84
|
+
url = i.href; // URL
|
|
85
|
+
else if (typeof i.url === 'string') {
|
|
86
|
+
url = i.url; // Request
|
|
87
|
+
method = i.method;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!url || isAIssueUrl(url))
|
|
91
|
+
return undefined;
|
|
92
|
+
const initMethod = init && typeof init === 'object' ? init.method : undefined;
|
|
93
|
+
if (typeof initMethod === 'string' && initMethod)
|
|
94
|
+
method = initMethod;
|
|
95
|
+
return {
|
|
96
|
+
method: (typeof method === 'string' && method ? method : 'GET').toUpperCase().slice(0, 16),
|
|
97
|
+
url: sanitizeUrl(url),
|
|
98
|
+
start: Date.now(),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** globalThis.fetch: method, URL without the query string, status. Requests to AIssue are skipped. */
|
|
102
|
+
export function instrumentFetch() {
|
|
103
|
+
wrapMethod(globalThis, 'fetch', (original) => function aissueFetch(...args) {
|
|
104
|
+
let info;
|
|
105
|
+
try {
|
|
106
|
+
if (breadcrumbsActive('fetch'))
|
|
107
|
+
info = fetchInfo(args[0], args[1]);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
info = undefined;
|
|
111
|
+
}
|
|
112
|
+
const result = original.apply(this, args);
|
|
113
|
+
if (info) {
|
|
114
|
+
try {
|
|
115
|
+
const buffer = currentBuffer();
|
|
116
|
+
const done = info;
|
|
117
|
+
const promise = result;
|
|
118
|
+
if (promise && typeof promise.then === 'function') {
|
|
119
|
+
// A side branch: the caller's promise is returned untouched.
|
|
120
|
+
promise.then((response) => {
|
|
121
|
+
const status = response?.status;
|
|
122
|
+
httpBreadcrumb('fetch', done, typeof status === 'number' ? status : undefined, buffer);
|
|
123
|
+
}, (error) => httpBreadcrumb('fetch', done, undefined, buffer, error ?? 'Error'));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// never throw into the host app
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { HeadersInput, QueryInput } from './types.js';
|
|
2
|
+
export declare const FILTERED = "[Filtered]";
|
|
3
|
+
export declare function isSensitiveKey(key: unknown): boolean;
|
|
4
|
+
export declare function truncate(value: string, max: number): string;
|
|
5
|
+
/** "/a/b?x=1#y" -> "/a/b". Also used for Referer. */
|
|
6
|
+
export declare function stripQuery(value: string): string;
|
|
7
|
+
/** The query part of a path or URL ("" when there is none), without the fragment. */
|
|
8
|
+
export declare function queryOf(value: string): string;
|
|
9
|
+
/** Keep only allowlisted headers, with canonical names; strip the query string from Referer. */
|
|
10
|
+
export declare function filterHeaders(input: HeadersInput | undefined | null): Record<string, string>;
|
|
11
|
+
/**
|
|
12
|
+
* Query parameters as {key: [values]}. Values under sensitive-looking keys become ["[Filtered]"].
|
|
13
|
+
* Accepts "a=1&b=2", "?a=1", URLSearchParams, or an object such as Express' req.query.
|
|
14
|
+
*/
|
|
15
|
+
export declare function filterQuery(input: QueryInput | undefined | null): Record<string, string[]>;
|
|
16
|
+
/**
|
|
17
|
+
* JSON-safe copy of arbitrary context: sensitive-looking keys filtered (recursively),
|
|
18
|
+
* depth / size limited, functions and cycles dropped.
|
|
19
|
+
*/
|
|
20
|
+
export declare function scrub(value: unknown, depth?: number, seen?: WeakSet<object>): unknown;
|
|
21
|
+
export declare const EMAIL_PLACEHOLDER = "[email]";
|
|
22
|
+
/** Redact credentials, card numbers and email addresses from free text. Never throws. */
|
|
23
|
+
export declare function scrubText(text: string): string;
|
|
24
|
+
/** "https://user:pass@host/a/b?x=1#y" -> "https://host/a/b" (no credentials, query or fragment). */
|
|
25
|
+
export declare function sanitizeUrl(url: string): string;
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
export const FILTERED = '[Filtered]';
|
|
2
|
+
/** Same rule as the Python SDK (sdks/python/aissue/client.py `_SENSITIVE_KEY_RE`). */
|
|
3
|
+
const SENSITIVE_KEY_RE = /passw(?:or)?d|passwd|pwd|secret|token|api[-_]?key|apikey|(?:private|access|secret|signing|encryption)[-_]?key|authoriz|session|cookie|csrf|credential|signature|credit[-_]?card|card[-_]?(?:number|num|no)|cvv|cvc|iban|ssn|^auth$|^cc$/i;
|
|
4
|
+
export function isSensitiveKey(key) {
|
|
5
|
+
return typeof key === 'string' && SENSITIVE_KEY_RE.test(key);
|
|
6
|
+
}
|
|
7
|
+
/** Only these request headers are ever sent (lower-case name -> canonical name). */
|
|
8
|
+
const HEADER_ALLOWLIST = {
|
|
9
|
+
'user-agent': 'User-Agent',
|
|
10
|
+
'content-type': 'Content-Type',
|
|
11
|
+
'content-length': 'Content-Length',
|
|
12
|
+
accept: 'Accept',
|
|
13
|
+
'accept-language': 'Accept-Language',
|
|
14
|
+
'accept-encoding': 'Accept-Encoding',
|
|
15
|
+
host: 'Host',
|
|
16
|
+
origin: 'Origin',
|
|
17
|
+
referer: 'Referer',
|
|
18
|
+
};
|
|
19
|
+
const MAX_HEADER_VALUE = 1024;
|
|
20
|
+
const MAX_QUERY_KEYS = 50;
|
|
21
|
+
const MAX_QUERY_VALUES = 20;
|
|
22
|
+
const MAX_QUERY_VALUE_LENGTH = 1024;
|
|
23
|
+
export function truncate(value, max) {
|
|
24
|
+
return value.length > max ? value.slice(0, max) + '...' : value;
|
|
25
|
+
}
|
|
26
|
+
/** "/a/b?x=1#y" -> "/a/b". Also used for Referer. */
|
|
27
|
+
export function stripQuery(value) {
|
|
28
|
+
const q = value.indexOf('?');
|
|
29
|
+
const h = value.indexOf('#');
|
|
30
|
+
let end = value.length;
|
|
31
|
+
if (q !== -1)
|
|
32
|
+
end = q;
|
|
33
|
+
if (h !== -1 && h < end)
|
|
34
|
+
end = h;
|
|
35
|
+
return value.slice(0, end);
|
|
36
|
+
}
|
|
37
|
+
/** The query part of a path or URL ("" when there is none), without the fragment. */
|
|
38
|
+
export function queryOf(value) {
|
|
39
|
+
const q = value.indexOf('?');
|
|
40
|
+
if (q === -1)
|
|
41
|
+
return '';
|
|
42
|
+
const rest = value.slice(q + 1);
|
|
43
|
+
const h = rest.indexOf('#');
|
|
44
|
+
return h === -1 ? rest : rest.slice(0, h);
|
|
45
|
+
}
|
|
46
|
+
function pairs(input) {
|
|
47
|
+
const out = [];
|
|
48
|
+
if (!input || typeof input !== 'object')
|
|
49
|
+
return out;
|
|
50
|
+
if (Array.isArray(input)) {
|
|
51
|
+
for (const item of input) {
|
|
52
|
+
if (Array.isArray(item) && item.length >= 2)
|
|
53
|
+
out.push([String(item[0]), item[1]]);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
const forEach = input.forEach;
|
|
58
|
+
if (typeof forEach === 'function') {
|
|
59
|
+
// Headers / URLSearchParams / Map-like: callback(value, key)
|
|
60
|
+
forEach.call(input, (value, key) => {
|
|
61
|
+
out.push([String(key), value]);
|
|
62
|
+
});
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
for (const key of Object.keys(input))
|
|
66
|
+
out.push([key, input[key]]);
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Keep only allowlisted headers, with canonical names; strip the query string from Referer. */
|
|
70
|
+
export function filterHeaders(input) {
|
|
71
|
+
const headers = {};
|
|
72
|
+
try {
|
|
73
|
+
for (const [name, raw] of pairs(input)) {
|
|
74
|
+
const lowered = name.toLowerCase();
|
|
75
|
+
const canonical = HEADER_ALLOWLIST[lowered];
|
|
76
|
+
if (!canonical || isSensitiveKey(lowered) || raw === undefined || raw === null)
|
|
77
|
+
continue;
|
|
78
|
+
let value = Array.isArray(raw) ? raw.map(String).join(', ') : String(raw);
|
|
79
|
+
if (lowered === 'referer')
|
|
80
|
+
value = stripQuery(value);
|
|
81
|
+
headers[canonical] = truncate(value, MAX_HEADER_VALUE);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// never throw into the host app
|
|
86
|
+
}
|
|
87
|
+
return headers;
|
|
88
|
+
}
|
|
89
|
+
function parseQueryString(query) {
|
|
90
|
+
const out = [];
|
|
91
|
+
const q = query.charAt(0) === '?' ? query.slice(1) : query;
|
|
92
|
+
for (const part of q.split('&')) {
|
|
93
|
+
if (!part)
|
|
94
|
+
continue;
|
|
95
|
+
const eq = part.indexOf('=');
|
|
96
|
+
const rawKey = eq === -1 ? part : part.slice(0, eq);
|
|
97
|
+
const rawValue = eq === -1 ? '' : part.slice(eq + 1);
|
|
98
|
+
out.push([decode(rawKey), decode(rawValue)]);
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
function decode(value) {
|
|
103
|
+
try {
|
|
104
|
+
return decodeURIComponent(value.replace(/\+/g, ' '));
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Query parameters as {key: [values]}. Values under sensitive-looking keys become ["[Filtered]"].
|
|
112
|
+
* Accepts "a=1&b=2", "?a=1", URLSearchParams, or an object such as Express' req.query.
|
|
113
|
+
*/
|
|
114
|
+
export function filterQuery(input) {
|
|
115
|
+
const result = {};
|
|
116
|
+
try {
|
|
117
|
+
const entries = typeof input === 'string' ? parseQueryString(input) : pairs(input);
|
|
118
|
+
let keys = 0;
|
|
119
|
+
for (const [key, value] of entries) {
|
|
120
|
+
if (!(key in result)) {
|
|
121
|
+
if (keys >= MAX_QUERY_KEYS)
|
|
122
|
+
continue;
|
|
123
|
+
keys++;
|
|
124
|
+
result[key] = [];
|
|
125
|
+
}
|
|
126
|
+
if (isSensitiveKey(key)) {
|
|
127
|
+
result[key] = [FILTERED];
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const list = result[key];
|
|
131
|
+
const values = Array.isArray(value) ? value : [value];
|
|
132
|
+
for (const v of values) {
|
|
133
|
+
if (list.length >= MAX_QUERY_VALUES)
|
|
134
|
+
break;
|
|
135
|
+
list.push(truncate(queryValueToString(v), MAX_QUERY_VALUE_LENGTH));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// never throw into the host app
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
function queryValueToString(value) {
|
|
145
|
+
if (value === undefined || value === null)
|
|
146
|
+
return '';
|
|
147
|
+
if (typeof value === 'object')
|
|
148
|
+
return '[Object]';
|
|
149
|
+
return String(value);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* JSON-safe copy of arbitrary context: sensitive-looking keys filtered (recursively),
|
|
153
|
+
* depth / size limited, functions and cycles dropped.
|
|
154
|
+
*/
|
|
155
|
+
export function scrub(value, depth = 0, seen = new WeakSet()) {
|
|
156
|
+
if (value === null || value === undefined)
|
|
157
|
+
return value ?? null;
|
|
158
|
+
const type = typeof value;
|
|
159
|
+
if (type === 'string')
|
|
160
|
+
return truncate(value, 4096);
|
|
161
|
+
if (type === 'number')
|
|
162
|
+
return Number.isFinite(value) ? value : String(value);
|
|
163
|
+
if (type === 'boolean')
|
|
164
|
+
return value;
|
|
165
|
+
if (type === 'bigint' || type === 'symbol')
|
|
166
|
+
return String(value);
|
|
167
|
+
if (type === 'function')
|
|
168
|
+
return '[Function]';
|
|
169
|
+
if (depth >= 6)
|
|
170
|
+
return '[Object]';
|
|
171
|
+
const obj = value;
|
|
172
|
+
if (seen.has(obj))
|
|
173
|
+
return '[Circular]';
|
|
174
|
+
seen.add(obj);
|
|
175
|
+
if (Array.isArray(obj))
|
|
176
|
+
return obj.slice(0, 50).map((v) => scrub(v, depth + 1, seen));
|
|
177
|
+
if (obj instanceof Date)
|
|
178
|
+
return isNaN(obj.getTime()) ? String(obj) : obj.toISOString();
|
|
179
|
+
const out = {};
|
|
180
|
+
let count = 0;
|
|
181
|
+
for (const key of Object.keys(obj)) {
|
|
182
|
+
if (count++ >= 100)
|
|
183
|
+
break;
|
|
184
|
+
out[key] = isSensitiveKey(key) ? FILTERED : scrub(obj[key], depth + 1, seen);
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
// --- free-text scrubbing (port of the server's app/utils/scrubbing.py `scrub_text`) ----------------
|
|
189
|
+
export const EMAIL_PLACEHOLDER = '[email]';
|
|
190
|
+
const SENSITIVE_NAME_IN_TEXT = 'passw(?:or)?d|passwd|pwd|secret|token|api[-_]?key|apikey|(?:private|access|secret|signing|encryption)[-_]?key' +
|
|
191
|
+
'|authoriz|session[-_]?id|sessionid|session[-_]?key|cookie|csrf|credential|signature' +
|
|
192
|
+
'|credit[-_]?card|card[-_]?(?:number|num|no)|cvv|cvc|iban|ssn';
|
|
193
|
+
const KEY_IN_TEXT = `[\\w-]*?(?:${SENSITIVE_NAME_IN_TEXT})[\\w-]*`;
|
|
194
|
+
// "password": "x" / 'api_key': 'x'
|
|
195
|
+
const QUOTED_PAIR_RE = new RegExp(`(["'])(${KEY_IN_TEXT})\\1(\\s*:\\s*)(["'])(?:\\\\.|(?!\\4).)*\\4`, 'gi');
|
|
196
|
+
// "password": 123 (unquoted value after a quoted key)
|
|
197
|
+
const QUOTED_KEY_BARE_VALUE_RE = new RegExp(`(["'])(${KEY_IN_TEXT})\\1(\\s*:\\s*)(?!["'\\[{])([^\\s,}\\]]+)`, 'gi');
|
|
198
|
+
// password=x (query strings, form bodies)
|
|
199
|
+
const ASSIGNMENT_RE = new RegExp(`\\b(${KEY_IN_TEXT})=(?!\\[Filtered\\])([^\\s&;,'")\\]}]+)`, 'gi');
|
|
200
|
+
// "Cookie: a=b" header lines
|
|
201
|
+
const HEADER_LINE_RE = /^(\s*(?:cookie|set-cookie|authorization|proxy-authorization|x-api-key)\s*:\s*).+$/gim;
|
|
202
|
+
const TOKEN_PATTERNS = [
|
|
203
|
+
/\beyJ[A-Za-z0-9_-]{5,}\.eyJ[A-Za-z0-9_-]{5,}\.[A-Za-z0-9_-]*/g, // JWT
|
|
204
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
|
|
205
|
+
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, // AWS access key ids
|
|
206
|
+
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]{10,}/g, // Stripe-style keys
|
|
207
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}/g, // GitHub tokens
|
|
208
|
+
/\bgithub_pat_[A-Za-z0-9_]{20,}/g,
|
|
209
|
+
/\bxox[abprs]-[A-Za-z0-9-]{10,}/g, // Slack tokens
|
|
210
|
+
/\bAIza[0-9A-Za-z_-]{30,}/g, // Google API keys
|
|
211
|
+
/\baissue_pat_[A-Za-z0-9_-]{8,}/g, // AIssue account tokens
|
|
212
|
+
];
|
|
213
|
+
// "Bearer <token>" / "Basic <base64>" (needs a digit so prose like "Basic authentication" survives)
|
|
214
|
+
const AUTH_SCHEME_RE = /\b(Bearer|Basic|Token)\s+(?=[A-Za-z0-9\-._~+/]*\d)[A-Za-z0-9\-._~+/]{8,}=*/gi;
|
|
215
|
+
const EMAIL_RE = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g;
|
|
216
|
+
const CARD_CANDIDATE_RE = /\b(?:\d[ -]?){12,18}\d\b/g;
|
|
217
|
+
function luhnValid(digits) {
|
|
218
|
+
let total = 0;
|
|
219
|
+
for (let i = 0; i < digits.length; i++) {
|
|
220
|
+
let n = digits.charCodeAt(digits.length - 1 - i) - 48;
|
|
221
|
+
if (i % 2 === 1) {
|
|
222
|
+
n *= 2;
|
|
223
|
+
if (n > 9)
|
|
224
|
+
n -= 9;
|
|
225
|
+
}
|
|
226
|
+
total += n;
|
|
227
|
+
}
|
|
228
|
+
return total % 10 === 0;
|
|
229
|
+
}
|
|
230
|
+
/** Redact credentials, card numbers and email addresses from free text. Never throws. */
|
|
231
|
+
export function scrubText(text) {
|
|
232
|
+
if (typeof text !== 'string' || !text)
|
|
233
|
+
return text;
|
|
234
|
+
try {
|
|
235
|
+
let out = text;
|
|
236
|
+
for (const pattern of TOKEN_PATTERNS)
|
|
237
|
+
out = out.replace(pattern, FILTERED);
|
|
238
|
+
out = out.replace(AUTH_SCHEME_RE, (_m, scheme) => `${scheme} ${FILTERED}`);
|
|
239
|
+
out = out.replace(HEADER_LINE_RE, (_m, prefix) => `${prefix}${FILTERED}`);
|
|
240
|
+
out = out.replace(QUOTED_PAIR_RE, (_m, q, key, sep, vq) => `${q}${key}${q}${sep}${vq}${FILTERED}${vq}`);
|
|
241
|
+
out = out.replace(QUOTED_KEY_BARE_VALUE_RE, (_m, q, key, sep) => `${q}${key}${q}${sep}${FILTERED}`);
|
|
242
|
+
out = out.replace(ASSIGNMENT_RE, (_m, key) => `${key}=${FILTERED}`);
|
|
243
|
+
out = out.replace(CARD_CANDIDATE_RE, (m) => {
|
|
244
|
+
const digits = m.replace(/\D/g, '');
|
|
245
|
+
return digits.length >= 13 && digits.length <= 19 && luhnValid(digits) ? FILTERED : m;
|
|
246
|
+
});
|
|
247
|
+
out = out.replace(EMAIL_RE, EMAIL_PLACEHOLDER);
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
return '';
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** "https://user:pass@host/a/b?x=1#y" -> "https://host/a/b" (no credentials, query or fragment). */
|
|
255
|
+
export function sanitizeUrl(url) {
|
|
256
|
+
return stripQuery(String(url)).replace(/^([a-z][a-z0-9+.-]*:\/\/)[^/@]*@/i, '$1');
|
|
257
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StackFrame } from './types.js';
|
|
2
|
+
export declare const MAX_FRAMES = 50;
|
|
3
|
+
/** Frames from browser extensions (and Safari's masked extension URLs). */
|
|
4
|
+
export declare function isExtensionUrl(filename: string): boolean;
|
|
5
|
+
/** false for library code, runtime internals and browser extensions. */
|
|
6
|
+
export declare function isInApp(filename: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Parse error.stack (V8, Firefox or Safari format) into frames,
|
|
9
|
+
* oldest call first and innermost (where it failed) last. At most 50 frames (the innermost ones).
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseStack(stack: unknown): StackFrame[];
|
|
12
|
+
/** True when the stack looks like V8's, which already starts with "Type: message". */
|
|
13
|
+
export declare function isV8Stack(stack: string): boolean;
|