@watchtowerobservability/node 0.3.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/dist/cjs/iast.d.ts +27 -0
- package/dist/cjs/iast.js +138 -0
- package/dist/esm/iast.d.ts +27 -0
- package/dist/esm/iast.js +130 -0
- package/package.json +11 -1
|
@@ -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/cjs/iast.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startIast = startIast;
|
|
4
|
+
exports.stopIast = stopIast;
|
|
5
|
+
exports.iastMiddleware = iastMiddleware;
|
|
6
|
+
exports.checkSink = checkSink;
|
|
7
|
+
exports.guardSink = guardSink;
|
|
8
|
+
exports.flushIast = flushIast;
|
|
9
|
+
// IAST (opt-in): notices at runtime when request-derived input reaches a sensitive sink and reports
|
|
10
|
+
// it to `POST /v1/projects/{prj}/code-security/runtime` (`{service, findings:[{vuln_type, trace_id,
|
|
11
|
+
// code_location, severity, title}]}`). Off until `startIast` is called; until then `iastMiddleware`
|
|
12
|
+
// is a pass-through and `guardSink` calls the original function untouched.
|
|
13
|
+
//
|
|
14
|
+
// How: `iastMiddleware()` stores the request's input strings (query, params, body, cookies, header
|
|
15
|
+
// values) in an AsyncLocalStorage for the life of the request. `guardSink(fn, vulnType)` wraps a
|
|
16
|
+
// sink (`pool.query`, `child_process.exec`, `fs.readFile`, ...); before the call it checks whether
|
|
17
|
+
// any input string appears inside the sink's first argument. Parameterised queries keep the input
|
|
18
|
+
// in the values array, not the text, so they never match. A hit is queued with the `file:line` of
|
|
19
|
+
// the caller (first stack frame outside node_modules and this file) and the active trace id, and
|
|
20
|
+
// is flushed in the background. Each (type, location) is reported once per process.
|
|
21
|
+
// shortcut: taint is substring matching of raw input values (>= 4 chars) in the sink text, not
|
|
22
|
+
// byte-level propagation; input that is transformed (encoded, split) before use is missed.
|
|
23
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
24
|
+
const api_1 = require("@opentelemetry/api");
|
|
25
|
+
const MIN_LEN = 4;
|
|
26
|
+
const MAX_INPUTS = 256;
|
|
27
|
+
const store = new node_async_hooks_1.AsyncLocalStorage();
|
|
28
|
+
let opts = null;
|
|
29
|
+
let queue = [];
|
|
30
|
+
let seen = new Set();
|
|
31
|
+
let timer = null;
|
|
32
|
+
function startIast(o) {
|
|
33
|
+
opts = o;
|
|
34
|
+
}
|
|
35
|
+
function stopIast() {
|
|
36
|
+
opts = null;
|
|
37
|
+
queue = [];
|
|
38
|
+
seen = new Set();
|
|
39
|
+
if (timer)
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
timer = null;
|
|
42
|
+
}
|
|
43
|
+
function collect(v, out, depth = 0) {
|
|
44
|
+
if (out.length >= MAX_INPUTS || depth > 4 || v == null)
|
|
45
|
+
return;
|
|
46
|
+
if (typeof v === 'string') {
|
|
47
|
+
if (v.length >= MIN_LEN)
|
|
48
|
+
out.push(v);
|
|
49
|
+
}
|
|
50
|
+
else if (Array.isArray(v)) {
|
|
51
|
+
for (const x of v)
|
|
52
|
+
collect(x, out, depth + 1);
|
|
53
|
+
}
|
|
54
|
+
else if (typeof v === 'object') {
|
|
55
|
+
for (const x of Object.values(v))
|
|
56
|
+
collect(x, out, depth + 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Express/Connect middleware: marks this request's inputs as tainted for downstream sinks. */
|
|
60
|
+
function iastMiddleware() {
|
|
61
|
+
return (req, _res, next) => {
|
|
62
|
+
if (!opts)
|
|
63
|
+
return next();
|
|
64
|
+
// Resolved at sink time: body parsers and routers run after this middleware.
|
|
65
|
+
const inputsFor = () => {
|
|
66
|
+
const inputs = [];
|
|
67
|
+
for (const k of ['query', 'params', 'body', 'cookies', 'headers'])
|
|
68
|
+
collect(req[k], inputs);
|
|
69
|
+
return inputs;
|
|
70
|
+
};
|
|
71
|
+
store.run(inputsFor, next);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function callerLocation() {
|
|
75
|
+
const frames = (new Error().stack || '').split('\n').slice(2);
|
|
76
|
+
for (const f of frames) {
|
|
77
|
+
if (f.includes('node_modules') || f.includes('node:internal') || /iast\.[jt]s/.test(f))
|
|
78
|
+
continue;
|
|
79
|
+
const m = /\(?([^()\s]+:\d+)(?::\d+)?\)?\s*$/.exec(f);
|
|
80
|
+
if (m)
|
|
81
|
+
return m[1].replace(/^file:\/\//, '');
|
|
82
|
+
}
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
/** Check a sink's text against the current request's inputs; queue a finding on a hit. */
|
|
86
|
+
function checkSink(vulnType, text, severity = 'high') {
|
|
87
|
+
const inputsFor = store.getStore();
|
|
88
|
+
if (!opts || !inputsFor || typeof text !== 'string')
|
|
89
|
+
return false;
|
|
90
|
+
if (!inputsFor().some((v) => text.includes(v)))
|
|
91
|
+
return false;
|
|
92
|
+
const loc = callerLocation();
|
|
93
|
+
const key = `${vulnType}|${loc}`;
|
|
94
|
+
if (seen.has(key))
|
|
95
|
+
return true;
|
|
96
|
+
seen.add(key);
|
|
97
|
+
queue.push({
|
|
98
|
+
vuln_type: vulnType,
|
|
99
|
+
trace_id: api_1.trace.getActiveSpan()?.spanContext().traceId || '',
|
|
100
|
+
code_location: loc,
|
|
101
|
+
severity,
|
|
102
|
+
title: `Request input reaches ${vulnType.replace(/_/g, ' ')} sink`,
|
|
103
|
+
});
|
|
104
|
+
if (!timer) {
|
|
105
|
+
timer = setTimeout(() => void flushIast(), opts.flushMs ?? 2000);
|
|
106
|
+
timer.unref?.();
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
/** Wrap a sink function whose first argument is the sensitive text (SQL, command, path, URL). */
|
|
111
|
+
function guardSink(fn, vulnType) {
|
|
112
|
+
return function (...a) {
|
|
113
|
+
const first = a[0];
|
|
114
|
+
checkSink(vulnType, typeof first === 'string' ? first : first?.text);
|
|
115
|
+
return fn.apply(this, a);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** Send queued findings now. Never throws; a failed send drops the batch (IAST is best effort). */
|
|
119
|
+
async function flushIast() {
|
|
120
|
+
if (timer)
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
timer = null;
|
|
123
|
+
const o = opts;
|
|
124
|
+
const findings = queue;
|
|
125
|
+
queue = [];
|
|
126
|
+
if (!o || findings.length === 0)
|
|
127
|
+
return;
|
|
128
|
+
try {
|
|
129
|
+
await (o.fetch || globalThis.fetch)(`${o.apiUrl.replace(/\/$/, '')}/v1/projects/${encodeURIComponent(o.project)}/code-security/runtime`, {
|
|
130
|
+
method: 'POST',
|
|
131
|
+
headers: { Authorization: `Bearer ${o.apiKey}`, 'Content-Type': 'application/json' },
|
|
132
|
+
body: JSON.stringify({ service: o.service, findings }),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (_e) {
|
|
136
|
+
/* best effort */
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@watchtowerobservability/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
|
|
@@ -37,6 +37,16 @@
|
|
|
37
37
|
"default": "./dist/cjs/appsec.js"
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
|
+
"./iast": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/esm/iast.d.ts",
|
|
43
|
+
"default": "./dist/esm/iast.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/cjs/iast.d.ts",
|
|
47
|
+
"default": "./dist/cjs/iast.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
40
50
|
"./probes": {
|
|
41
51
|
"import": {
|
|
42
52
|
"types": "./dist/esm/probes.d.ts",
|