UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

81 lines (80 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hapi = void 0; const isPlainObject_1 = require("../helpers/isPlainObject"); const wrapRequestHandler_1 = require("./hapi/wrapRequestHandler"); const wrapNewInstance_1 = require("../agent/hooks/wrapNewInstance"); const wrapExport_1 = require("../agent/hooks/wrapExport"); class Hapi { wrapRouteHandler(args) { if (args.length < 1 || (!(0, isPlainObject_1.isPlainObject)(args[0]) && !Array.isArray(args[0]))) { return args; } const routeOptions = Array.isArray(args[0]) ? args[0] : [args[0]]; for (const route of routeOptions) { if (typeof route.handler === "function") { route.handler = (0, wrapRequestHandler_1.wrapRequestHandler)(route.handler); } if ((0, isPlainObject_1.isPlainObject)(route.options) && typeof route.options.handler === "function") { route.options.handler = (0, wrapRequestHandler_1.wrapRequestHandler)(route.options.handler); } } return args; } wrapExtensionFunction(args) { return args.map((arg) => { // Ignore non-function arguments if (typeof arg !== "function") { return arg; } return (0, wrapRequestHandler_1.wrapRequestHandler)(arg); }); } wrapDecorateFunction(args) { if (args.length < 3 || args[0] !== "handler" || typeof args[2] !== "function") { return args; } const decorator = args[2]; function wrappedDecorator() { // @ts-expect-error We don't know the type of this const handler = decorator.apply(this, arguments); return (0, wrapRequestHandler_1.wrapRequestHandler)(handler); } args[2] = wrappedDecorator; return args; } wrapServer(server, pkgInfo) { (0, wrapExport_1.wrapExport)(server, "route", pkgInfo, { kind: undefined, modifyArgs: (args) => this.wrapRouteHandler(args), }); (0, wrapExport_1.wrapExport)(server, "ext", pkgInfo, { kind: undefined, modifyArgs: (args) => this.wrapExtensionFunction(args), }); (0, wrapExport_1.wrapExport)(server, "decorate", pkgInfo, { kind: undefined, modifyArgs: (args) => this.wrapDecorateFunction(args), }); } wrap(hooks) { hooks .addPackage("@hapi/hapi") .withVersion("^21.0.0") .onRequire((exports, pkgInfo) => { (0, wrapNewInstance_1.wrapNewInstance)(exports, "Server", pkgInfo, (server) => { this.wrapServer(server, pkgInfo); }); (0, wrapNewInstance_1.wrapNewInstance)(exports, "server", pkgInfo, (server) => { this.wrapServer(server, pkgInfo); }); }); } } exports.Hapi = Hapi;