@watchtowerobservability/node 0.2.0 → 0.4.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/README.md +186 -3
- package/dist/cjs/appsec.d.ts +95 -0
- package/dist/cjs/appsec.js +206 -0
- package/dist/cjs/iast.d.ts +27 -0
- package/dist/cjs/iast.js +138 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +14 -1
- package/dist/cjs/integrations/amqp.d.ts +13 -0
- package/dist/cjs/integrations/amqp.js +96 -0
- package/dist/cjs/integrations/bullmq.d.ts +19 -0
- package/dist/cjs/integrations/bullmq.js +83 -0
- package/dist/cjs/integrations/cache.d.ts +13 -0
- package/dist/cjs/integrations/cache.js +51 -0
- package/dist/cjs/integrations/db.d.ts +62 -0
- package/dist/cjs/integrations/db.js +288 -0
- package/dist/cjs/integrations/duckdb.d.ts +22 -0
- package/dist/cjs/integrations/duckdb.js +66 -0
- package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
- package/dist/cjs/integrations/graphile-worker.js +50 -0
- package/dist/cjs/integrations/level.d.ts +18 -0
- package/dist/cjs/integrations/level.js +71 -0
- package/dist/cjs/integrations/lmdb.d.ts +26 -0
- package/dist/cjs/integrations/lmdb.js +78 -0
- package/dist/cjs/integrations/messaging.d.ts +17 -0
- package/dist/cjs/integrations/messaging.js +73 -0
- package/dist/cjs/integrations/mqtt.d.ts +7 -0
- package/dist/cjs/integrations/mqtt.js +41 -0
- package/dist/cjs/integrations/nats.d.ts +17 -0
- package/dist/cjs/integrations/nats.js +73 -0
- package/dist/cjs/integrations/orm.d.ts +47 -0
- package/dist/cjs/integrations/orm.js +93 -0
- package/dist/cjs/integrations/pg-boss.d.ts +8 -0
- package/dist/cjs/integrations/pg-boss.js +63 -0
- package/dist/cjs/integrations/pools.d.ts +41 -0
- package/dist/cjs/integrations/pools.js +94 -0
- package/dist/cjs/integrations/postgres.d.ts +12 -0
- package/dist/cjs/integrations/postgres.js +92 -0
- package/dist/cjs/integrations/sqlite.d.ts +36 -0
- package/dist/cjs/integrations/sqlite.js +125 -0
- package/dist/cjs/pprof.d.ts +6 -0
- package/dist/cjs/pprof.js +88 -0
- package/dist/cjs/profiling.d.ts +4 -1
- package/dist/cjs/profiling.js +18 -8
- package/dist/cjs/sql.d.ts +19 -0
- package/dist/cjs/sql.js +332 -0
- package/dist/cjs/test-plan.d.ts +10 -1
- package/dist/cjs/test-plan.js +91 -1
- package/dist/esm/appsec.d.ts +95 -0
- package/dist/esm/appsec.js +193 -0
- package/dist/esm/iast.d.ts +27 -0
- package/dist/esm/iast.js +130 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +14 -1
- package/dist/esm/integrations/amqp.d.ts +13 -0
- package/dist/esm/integrations/amqp.js +92 -0
- package/dist/esm/integrations/bullmq.d.ts +19 -0
- package/dist/esm/integrations/bullmq.js +79 -0
- package/dist/esm/integrations/cache.d.ts +13 -0
- package/dist/esm/integrations/cache.js +46 -0
- package/dist/esm/integrations/db.d.ts +62 -0
- package/dist/esm/integrations/db.js +239 -0
- package/dist/esm/integrations/duckdb.d.ts +22 -0
- package/dist/esm/integrations/duckdb.js +29 -0
- package/dist/esm/integrations/graphile-worker.d.ts +22 -0
- package/dist/esm/integrations/graphile-worker.js +46 -0
- package/dist/esm/integrations/level.d.ts +18 -0
- package/dist/esm/integrations/level.js +35 -0
- package/dist/esm/integrations/lmdb.d.ts +26 -0
- package/dist/esm/integrations/lmdb.js +42 -0
- package/dist/esm/integrations/messaging.d.ts +17 -0
- package/dist/esm/integrations/messaging.js +64 -0
- package/dist/esm/integrations/mqtt.d.ts +7 -0
- package/dist/esm/integrations/mqtt.js +38 -0
- package/dist/esm/integrations/nats.d.ts +17 -0
- package/dist/esm/integrations/nats.js +70 -0
- package/dist/esm/integrations/orm.d.ts +47 -0
- package/dist/esm/integrations/orm.js +86 -0
- package/dist/esm/integrations/pg-boss.d.ts +8 -0
- package/dist/esm/integrations/pg-boss.js +60 -0
- package/dist/esm/integrations/pools.d.ts +41 -0
- package/dist/esm/integrations/pools.js +89 -0
- package/dist/esm/integrations/postgres.d.ts +12 -0
- package/dist/esm/integrations/postgres.js +55 -0
- package/dist/esm/integrations/sqlite.d.ts +36 -0
- package/dist/esm/integrations/sqlite.js +84 -0
- package/dist/esm/pprof.d.ts +6 -0
- package/dist/esm/pprof.js +85 -0
- package/dist/esm/profiling.d.ts +4 -1
- package/dist/esm/profiling.js +17 -8
- package/dist/esm/sql.d.ts +19 -0
- package/dist/esm/sql.js +324 -0
- package/dist/esm/test-plan.d.ts +10 -1
- package/dist/esm/test-plan.js +89 -1
- package/package.json +171 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// App and API Protection in-app WAF (W52). `startAppsec()` polls the project's ruleset from the
|
|
2
|
+
// edge (`GET /v1/appsec/ruleset`, an ingestion key with scope `probes:read`, ETag, every `poll_s`):
|
|
3
|
+
// mode, enabled pattern rules and the denylist. `appsecMiddleware()` (Express, Connect, any
|
|
4
|
+
// `(req, res, next)` stack) or `handleRequest(req, res)` (plain `http`, Fastify `onRequest`, Koa)
|
|
5
|
+
// inspects each request before the handler: path, query, User-Agent, other header values and an
|
|
6
|
+
// already parsed body, percent-decoded once, against the same rules the worker runs over stored
|
|
7
|
+
// spans. A match is recorded on the active server span (`appsec.event`, `appsec.rule_id`,
|
|
8
|
+
// `appsec.category`, `appsec.blocked`); in `block` mode, and for denylisted IPs or users in any
|
|
9
|
+
// mode, the request is answered with 403 problem+json and the handler never runs.
|
|
10
|
+
// Nothing is fetched or inspected until `startAppsec` is called; with no ruleset yet every
|
|
11
|
+
// request passes. Rules are matched case-insensitively; a rule that does not compile is skipped.
|
|
12
|
+
import { trace } from '@opentelemetry/api';
|
|
13
|
+
/** Rule id recorded for denylist blocks (the worker reports them as their own signal). */
|
|
14
|
+
export const DENYLIST = 'denylist';
|
|
15
|
+
const BODY_MAX = 64 * 1024;
|
|
16
|
+
let mode = 'monitor';
|
|
17
|
+
let compiled = [];
|
|
18
|
+
let denyIp = new Map();
|
|
19
|
+
let denyUser = new Map();
|
|
20
|
+
const expiryMap = (list) => new Map((list || []).map((e) => [e.value, e.expires_at ? e.expires_at * 1000 : Infinity]));
|
|
21
|
+
/** Replace the active ruleset (what the poller calls; tests may call it directly). */
|
|
22
|
+
export function setRuleset(r) {
|
|
23
|
+
mode = r?.mode === 'block' ? 'block' : 'monitor';
|
|
24
|
+
compiled = [];
|
|
25
|
+
for (const rule of r?.rules || []) {
|
|
26
|
+
try {
|
|
27
|
+
compiled.push({ rule, re: new RegExp(rule.pattern, 'i') });
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// not portable to JavaScript: skipped here, the worker still evaluates it
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
denyIp = expiryMap(r?.denylist?.ip);
|
|
34
|
+
denyUser = expiryMap(r?.denylist?.user);
|
|
35
|
+
}
|
|
36
|
+
const listed = (m, v, now) => !!v && (m.get(v) ?? 0) > now;
|
|
37
|
+
/** Whether `id` is on the user denylist (call after authentication to refuse a listed user). */
|
|
38
|
+
export const isUserBlocked = (id, now = Date.now()) => listed(denyUser, id, now);
|
|
39
|
+
/** Judge one request. Denylisted callers are blocked in either mode. */
|
|
40
|
+
export function inspect(req, now = Date.now()) {
|
|
41
|
+
if (listed(denyIp, req.ip, now) || listed(denyUser, req.user, now)) {
|
|
42
|
+
return { action: 'block', rule_id: DENYLIST, category: 'abuse' };
|
|
43
|
+
}
|
|
44
|
+
for (const { rule, re } of compiled) {
|
|
45
|
+
for (const f of rule.fields) {
|
|
46
|
+
const v = req[f];
|
|
47
|
+
if (!v)
|
|
48
|
+
continue;
|
|
49
|
+
const m = re.exec(v);
|
|
50
|
+
if (m) {
|
|
51
|
+
return { action: mode === 'block' ? 'block' : 'monitor', rule_id: rule.id, category: rule.category, match: m[0].slice(0, 200) };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { action: 'allow' };
|
|
56
|
+
}
|
|
57
|
+
const decode = (s, form = false) => {
|
|
58
|
+
const x = form ? s.replace(/\+/g, ' ') : s;
|
|
59
|
+
try {
|
|
60
|
+
return decodeURIComponent(x);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return x;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const first = (v) => (Array.isArray(v) ? v[0] : v);
|
|
67
|
+
/** Client IP: first `X-Forwarded-For` hop, else `X-Real-IP`, else the socket peer.
|
|
68
|
+
* shortcut: trusts the forwarding headers; run behind a proxy that overwrites them. */
|
|
69
|
+
export function clientIp(req) {
|
|
70
|
+
const xff = first(req.headers?.['x-forwarded-for']);
|
|
71
|
+
if (xff)
|
|
72
|
+
return xff.split(',')[0].trim();
|
|
73
|
+
return first(req.headers?.['x-real-ip']) || req.socket?.remoteAddress?.replace(/^::ffff:/, '');
|
|
74
|
+
}
|
|
75
|
+
/** Decode a Node request into the inspector's fields. */
|
|
76
|
+
export function fromNode(req, user) {
|
|
77
|
+
const url = req.originalUrl || req.url || '/';
|
|
78
|
+
const q = url.indexOf('?');
|
|
79
|
+
const headers = Object.entries(req.headers || {})
|
|
80
|
+
.filter(([k]) => k !== 'user-agent')
|
|
81
|
+
.map(([, v]) => (Array.isArray(v) ? v.join(', ') : v || ''))
|
|
82
|
+
.join('\n');
|
|
83
|
+
let body;
|
|
84
|
+
if (typeof req.body === 'string')
|
|
85
|
+
body = req.body.slice(0, BODY_MAX);
|
|
86
|
+
else if (req.body && typeof req.body === 'object') {
|
|
87
|
+
try {
|
|
88
|
+
body = JSON.stringify(req.body).slice(0, BODY_MAX);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
body = undefined;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
path: decode(q < 0 ? url : url.slice(0, q)),
|
|
96
|
+
query: q < 0 ? '' : decode(url.slice(q + 1), true),
|
|
97
|
+
user_agent: first(req.headers?.['user-agent']) || '',
|
|
98
|
+
headers,
|
|
99
|
+
body,
|
|
100
|
+
ip: clientIp(req),
|
|
101
|
+
user,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** Record a decision on the active span. */
|
|
105
|
+
export function annotate(d, ip) {
|
|
106
|
+
const span = trace.getActiveSpan();
|
|
107
|
+
if (!span)
|
|
108
|
+
return;
|
|
109
|
+
if (ip)
|
|
110
|
+
span.setAttribute('appsec.client_ip', ip);
|
|
111
|
+
if (d.action === 'allow')
|
|
112
|
+
return;
|
|
113
|
+
span.setAttributes({
|
|
114
|
+
'appsec.event': true,
|
|
115
|
+
'appsec.rule_id': d.rule_id || '',
|
|
116
|
+
'appsec.category': d.category || '',
|
|
117
|
+
'appsec.blocked': d.action === 'block',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const PROBLEM = JSON.stringify({
|
|
121
|
+
type: 'about:blank',
|
|
122
|
+
title: 'Forbidden',
|
|
123
|
+
status: 403,
|
|
124
|
+
detail: 'Request blocked by Watchtower App and API Protection',
|
|
125
|
+
});
|
|
126
|
+
/** Inspect and, when blocked, answer 403. Returns true when the request was answered. */
|
|
127
|
+
export function handleRequest(req, res, user) {
|
|
128
|
+
if (!compiled.length && !denyIp.size && !denyUser.size)
|
|
129
|
+
return false;
|
|
130
|
+
const r = fromNode(req, user);
|
|
131
|
+
const d = inspect(r);
|
|
132
|
+
annotate(d, r.ip);
|
|
133
|
+
if (d.action !== 'block')
|
|
134
|
+
return false;
|
|
135
|
+
res.statusCode = 403;
|
|
136
|
+
res.setHeader?.('content-type', 'application/problem+json');
|
|
137
|
+
res.end(PROBLEM);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/** Connect-style middleware; `user(req)` names the caller when the app already knows it. */
|
|
141
|
+
export function appsecMiddleware(o = {}) {
|
|
142
|
+
return (req, res, next) => {
|
|
143
|
+
if (!handleRequest(req, res, o.user?.(req)))
|
|
144
|
+
next();
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Mark the current request as a login attempt; failed ones feed the account-takeover rules. */
|
|
148
|
+
export function trackLogin(login, success) {
|
|
149
|
+
const span = trace.getActiveSpan();
|
|
150
|
+
span?.setAttributes({ 'appsec.login': login, 'appsec.login.success': success });
|
|
151
|
+
if (success)
|
|
152
|
+
span?.setAttribute('appsec.auth', true);
|
|
153
|
+
}
|
|
154
|
+
/** Fetch the ruleset now and keep polling; `stop()` ends the polling. */
|
|
155
|
+
export function startAppsec(o = {}) {
|
|
156
|
+
const env = process.env;
|
|
157
|
+
const base = (o.apiUrl || env.WT_API_URL || 'https://api.watchtower.dev').replace(/\/$/, '');
|
|
158
|
+
const key = o.key || env.WT_INGEST_KEY || '';
|
|
159
|
+
const service = o.service || env.WT_SERVICE || env.OTEL_SERVICE_NAME || 'unknown';
|
|
160
|
+
const f = o.fetch || globalThis.fetch;
|
|
161
|
+
let etag = '';
|
|
162
|
+
let timer;
|
|
163
|
+
let stopped = false;
|
|
164
|
+
let pollS = 30;
|
|
165
|
+
const poll = async () => {
|
|
166
|
+
try {
|
|
167
|
+
const res = await f(`${base}/v1/appsec/ruleset?service=${encodeURIComponent(service)}`, {
|
|
168
|
+
headers: { Authorization: `Bearer ${key}`, ...(etag ? { 'If-None-Match': etag } : {}) },
|
|
169
|
+
});
|
|
170
|
+
if (res.status === 200) {
|
|
171
|
+
const body = (await res.json());
|
|
172
|
+
setRuleset(body);
|
|
173
|
+
etag = res.headers.get('etag') || '';
|
|
174
|
+
pollS = Math.max(10, body.poll_s || 30);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// keep the last ruleset; try again on the next poll
|
|
179
|
+
}
|
|
180
|
+
if (!stopped) {
|
|
181
|
+
timer = setTimeout(() => void poll(), pollS * 1000);
|
|
182
|
+
timer.unref?.();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
ready: poll(),
|
|
187
|
+
stop() {
|
|
188
|
+
stopped = true;
|
|
189
|
+
if (timer)
|
|
190
|
+
clearTimeout(timer);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface IastOptions {
|
|
2
|
+
/** API base URL, e.g. `https://api.example.com`. */
|
|
3
|
+
apiUrl: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
project: string;
|
|
6
|
+
service: string;
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
/** Flush delay in ms (default 2000). */
|
|
9
|
+
flushMs?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface TaintFinding {
|
|
12
|
+
vuln_type: string;
|
|
13
|
+
trace_id: string;
|
|
14
|
+
code_location: string;
|
|
15
|
+
severity: string;
|
|
16
|
+
title: string;
|
|
17
|
+
}
|
|
18
|
+
export declare function startIast(o: IastOptions): void;
|
|
19
|
+
export declare function stopIast(): void;
|
|
20
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
21
|
+
export declare function iastMiddleware(): (req: any, _res: unknown, next: (e?: unknown) => void) => void;
|
|
22
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
23
|
+
export declare function checkSink(vulnType: string, text: unknown, severity?: string): boolean;
|
|
24
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
25
|
+
export declare function guardSink<A extends unknown[], R>(fn: (...a: A) => R, vulnType: string): (...a: A) => R;
|
|
26
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
27
|
+
export declare function flushIast(): Promise<void>;
|
package/dist/esm/iast.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// IAST (opt-in): notices at runtime when request-derived input reaches a sensitive sink and reports
|
|
2
|
+
// it to `POST /v1/projects/{prj}/code-security/runtime` (`{service, findings:[{vuln_type, trace_id,
|
|
3
|
+
// code_location, severity, title}]}`). Off until `startIast` is called; until then `iastMiddleware`
|
|
4
|
+
// is a pass-through and `guardSink` calls the original function untouched.
|
|
5
|
+
//
|
|
6
|
+
// How: `iastMiddleware()` stores the request's input strings (query, params, body, cookies, header
|
|
7
|
+
// values) in an AsyncLocalStorage for the life of the request. `guardSink(fn, vulnType)` wraps a
|
|
8
|
+
// sink (`pool.query`, `child_process.exec`, `fs.readFile`, ...); before the call it checks whether
|
|
9
|
+
// any input string appears inside the sink's first argument. Parameterised queries keep the input
|
|
10
|
+
// in the values array, not the text, so they never match. A hit is queued with the `file:line` of
|
|
11
|
+
// the caller (first stack frame outside node_modules and this file) and the active trace id, and
|
|
12
|
+
// is flushed in the background. Each (type, location) is reported once per process.
|
|
13
|
+
// shortcut: taint is substring matching of raw input values (>= 4 chars) in the sink text, not
|
|
14
|
+
// byte-level propagation; input that is transformed (encoded, split) before use is missed.
|
|
15
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
16
|
+
import { trace } from '@opentelemetry/api';
|
|
17
|
+
const MIN_LEN = 4;
|
|
18
|
+
const MAX_INPUTS = 256;
|
|
19
|
+
const store = new AsyncLocalStorage();
|
|
20
|
+
let opts = null;
|
|
21
|
+
let queue = [];
|
|
22
|
+
let seen = new Set();
|
|
23
|
+
let timer = null;
|
|
24
|
+
export function startIast(o) {
|
|
25
|
+
opts = o;
|
|
26
|
+
}
|
|
27
|
+
export function stopIast() {
|
|
28
|
+
opts = null;
|
|
29
|
+
queue = [];
|
|
30
|
+
seen = new Set();
|
|
31
|
+
if (timer)
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
timer = null;
|
|
34
|
+
}
|
|
35
|
+
function collect(v, out, depth = 0) {
|
|
36
|
+
if (out.length >= MAX_INPUTS || depth > 4 || v == null)
|
|
37
|
+
return;
|
|
38
|
+
if (typeof v === 'string') {
|
|
39
|
+
if (v.length >= MIN_LEN)
|
|
40
|
+
out.push(v);
|
|
41
|
+
}
|
|
42
|
+
else if (Array.isArray(v)) {
|
|
43
|
+
for (const x of v)
|
|
44
|
+
collect(x, out, depth + 1);
|
|
45
|
+
}
|
|
46
|
+
else if (typeof v === 'object') {
|
|
47
|
+
for (const x of Object.values(v))
|
|
48
|
+
collect(x, out, depth + 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
52
|
+
export function iastMiddleware() {
|
|
53
|
+
return (req, _res, next) => {
|
|
54
|
+
if (!opts)
|
|
55
|
+
return next();
|
|
56
|
+
// Resolved at sink time: body parsers and routers run after this middleware.
|
|
57
|
+
const inputsFor = () => {
|
|
58
|
+
const inputs = [];
|
|
59
|
+
for (const k of ['query', 'params', 'body', 'cookies', 'headers'])
|
|
60
|
+
collect(req[k], inputs);
|
|
61
|
+
return inputs;
|
|
62
|
+
};
|
|
63
|
+
store.run(inputsFor, next);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function callerLocation() {
|
|
67
|
+
const frames = (new Error().stack || '').split('\n').slice(2);
|
|
68
|
+
for (const f of frames) {
|
|
69
|
+
if (f.includes('node_modules') || f.includes('node:internal') || /iast\.[jt]s/.test(f))
|
|
70
|
+
continue;
|
|
71
|
+
const m = /\(?([^()\s]+:\d+)(?::\d+)?\)?\s*$/.exec(f);
|
|
72
|
+
if (m)
|
|
73
|
+
return m[1].replace(/^file:\/\//, '');
|
|
74
|
+
}
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
78
|
+
export function checkSink(vulnType, text, severity = 'high') {
|
|
79
|
+
const inputsFor = store.getStore();
|
|
80
|
+
if (!opts || !inputsFor || typeof text !== 'string')
|
|
81
|
+
return false;
|
|
82
|
+
if (!inputsFor().some((v) => text.includes(v)))
|
|
83
|
+
return false;
|
|
84
|
+
const loc = callerLocation();
|
|
85
|
+
const key = `${vulnType}|${loc}`;
|
|
86
|
+
if (seen.has(key))
|
|
87
|
+
return true;
|
|
88
|
+
seen.add(key);
|
|
89
|
+
queue.push({
|
|
90
|
+
vuln_type: vulnType,
|
|
91
|
+
trace_id: trace.getActiveSpan()?.spanContext().traceId || '',
|
|
92
|
+
code_location: loc,
|
|
93
|
+
severity,
|
|
94
|
+
title: `Request input reaches ${vulnType.replace(/_/g, ' ')} sink`,
|
|
95
|
+
});
|
|
96
|
+
if (!timer) {
|
|
97
|
+
timer = setTimeout(() => void flushIast(), opts.flushMs ?? 2000);
|
|
98
|
+
timer.unref?.();
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
103
|
+
export function guardSink(fn, vulnType) {
|
|
104
|
+
return function (...a) {
|
|
105
|
+
const first = a[0];
|
|
106
|
+
checkSink(vulnType, typeof first === 'string' ? first : first?.text);
|
|
107
|
+
return fn.apply(this, a);
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
111
|
+
export async function flushIast() {
|
|
112
|
+
if (timer)
|
|
113
|
+
clearTimeout(timer);
|
|
114
|
+
timer = null;
|
|
115
|
+
const o = opts;
|
|
116
|
+
const findings = queue;
|
|
117
|
+
queue = [];
|
|
118
|
+
if (!o || findings.length === 0)
|
|
119
|
+
return;
|
|
120
|
+
try {
|
|
121
|
+
await (o.fetch || globalThis.fetch)(`${o.apiUrl.replace(/\/$/, '')}/v1/projects/${encodeURIComponent(o.project)}/code-security/runtime`, {
|
|
122
|
+
method: 'POST',
|
|
123
|
+
headers: { Authorization: `Bearer ${o.apiKey}`, 'Content-Type': 'application/json' },
|
|
124
|
+
body: JSON.stringify({ service: o.service, findings }),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
catch (_e) {
|
|
128
|
+
/* best effort */
|
|
129
|
+
}
|
|
130
|
+
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { LoggerProvider } from '@opentelemetry/sdk-logs';
|
|
|
6
6
|
import type { MeterProvider } from '@opentelemetry/sdk-metrics';
|
|
7
7
|
import { captureException } from './errors.js';
|
|
8
8
|
import type { CaptureContext } from './errors.js';
|
|
9
|
+
import type { Profiler } from './profiling.js';
|
|
9
10
|
export { captureException, metrics, trace };
|
|
10
11
|
export { setCaptureRules, capturePayload } from './payload.js';
|
|
11
12
|
export type { CaptureRule } from './payload.js';
|
|
@@ -86,6 +87,8 @@ export declare class Client {
|
|
|
86
87
|
readonly meterProvider?: MeterProvider;
|
|
87
88
|
private readonly stopRuntime;
|
|
88
89
|
uninstall?: () => void;
|
|
90
|
+
/** Set when WT_PROFILING=1 started the continuous profiler. */
|
|
91
|
+
profiler?: Promise<Profiler | undefined>;
|
|
89
92
|
constructor(opts: InitOptions);
|
|
90
93
|
/** Emit an OTLP log record; trace and span ids come from the active context. */
|
|
91
94
|
log(level: LogLevel, body: string, attributes?: Record<string, unknown>): void;
|
package/dist/esm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import { startMetrics, switchOn } from './metrics.js';
|
|
|
13
13
|
import * as nodePath from 'node:path';
|
|
14
14
|
import { captureException, installProcessHandlers, dropReserved } from './errors.js';
|
|
15
15
|
import { loadCaptureRules } from './payload.js';
|
|
16
|
+
import { startProfiling } from './profiling.js';
|
|
16
17
|
export { captureException, metrics, trace };
|
|
17
18
|
export { setCaptureRules, capturePayload } from './payload.js';
|
|
18
19
|
export const DEFAULT_ENDPOINT = 'http://localhost:4318';
|
|
@@ -37,6 +38,8 @@ export class Client {
|
|
|
37
38
|
meterProvider;
|
|
38
39
|
stopRuntime = () => { };
|
|
39
40
|
uninstall;
|
|
41
|
+
/** Set when WT_PROFILING=1 started the continuous profiler. */
|
|
42
|
+
profiler;
|
|
40
43
|
constructor(opts) {
|
|
41
44
|
const o = opts || {};
|
|
42
45
|
const env = process.env;
|
|
@@ -206,7 +209,7 @@ export class Client {
|
|
|
206
209
|
}
|
|
207
210
|
shutdown() {
|
|
208
211
|
this.stopRuntime();
|
|
209
|
-
return Promise.all([this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
|
|
212
|
+
return Promise.all([this.profiler?.then((p) => p?.stop()), this.provider.shutdown(), this.loggerProvider.shutdown(), this.meterProvider?.shutdown()]).then(() => undefined);
|
|
210
213
|
}
|
|
211
214
|
}
|
|
212
215
|
function redactor(hook) {
|
|
@@ -224,6 +227,7 @@ let current;
|
|
|
224
227
|
export function init(opts) {
|
|
225
228
|
if (current && current.uninstall)
|
|
226
229
|
current.uninstall();
|
|
230
|
+
void current?.profiler?.then((p) => p?.stop());
|
|
227
231
|
const c = new Client(opts);
|
|
228
232
|
current = c;
|
|
229
233
|
c.provider.register(); // global tracer + async context so trace.getActiveSpan() works
|
|
@@ -231,6 +235,15 @@ export function init(opts) {
|
|
|
231
235
|
metrics.disable(); // a re-init replaces the previous client's provider
|
|
232
236
|
metrics.setGlobalMeterProvider(c.meterProvider);
|
|
233
237
|
}
|
|
238
|
+
if (process.env.WT_PROFILING === '1') {
|
|
239
|
+
const attrs = c.provider.resource.attributes;
|
|
240
|
+
c.profiler = startProfiling({
|
|
241
|
+
apiKey: c.headers.Authorization.slice('Bearer '.length),
|
|
242
|
+
endpoint: c.endpoint,
|
|
243
|
+
service: String(attrs['service.name']),
|
|
244
|
+
tags: { version: attrs['service.version'] },
|
|
245
|
+
}).catch(() => undefined); // best effort: never break init
|
|
246
|
+
}
|
|
234
247
|
if ((opts || {}).captureProcessErrors !== false) {
|
|
235
248
|
c.uninstall = installProcessHandlers(() => c.forceFlush());
|
|
236
249
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
3
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
4
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
5
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
6
|
+
*/
|
|
7
|
+
export declare function instrumentAmqplib(channel: object): void;
|
|
8
|
+
/**
|
|
9
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
10
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
11
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
12
|
+
*/
|
|
13
|
+
export declare function instrumentRabbitmqClient(connection: object): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { patch } from './db.js';
|
|
2
|
+
import { consume, produce } from './messaging.js';
|
|
3
|
+
const sendAttrs = (dest, routingKey, op, messageId, size) => ({
|
|
4
|
+
'messaging.system': 'rabbitmq',
|
|
5
|
+
'messaging.destination.name': dest,
|
|
6
|
+
'messaging.operation.type': 'send',
|
|
7
|
+
'messaging.operation.name': op,
|
|
8
|
+
'messaging.rabbitmq.destination.routing_key': routingKey,
|
|
9
|
+
...(typeof messageId === 'string' ? { 'messaging.message.id': messageId } : {}),
|
|
10
|
+
...(typeof size === 'number' ? { 'messaging.message.body.size': size } : {}),
|
|
11
|
+
});
|
|
12
|
+
function processAttrs(queue, m) {
|
|
13
|
+
const a = {
|
|
14
|
+
'messaging.system': 'rabbitmq',
|
|
15
|
+
'messaging.destination.name': queue,
|
|
16
|
+
'messaging.operation.type': 'process',
|
|
17
|
+
'messaging.operation.name': 'consume',
|
|
18
|
+
};
|
|
19
|
+
if (m.properties?.messageId)
|
|
20
|
+
a['messaging.message.id'] = m.properties.messageId;
|
|
21
|
+
if (typeof m.content?.length === 'number')
|
|
22
|
+
a['messaging.message.body.size'] = m.content.length;
|
|
23
|
+
if (m.fields?.deliveryTag !== undefined)
|
|
24
|
+
a['messaging.rabbitmq.message.delivery_tag'] = m.fields.deliveryTag;
|
|
25
|
+
if (m.fields?.redelivered !== undefined)
|
|
26
|
+
a['messaging.rabbitmq.message.redelivered'] = m.fields.redelivered;
|
|
27
|
+
if (m.fields?.consumerTag)
|
|
28
|
+
a['messaging.consumer.group.name'] = m.fields.consumerTag;
|
|
29
|
+
return a;
|
|
30
|
+
}
|
|
31
|
+
const withHeaders = (o, h) => ({ ...o, headers: { ...o?.headers, ...h } });
|
|
32
|
+
/**
|
|
33
|
+
* `instrumentAmqplib(await conn.createChannel())`: patches that channel's class (every channel of
|
|
34
|
+
* it): PRODUCER spans on `publish`/`sendToQueue` with the trace context in `options.headers`, and a
|
|
35
|
+
* CONSUMER span (child of and linked to the producer) around each `consume` callback.
|
|
36
|
+
* shortcut: a ConfirmChannel is its own class; pass one of those too if you use both kinds.
|
|
37
|
+
*/
|
|
38
|
+
export function instrumentAmqplib(channel) {
|
|
39
|
+
const proto = Object.getPrototypeOf(channel);
|
|
40
|
+
patch(proto, 'publish', (orig) => function (exchange, routingKey, content, options, ...rest) {
|
|
41
|
+
const dest = exchange || routingKey;
|
|
42
|
+
return produce(`publish ${dest}`, sendAttrs(dest, routingKey, 'publish', options?.messageId, content?.length), (h) => orig.call(this, exchange, routingKey, content, withHeaders(options, h), ...rest));
|
|
43
|
+
});
|
|
44
|
+
// amqplib's sendToQueue calls publish('', queue): that inner call adds no second span
|
|
45
|
+
patch(proto, 'sendToQueue', (orig) => function (queue, content, options, ...rest) {
|
|
46
|
+
return produce(`publish ${queue}`, sendAttrs(queue, queue, 'sendToQueue', options?.messageId, content?.length), (h) => orig.call(this, queue, content, withHeaders(options, h), ...rest));
|
|
47
|
+
});
|
|
48
|
+
patch(proto, 'consume', (orig) => function (queue, onMessage, ...rest) {
|
|
49
|
+
if (typeof onMessage !== 'function')
|
|
50
|
+
return orig.call(this, queue, onMessage, ...rest);
|
|
51
|
+
const wrapped = (m) => (m
|
|
52
|
+
? consume(`process ${queue}`, processAttrs(queue, m), [m.properties?.headers], () => onMessage(m))
|
|
53
|
+
: onMessage(m)); // null: the server cancelled the consumer
|
|
54
|
+
return orig.call(this, queue, wrapped, ...rest);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* `instrumentRabbitmqClient(new Connection(url))` for rabbitmq-client: publishers made by
|
|
59
|
+
* `createPublisher` get PRODUCER spans on `send` (context in the envelope headers); handlers given
|
|
60
|
+
* to `createConsumer` run in a CONSUMER span. Call it before creating publishers and consumers.
|
|
61
|
+
*/
|
|
62
|
+
export function instrumentRabbitmqClient(connection) {
|
|
63
|
+
patch(connection, 'createPublisher', (orig) => function (...a) {
|
|
64
|
+
const pub = orig.apply(this, a);
|
|
65
|
+
patch(pub, 'send', (send) => function (envelope, body, ...rest) {
|
|
66
|
+
const env = typeof envelope === 'string' ? { routingKey: envelope } : { ...envelope };
|
|
67
|
+
const rk = env.routingKey ?? '';
|
|
68
|
+
const dest = env.exchange || rk;
|
|
69
|
+
const size = typeof body === 'string' || body instanceof Uint8Array ? body.length : undefined;
|
|
70
|
+
return produce(`publish ${dest}`, sendAttrs(dest, rk, 'send', env.messageId, size), (h) => send.call(this, { ...env, headers: { ...env.headers, ...h } }, body, ...rest));
|
|
71
|
+
});
|
|
72
|
+
return pub;
|
|
73
|
+
});
|
|
74
|
+
patch(connection, 'createConsumer', (orig) => function (props, handler, ...rest) {
|
|
75
|
+
if (typeof handler !== 'function')
|
|
76
|
+
return orig.call(this, props, handler, ...rest);
|
|
77
|
+
const queue = (typeof props === 'string' ? props : props?.queue) ?? '';
|
|
78
|
+
const wrapped = function (m, ...more) {
|
|
79
|
+
const a = {
|
|
80
|
+
'messaging.system': 'rabbitmq',
|
|
81
|
+
'messaging.destination.name': queue,
|
|
82
|
+
'messaging.operation.type': 'process',
|
|
83
|
+
'messaging.operation.name': 'consume',
|
|
84
|
+
...(m?.messageId ? { 'messaging.message.id': m.messageId } : {}),
|
|
85
|
+
...(m?.deliveryTag !== undefined ? { 'messaging.rabbitmq.message.delivery_tag': m.deliveryTag } : {}),
|
|
86
|
+
...(m?.redelivered !== undefined ? { 'messaging.rabbitmq.message.redelivered': m.redelivered } : {}),
|
|
87
|
+
};
|
|
88
|
+
return consume(`process ${queue}`, a, [m?.headers], () => handler.call(this, m, ...more));
|
|
89
|
+
};
|
|
90
|
+
return orig.call(this, props, wrapped, ...rest);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CONTEXT_KEY } from './messaging.js';
|
|
2
|
+
export { CONTEXT_KEY };
|
|
3
|
+
interface BullMQLike {
|
|
4
|
+
Queue: {
|
|
5
|
+
prototype: Record<string | symbol, unknown>;
|
|
6
|
+
};
|
|
7
|
+
Worker: {
|
|
8
|
+
prototype: Record<string | symbol, unknown>;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
13
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
14
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
15
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
16
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
17
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
18
|
+
*/
|
|
19
|
+
export declare function instrumentBullMQ(bullmq: BullMQLike): boolean;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { context, propagation, trace, SpanKind, SpanStatusCode } from '@opentelemetry/api';
|
|
2
|
+
import { CONTEXT_KEY, injectData as inject, isPlainObject } from './messaging.js';
|
|
3
|
+
export { CONTEXT_KEY };
|
|
4
|
+
const PATCHED = Symbol.for('watchtower.bullmq');
|
|
5
|
+
const tracer = () => trace.getTracer('watchtower');
|
|
6
|
+
const base = (queue, op, name) => ({
|
|
7
|
+
'messaging.system': 'bullmq',
|
|
8
|
+
'messaging.destination.name': queue,
|
|
9
|
+
'messaging.operation.type': op,
|
|
10
|
+
'messaging.operation.name': name,
|
|
11
|
+
});
|
|
12
|
+
async function run(span, fn) {
|
|
13
|
+
try {
|
|
14
|
+
return await fn();
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
span.recordException(e);
|
|
18
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: e?.message });
|
|
19
|
+
throw e;
|
|
20
|
+
}
|
|
21
|
+
finally {
|
|
22
|
+
span.end();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* `import * as bullmq from 'bullmq'; instrumentBullMQ(bullmq)` (ESM or CJS): PRODUCER spans on
|
|
27
|
+
* `Queue.add`/`addBulk`, CONSUMER spans around each job a `Worker` processes, parented by the
|
|
28
|
+
* producer's context carried in job data under `_wt` (stripped before your processor runs; object
|
|
29
|
+
* data only). Call it once, before creating queues and workers. Returns false when this bullmq
|
|
30
|
+
* has no `Worker.callProcessJob` (consumer spans off; producer spans still on).
|
|
31
|
+
* shortcut: sandboxed (file path) processors run in a child process and get no consumer span.
|
|
32
|
+
*/
|
|
33
|
+
export function instrumentBullMQ(bullmq) {
|
|
34
|
+
const q = bullmq.Queue.prototype;
|
|
35
|
+
const w = bullmq.Worker.prototype;
|
|
36
|
+
if (q[PATCHED])
|
|
37
|
+
return typeof w.callProcessJob === 'function';
|
|
38
|
+
q[PATCHED] = true;
|
|
39
|
+
const add = q.add;
|
|
40
|
+
q.add = function (name, data, opts) {
|
|
41
|
+
const attrs = { ...base(this.name, 'send', 'add'), 'messaging.bullmq.job.name': name };
|
|
42
|
+
if (opts?.delay)
|
|
43
|
+
attrs['messaging.bullmq.job.delay_ms'] = opts.delay;
|
|
44
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, async () => {
|
|
45
|
+
const job = (await add.call(this, name, inject(data), opts));
|
|
46
|
+
if (job?.id)
|
|
47
|
+
span.setAttribute('messaging.message.id', job.id);
|
|
48
|
+
return job;
|
|
49
|
+
}));
|
|
50
|
+
};
|
|
51
|
+
const addBulk = q.addBulk;
|
|
52
|
+
if (addBulk) {
|
|
53
|
+
q.addBulk = function (jobs) {
|
|
54
|
+
const attrs = { ...base(this.name, 'send', 'addBulk'), 'messaging.batch.message_count': jobs.length };
|
|
55
|
+
return tracer().startActiveSpan(`send ${this.name}`, { kind: SpanKind.PRODUCER, attributes: attrs }, (span) => run(span, () => addBulk.call(this, jobs.map((j) => ({ ...j, data: inject(j.data) })))));
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const call = w.callProcessJob;
|
|
59
|
+
if (typeof call !== 'function')
|
|
60
|
+
return false;
|
|
61
|
+
w.callProcessJob = function (job, ...rest) {
|
|
62
|
+
let parent = context.active();
|
|
63
|
+
if (isPlainObject(job.data) && isPlainObject(job.data[CONTEXT_KEY])) {
|
|
64
|
+
parent = propagation.extract(parent, job.data[CONTEXT_KEY]);
|
|
65
|
+
delete job.data[CONTEXT_KEY];
|
|
66
|
+
}
|
|
67
|
+
const attrs = {
|
|
68
|
+
...base(this.name, 'process', 'process'),
|
|
69
|
+
'messaging.bullmq.job.name': job.name,
|
|
70
|
+
'messaging.bullmq.job.attempt': (job.attemptsMade ?? 0) + 1,
|
|
71
|
+
};
|
|
72
|
+
if (job.id)
|
|
73
|
+
attrs['messaging.message.id'] = job.id;
|
|
74
|
+
if (job.opts?.delay)
|
|
75
|
+
attrs['messaging.bullmq.job.delay_ms'] = job.opts.delay;
|
|
76
|
+
return tracer().startActiveSpan(`process ${this.name}`, { kind: SpanKind.CONSUMER, attributes: attrs }, parent, (span) => run(span, () => call.call(this, job, ...rest)));
|
|
77
|
+
};
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Hit/miss counters and entry count for a node-cache instance (`get`, `mget`). Returns `stop()`. */
|
|
2
|
+
export declare function instrumentNodeCache(cache: {
|
|
3
|
+
getStats?(): {
|
|
4
|
+
keys?: number;
|
|
5
|
+
};
|
|
6
|
+
}, name?: string): () => void;
|
|
7
|
+
/** Hit/miss counters and entry count for an lru-cache instance (`get`). Returns `stop()`.
|
|
8
|
+
* shortcut: `fetch()` (the async loader) is not counted. */
|
|
9
|
+
export declare function instrumentLruCache(cache: {
|
|
10
|
+
size?: number;
|
|
11
|
+
}, name?: string): () => void;
|
|
12
|
+
/** Hit/miss counters for a Keyv instance (`get` with one key or an array; also `getMany`). */
|
|
13
|
+
export declare function instrumentKeyv(keyv: object, name?: string): void;
|