cloudist
v2.4.5
Published
Simple WAF Web App Firewall, middleware for Node.js — blocks SQLi, XSS, path traversal, command injection & bots, with a waf-style browser check, framework-agnostic
Downloads
17
Maintainers
Readme
Cloudist
Simple WAF (Web Application Firewall) middleware for Node.js. It blocks common attacks such as SQL injection, XSS, path traversal, and command injection, and adds a Cloudflare-style browser check for suspicious traffic. Zero dependencies, framework-agnostic.
Features
- Blocks SQL injection, XSS, path traversal, and command injection payloads
- Detects known bad bot user-agents and basic HTTP script libraries
- Behavior scoring based on request headers, to catch bots that fake their user-agent
- Rate limiting per IP
- Connection flood and slowloris protection
- Automatic "under attack" detection when traffic spikes
- Optional automatic IP banning after repeated violations
- Browser challenge page (interstitial) or an unobtrusive checkbox widget
- Works with Express, Connect, or plain
http.createServer
Installation
npm install cloudistQuick start
With Express:
const express = require('express');
const cloudist = require('cloudist');
const app = express();
app.use(cloudist({
secret: 'change-this-to-a-random-string',
mode: 'block'
}));
app.get('/', (req, res) => {
res.send('Hello world');
});
app.listen(3000);With plain http:
const http = require('http');
const cloudist = require('cloudist');
const waf = cloudist({ secret: 'change-this-to-a-random-string' });
const server = http.createServer((req, res) => {
waf(req, res, () => {
res.end('Hello world');
});
});
server.listen(3000);Always set your own secret. The default value is only a placeholder and is not safe to use in production, since it is used to sign the verification token.
How it works
Every request goes through these checks, in order:
- Whitelist check — whitelisted IPs skip everything else.
- Ban check — if the IP was auto-banned, it gets rejected immediately.
- Connection flood check — too many simultaneous connections from one IP.
- Rate limit check — too many requests in the configured time window.
- Threat detection — the URL and request body are scanned for SQLi, XSS, path traversal, and command injection patterns.
- Behavior scoring — headers are analyzed to see how closely the request resembles a real browser.
- Bot/challenge handling — depending on
modeand the behavior score, suspicious requests see a captcha page, a challenge page, a checkbox widget, or (if nothing is suspicious) are passed through to your app.
Modes
Cloudist supports three modes, set with the mode option:
block(default) — malicious requests are blocked with a 403 page. Requests that only look bot-like (not malicious) get a short browser challenge page.challenge— every request must pass the browser challenge page first, then gets a cookie so it isn't challenged again for a while. The page waits out the delay and confirms with the server (with a signed, time-locked ticket) before the verified cookie is issued — a scraper reading the page source can't skip the wait.checkbox— your page loads normally. A small checkbox widget is injected into the bottom of the HTML response. Clicking it shows a full-page overlay with a spinner, then a randomly generated code the visitor must type manually (pasting is blocked). The code is verified against a signed server-side proof, so it can't be bypassed by calling the verify endpoint directly. Actual attacks (SQLi, XSS, etc.) are still blocked in this mode regardless of verification status.
Captcha fallback
Independent of mode, any request whose behavior score drops below captchaThreshold (a much lower bar than behaviorThreshold) is treated as an obvious bot and gets a distorted-text image captcha instead of the lighter challenge/checkbox flow — waiting out a delay or clicking a checkbox doesn't help a script that scores this low.
The image is an inline SVG (no external image requests, nothing to self-host): each character gets independent rotation, vertical jitter, and size variance, plus background noise lines and dots, so it can't be lifted with a fixed-position crop. The answer is verified the same way as the checkbox code — a signed, time-limited, IP-bound proof embedded in the page, checked server-side with no session storage needed.
cloudist({
captchaThreshold: 20, // behavior score below this triggers the captcha page instead of challenge/checkbox
captchaVerifyPath: '/__cl-captcha-verify', // internal endpoint used by the captcha page
captchaLength: 6 // number of characters in the captcha image
})Set captchaThreshold to 0 to disable the captcha fallback entirely (only challenge/checkbox/block will be used).
Configuration options
cloudist({
secret: 'change-this-to-a-random-string', // used to sign challenge tokens
mode: 'block', // 'block' | 'challenge' | 'checkbox'
siteName: 'My Site', // shown on the challenge/block page
theme: 'mono', // 'mono' | 'color'
cookieName: 'cl_verify',
cookieMaxAgeMs: 30 * 60 * 1000, // how long a verified visitor stays verified
challengeDelaySeconds: 4,
challengeVerifyPath: '/__cl-challenge-verify', // internal endpoint used by the challenge page
checkboxVerifyPath: '/__cl-verify', // internal endpoint used by the checkbox widget
checkboxSpinnerSeconds: 4, // how long the spinner overlay shows before the code appears
checkboxCodeLength: 6, // length of the manual-entry verification code
whitelist: [], // array of IPs that skip all checks
rateLimit: {
windowMs: 60000,
max: 100
},
maxConcurrentConnections: 50, // per IP
underAttack: {
windowMs: 10000,
requestThreshold: 200,
cooldownMs: 60000
},
behaviorThreshold: 50, // score below this is treated as bot-like
fingerprintCookieName: 'cl_fp',
requireFingerprint: false, // reject requests with a suspicious client fingerprint
captchaThreshold: 20, // behavior score below this triggers a captcha page (0 disables it)
captchaVerifyPath: '/__cl-captcha-verify',
captchaLength: 6,
autoban: {
threshold: 5, // violations before a ban
windowMs: 10 * 60 * 1000,
banDurationMs: 60 * 60 * 1000,
filePath: null, // optional path to persist bans to disk
onBan: (ip, reason) => {}
},
onBlock: ({ ip, reason, req }) => {
// called whenever a request gets blocked, for logging etc.
}
})All options are optional except secret, which you should always set explicitly in production.
Rate limiting
Pass a rateLimit object to enable it:
cloudist({
rateLimit: { windowMs: 60000, max: 100 } // max 100 requests per IP per minute
});Automatic IP banning
Disabled by default. Enable it with the autoban option:
cloudist({
autoban: {
threshold: 3, // ban after 3 violations
windowMs: 10 * 60 * 1000, // violations are counted within this window
banDurationMs: 60 * 60 * 1000, // ban lasts 1 hour
onBan: (ip, reason) => {
console.log(`Banned ${ip} for ${reason}`);
}
}
});You can inspect or manage bans manually through the middleware's blacklist property:
const waf = cloudist({ autoban: { threshold: 3 } });
waf.blacklist.isBanned('1.2.3.4');
waf.blacklist.unban('1.2.3.4');Whitelisting IPs
cloudist({
whitelist: ['127.0.0.1', '10.0.0.5']
});Whitelisted IPs skip every check, including threat detection.
Attaching to a raw server (connection flood and slowloris protection)
Connection-level protections (flood detection, slowloris) need access to the underlying server, not just each request. Attach them like this:
const http = require('http');
const cloudist = require('cloudist');
const waf = cloudist({ secret: 'change-this-to-a-random-string' });
const server = http.createServer((req, res) => {
waf(req, res, () => res.end('ok'));
});
waf.attachToServer(server);
server.listen(3000);Checking attack status programmatically
const waf = cloudist({ /* ... */ });
if (waf.isUnderAttack()) {
console.log('Traffic spike detected');
}Running the examples
The examples/ folder has a small Express demo for the checkbox mode:
npm install express
node examples/checkbox-server.jsThen open http://localhost:3000. See examples/README.md for details.
Running tests
npm testLicense
MIT
