@aikidosec/firewall
Version:
Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks
29 lines (28 loc) • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addFastifyHook = addFastifyHook;
const shouldBlockRequest_1 = require("./shouldBlockRequest");
const escapeHTML_1 = require("../helpers/escapeHTML");
/**
* Calling this function will setup rate limiting and user blocking for the provided Fastify app by adding a onRequest hook.
* Attacks will still be blocked by Zen if you do not call this function.
* Execute this function as early as possible in your Fastify app, but after the hook that sets the user.
*/
function addFastifyHook(app) {
app.addHook("onRequest", (request, reply, done) => {
const result = (0, shouldBlockRequest_1.shouldBlockRequest)();
if (result.block) {
if (result.type === "ratelimited") {
let message = "You are rate limited by Zen.";
if (result.trigger === "ip" && result.ip) {
message += ` (Your IP: ${(0, escapeHTML_1.escapeHTML)(result.ip)})`;
}
return reply.status(429).send(message);
}
if (result.type === "blocked") {
return reply.status(403).send("You are blocked by Aikido firewall.");
}
}
done();
});
}