At the very beginning of your app.js file, add the following line:
require("@aikidosec/firewall"); // <-- Include this before any other code or imports
const Hapi = require("@hapi/hapi");
const server = Hapi.server({
port: 3000,
host: "localhost",
});
// ...or ESM import style:
import "@aikidosec/firewall";
// ...By default, the firewall will run in non-blocking mode. When it detects an attack, the attack will be reported to Aikido if the environment variable AIKIDO_TOKEN is set and continue executing the call.
You can enable blocking mode by setting the environment variable AIKIDO_BLOCK to true:
AIKIDO_BLOCK=true node app.jsIt's recommended to enable this on your staging environment for a considerable amount of time before enabling it on your production environment (e.g. one week).
If you want to add the rate limiting feature to your app, modify your code like this:
const Zen = require("@aikidosec/firewall");
const server = Hapi.server(...);
// Optional, if you want to use user based rate limiting or block specific users
server.ext('onRequest', function (request, h) {
// Get the user from your authentication middleware
// or wherever you store the user
Zen.setUser({
id: "123",
name: "John Doe", // Optional
});
return h.continue;
});
// Call this after auth middleware, as early as possible in the middleware stack
Zen.addHapiMiddleware(app);
server.route(...);If you need to debug the firewall, you can run your hapi app with the environment variable AIKIDO_DEBUG set to true:
AIKIDO_DEBUG=true node app.jsThis will output debug information to the console (e.g. if the agent failed to start, no token was found, unsupported packages, ...).
Zen can also protect your application against prototype pollution attacks.
Read Protect against prototype pollution to learn how to set it up.
That's it! Your app is now protected by Zen.
If you want to see a full example, check our hapi sample app.