它的功能:清理来自 POST 请求正文 ( req.body)、 GET 请求查询 ( req.query) 和 URL 参数 ( req.params) 的用户输入。
它能防范什么:跨站脚本/XSS攻击。
使用方法:
npm install xss-clean
constapp=require('express')();constxssClean=require('xss-clean');// Protect against XSS attacks, should come before any routesapp.use(xssClean());app.listen(1337);
constrestify=require('restify')constxss=require('xss-clean')constapp=restify.createServer()app.use(restify.bodyParser())// make sure this comes before any routesapp.use(xss())app.listen(8080)
// ...varhpp=require('hpp');// ...app.use(bodyParser.urlencoded());// Make sure the body is parsed beforehand.app.use(hpp());// <- THIS IS THE NEW LINE// Add your own middlewares afterwards, e.g.:app.get('/search',
其作用:用于限制 IP 地址对 API 端点的重复请求。例如,可以限制用于发送密码重置邮件的端点的请求频率,因为重复请求可能会产生额外费用。
它可以防御:暴力破解、拒绝服务 (DoS) 攻击和分布式拒绝服务 (DDoS) 攻击。
使用方法:
npm install express-rate-limit
constapp=require('express')();constrateLimit=require('express-rate-limit');// Restrict all routes to only 100 requests per IP address every 1o minutesconstlimiter=rateLimit({windowMs:10*60*1000,// 10 minutesmax:100// 100 requests per IP});app.use(limiter);app.listen(1337);
import{rateLimit}from'express-rate-limit'constlimiter=rateLimit({windowMs: 15*60*1000,// 15 minuteslimit: 100,// Limit each IP to 100 requests per `window` (here, per 15 minutes).standardHeaders: 'draft-7',// draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` headerlegacyHeaders: false,// Disable the `X-RateLimit-*` headers.// store: ... , // Redis, Memcached, etc. See below.})// Apply the rate limiting middleware to all requests.app.use(limiter)