Node.js core binding (N1)
Ready
The hub proposes this hook for the containers it fits, and nothing changes until a player approves the proposal (Hook proposals). The cheat sheet has the steps by hand.
The base of every JavaScript hook package. It loads vulnboxd's core library into the Node.js process as a native addon, keeps the current request in an AsyncLocalStorage, and observes the few places where a Node service starts processes, opens connections and opens files, so each of those can be linked to the request that caused it. It also marks the requests of Node's own http/https servers, so every framework built on them (Express, Koa, Fastify, Hapi, NestJS, Next.js, … — see N2) is covered without anything framework-specific. It gets in with a compose-only change: a read-only mount of the hook directory and one --require in NODE_OPTIONS. The image and the source stay untouched.
The same change also loads N2 (http2 servers) and N3 (WebSockets, raw TCP/TLS, UDP) when they are installed next to it. N4 (Bun) and N5 (Deno) reuse this binding through their own preloads.
Covers
| Versions | |
|---|---|
| Node.js | 16, 18, 20, 22, 24 (N-API 8+) |
| C library | glibc (Debian) and musl (Alpine); x86_64 (aarch64: same code, the core ships for both) |
| Module systems | CommonJS and ES modules services; --require works for both |
| Dependencies | none (Node's own modules only) |
| Servers | node:http, node:https (and everything built on them); http2 through N2 |
The N1 sample service runs the same scenario on Node 16, 18, 20, 22 and 24 (official node:*-alpine binaries, musl) in one container, and on Node 22 on Debian (glibc).
What the kernel already links without any hook: a process per connection (cluster workers are processes, but each serves many connections, so they still need the hook) and services that run nothing on behalf of a request. Every ordinary Node service serves all its requests on one event-loop thread, so the kernel alone cannot tell which request a spawn, connect or open belonged to — that is what N1 adds.
How it gets in
From a registry (local development, or a build with network; the same release as the hook directory): npm install vbxtrace@1.0.0, then node --require vbxtrace/register app.js (ESM: --import vbxtrace/register.mjs); see Local development.
Compose-only. The hub proposes (template hooks/node/n1/proposals/compose.toml):
services:
<service>:
+ volumes:
+ - /usr/local/lib/vulnboxd/trace:/opt/vbxtrace:ro
+ environment:
+ NODE_OPTIONS: --require /opt/vbxtrace/node/register.jsIf the service already sets NODE_OPTIONS (in compose, or ENV NODE_OPTIONS=… in its image), the hub appends with a space (NODE_OPTIONS: --max-old-space-size=512 --require /opt/vbxtrace/node/register.js) and repeats the image's value, because a compose value replaces the image's.
Both lines belong together. Node refuses to start when a --require file is missing, so the mount and the variable are always added (and removed) as one change. Reverting the change removes the hook completely.
Alternative for Node ≥ 20.6 (template compose-esm.toml, ranked lower): NODE_OPTIONS: --import /opt/vbxtrace/node/register.mjs. It installs the same hooks; use it only when something filters --require out of NODE_OPTIONS. Giving both is harmless (the hooks install once).
NODE_OPTIONS is inherited by child processes: Node children (npm start → node server.js, cluster workers, worker_threads) load the hook too; non-Node children ignore it.
The hub proposes this when it detects: package.json or a lockfile (package-lock.json, yarn.lock, pnpm-lock.yaml), a node:* base image (also keymetrics/pm2, ghost, nodered/*), a command running node, npm start, npx, yarn start, pnpm start, pm2-runtime, nodemon, ts-node or tsx, or a dependency on a known Node framework.
What happens at startup (register.js):
VBXTRACE=0→ return, nothing loaded.- Load
/opt/vbxtrace/libvbxtrace-<arch>.sowithprocess.dlopen(the core doubles as an N-API addon; no compiler, no.nodebuild).VBXTRACE_LIBcan point elsewhere. Missing core or unsupported architecture → return, nothing wrapped. - Install the hooks below, then N2 and N3 if
node/n2,node/n3exist, thenmodule.syncBuiltinESMExports()soimport { openSync } from 'node:fs'sees the wrappers. - Inside a worker thread, take the worker's own context (see below).
Source change
Not needed for any Node server: every request path goes through node:http/https/http2/net. Code that wants to mark its own units of work can use the binding directly:
const vbx = require('/opt/vbxtrace/node/vbxtrace.js');
if (vbx.active()) {
const c = vbx.beginRequest(socket, vbx.PROTOCOL.RAW); // accepted socket; index counted per socket
vbx.runIn(c, handler, null, [socket]);
// … vbx.endRequest(c, 0) when done
}What it records
| Event | Emitted from | Join key |
|---|---|---|
request_begin | the http.server.request.start diagnostics channel (Node ≥ 16.17 / 18.7; older: a wrap of Server.prototype.emit('request')), synchronously before the 'request' event | accepted socket's local port, peer address and port, request index on the connection (count of requests on that socket), protocol http1 |
request_end | the http.server.response.finish channel (the response's 'finish'), or the response's 'close' when it never finished (status 0) | context id |
spawned | ChildProcess.prototype.spawn (every async child: spawn, exec, execFile, fork), after it returns, with child.pid | child pid |
context_enter / context_exit | around spawnSync, execSync, execFileSync, readFileSync, writeFileSync, appendFileSync, copyFileSync, cpSync, truncateSync — the event-loop thread does the fork or open itself and runs nothing else meanwhile | thread (pid + tid) between the two |
opened | fs.openSync (after it returns), fs.open (its callback is wrapped to report the fd first; fs.writeFile, fs.appendFile, createWriteStream, createReadStream use it), fs.promises.open, and FileHandle.prototype.fd (read by fs.promises.writeFile/readFile/appendFile while the file is open; flags unknown) | pid + fd; flags are the open(2) flags of the fs flag string ('w' → 0x241), or 0xffffffff |
connected | net.Socket.prototype.connect (every client: net.connect, http.Agent, tls.connect, new net.Socket().connect() as used by pg), on the socket's 'connect' | local port (TCP; transport unix for unix sockets) |
handoff | a worker_threads.Worker created during a request: parent = the request, child = the worker's context | context ids |
Only events on behalf of a request are reported; with no current request nothing is emitted (a context id of 0 carries no link).
The libuv thread pool performs async fs syscalls on other threads, so the sensor joins opened by pid + fd, never by thread. The fd is reported while the file is still open, so the join is exact.
Why some of these are wraps rather than diagnostics channels: Node 18 publishes no child_process channel; net.client.socket is only published by net.connect() and misses tls.connect and drivers that construct their own socket.
Context following
The current request is an AsyncLocalStorage store, entered in the http.server.request.start subscriber with enterWith, so the request handler and every callback, timer, promise and stream it starts carry it. The request and response objects are also bound (their emit runs inside the request's context), because Node 24's AsyncContextFrame does not carry enterWith into later socket callbacks such as the request body's 'end' — the sample checks work done in 'end' and a timer started there on every version.
AsyncLocalStorage is only used while a tracer is attached; when the sensor detaches the binding calls als.disable() (on Node < 24 it otherwise keeps a promise hook installed).
Worker threads: a worker's JavaScript runs in its own realm and does not inherit the context. The worker (which loads the hook through the inherited NODE_OPTIONS) takes a fresh context id, uses it for everything it does outside its own requests, and announces it on a BroadcastChannel; the creating thread learns which request created the worker from the worker_threads diagnostics channel and emits handoff(request, worker) when the announcement arrives. Nothing is added to workerData, the worker's arguments or its messages. The handoff may arrive after the worker's first events; the sensor joins them by id.
cluster workers are separate processes with their own copy of the hook; nothing to hand off.
Known gaps
- Async whole-file helpers on Node's internal bindings.
fs.readFile(callback form) and other asyncfscalls that never call the exportedfs.openand never hand the fd to JavaScript run on the thread pool: noopened. (fs.writeFile,fs.appendFile, streams,fs.promises.*and every sync call are covered.) - Worker handoff on Node 18: Node 18 publishes no
worker_threadschannel; workers created there are not linked. - Pool workers: a worker created at startup and fed requests with
postMessagegets one context for its whole life; per-message context is not followed (messages are never touched). - Native addons with their own I/O (
pg-native,sqlite3file I/O on the thread pool, gRPC's C core): their sockets and files are linked only when the kernel's thread rule applies. - io_uring: Node 20.3–20.10 (libuv 1.45–1.46) could use io_uring for file operations, which bypass the syscall tracepoints the sensor uses; later releases disabled it by default.
fs.promisesFileHandle prototype: the class is not exported; the binding takes it from the first handle the service opens, or opens/dev/nullonce (read-only) on the first request with a tracer attached. Until then,fs.promises.writeFile/readFilebefore any handle exists are not reported.
Safety
- Wrapped:
ChildProcess.prototype.spawn,child_process.spawnSync/execSync/execFileSync,fs.open/openSync/readFileSync/writeFileSync/appendFileSync/copyFileSync/cpSync/truncateSync,fs.promises.open,FileHandle.prototype.fd(getter),net.Socket.prototype.connect,IncomingMessage.prototype.emit,ServerResponse.prototype.emit(and on Node < 16.17,http(s).Server.prototype.emit). Subscribed:http.server.request.start,http.server.response.finish,worker_threads. - Every wrapper calls the original with the same
this, arguments andnew.target, returns its result and lets its exceptions through unchanged; it keeps the function'sname,lengthand own properties (e.g.util.promisify.customonexec). Two narrow, documented substitutions:fs.open's callback is replaced by one that reports the fd and then calls the original with the samethisand arguments;fs.promises.openreturns an equivalent promise (same value or rejection, a constant few microtask ticks later) — only while a tracer is attached and a request is current. - Every call into the core is in
try/catch; internal errors are counted and the binding switches itself off for the process after 5. Nothing is ever logged unlessVBXTRACE_DEBUG=1(then to stderr). - While no tracer is attached, every wrapper does one call into the core (
enabled()) and nothing else. - Chaos test: with
VBXTRACE_CHAOS=1every event function of the core throws (and the error limit is lifted so every wrapper runs its failing path); the sample services' responses are byte-identical to the normal run and to the run without the hook. - Overhead: not measured yet; it will be measured in the performance phase.
Verified (hooks/run-samples.sh node --package n1):
| Sample | Runtimes | events | chaos | baseline |
|---|---|---|---|---|
core | Node 16.20, 18.20, 20.20, 22.23, 24.22 (Alpine, musl) | 204 events match | byte-identical | byte-identical |
core-glibc | Node 22 (Debian, glibc) | 41 events match | byte-identical | byte-identical |
The scenario: two requests on one keep-alive connection (indexes 0 and 1), execFile, execFileSync, fs.writeFile, fs.writeFileSync, createWriteStream({flags:'a'}), fs.promises.writeFile, fs.promises.open + FileHandle methods, http.get, new net.Socket().connect(), work in the request body's 'end' and a timer started there, and a Worker that writes a file. Unit tests (hooks/node/n1/test/unit.js: wrapper transparency, exceptions, VBXTRACE=0, the error limit, chaos, flag mapping, events, --import — skipped below Node 20.6) pass on Node 16, 18, 20, 22 and 24.
Turning it off
Set VBXTRACE=0 in the service's environment and restart it, or revert the change that added the hook (remove the mount and the --require together); vulnboxd's rollback covers a hook that breaks a service.
Troubleshooting
- Are the probes there?
readelf -n /usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.solists them;sudo bpftrace -l 'usdt:/usr/local/lib/vulnboxd/trace/libvbxtrace-x86_64.so:*'too. - Is the hook loaded?
grep vbxtrace /proc/<pid>/mapsinside the container shows the core mapped into the node process. From inside the service:globalThis[Symbol.for('vbxtrace.node.v1')]?.status()→{ loaded: true, core: 'napi', off: false, … }. - Which links did a flow get? The hub shows each link's method; this package's are
hook:N1. Cannot find module '/opt/vbxtrace/node/register.js'at startup: theNODE_OPTIONSline is there but the mount is not (or points elsewhere). Add the mount or remove the line.- Nothing recorded for a request: is the server
node:http/https? (Raw TCP needs N3, http2 N2.) Is the work done in a worker created before the request (pool)? SetVBXTRACE_DEBUG=1to see internal errors and whether the binding switched itself off.