Express, Fastify and NestJS
All three use the same helper,
node/bower-events.mjs, which has no
dependencies and works on Node.js 20 and later. Read the
shared guidance first.
import { BowerClient, buildEvent, clientIp } from './bower-events.mjs';
const bower = new BowerClient(); // BOWER_COLLECTOR_URL, BOWER_INGEST_TOKEN
const trusted = (process.env.TRUSTED_PROXIES ?? '').split(',').filter(Boolean);
BowerClient.send() never throws. It returns the HTTP status, or null if the collector is
unreachable.
Express
express-app.mjs, Express 5:
- The first middleware sets
req.id(fromtraceparentor a new UUID) andreq.clientIp, and returnsx-request-id. It runs beforeexpress.json(), so a body parse error still has context. - Leave
app.set('trust proxy')off:clientIp()appliesTRUSTED_PROXIES. security(req, spec)adds the route template (req.route.path), user agent and request fields, and sends without awaiting.- The final error handler turns body parse errors into
input_validation_failureand everything else intoapplication_error, and returns a generic message (ASVS 5.0 V16.5.1).
Fastify
fastify-app.mjs, Fastify 5:
genReqIdreuses thetraceparenttrace ID; anonRequesthook setsreq.clientIp.- Keep
trustProxyfalse. req.routeOptions.urlis the route template.- Fastify's built-in Pino logger removes
authorizationandcookieheaders. setErrorHandleremits validation and unhandled-error events.
NestJS
nestjs/bower.module.ts
provides BowerModule:
BowerSecurity.emit(req, spec)is injectable. Call it from yourAuthServicewhere the credential check happens.- A global exception filter records
ForbiddenExceptionfrom guards asauthorisation_failure,BadRequestException(includingValidationPipe) asinput_validation_failure, and anything that isn't anHttpExceptionasapplication_error. - Request context is computed on first use, because Nest's body parser runs before module middleware.
nestjs/main.ts is a runnable app;
nestjs/check.sh builds it in a temporary copy.
Pino
logger.mjs uses ISO UTC timestamps and
removes authorization, cookie, set-cookie, passwords, tokens and bodies. Pino writes
JSON, which escapes CR and LF.
What was run
All three apps were run against a local collector with the ASVS pack. Each produced the four
expected events (failure, success, authorisation failure, validation failure), all accepted.
A spoofed X-Forwarded-For was ignored, and no password or CRLF reached the redacted output.
The helper matches the cross-language contract.