guardian-risk-express
v0.2.0
Published
Express middleware plugin for guardian-risk — adds request signals
Maintainers
Readme
guardian-risk-express
Requires:
guardian-risk(core)
npm install guardian-risk guardian-risk-expressExpress integration for guardian-risk. Validates client IP, reads HTTP metadata, and provides production middleware.
Signals
| Signal | Source |
|--------|--------|
| clientIp | Validated req.ip / req.ips (trust proxy aware) |
| userAgent | User-Agent header (truncated) |
| requestMethod | req.method |
| requestPath | req.path |
Production usage
import express from 'express';
import { Guardian } from 'guardian-risk';
import { expressPlugin, guardianMiddleware } from 'guardian-risk-express';
import { redisPlugin } from 'guardian-risk-redis';
const app = express();
app.set('trust proxy', 1); // required behind load balancers
const template = new Guardian()
.use(expressPlugin({ trustProxy: true }))
.use(redisPlugin({ url: process.env.REDIS_URL }))
.rule({ name: 'HighRate', when: (s) => (s.requestsPerMinute ?? 0) > 120, score: 40 });
app.get('/health', (_req, res) => res.json({ ok: true }));
app.use(
guardianMiddleware(template, {
blockAboveScore: 70,
onAnalyzeError: 'block', // fail closed when hooks fail
exposeBlockDetails: false, // never leak reasons to clients
}),
);Security notes
- Set
app.set('trust proxy', N)when behind a reverse proxy — otherwiseclientIpreflects the proxy, not the user. clientIpis validated — malformedX-Forwarded-Forvalues are rejected.- Use
onAnalyzeError: 'block'in production whenblockAboveScoreis set. - Use
template.fork()orguardianMiddleware— never share oneGuardianacross requests. - Headers and user agents are spoofable — treat as hints, not proof.
API
expressPlugin(options)— registersbeforeAnalyzehookfromRequest(req, guardian, options)— manual per-request signal injectionguardianMiddleware(template, options)— Express middleware with optional blockinganalyzeRequest(template, req, options)— programmatic analyze helper
See SECURITY.md and MIGRATION.md.
