UNPKG

@aikidosec/firewall

Version:

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

49 lines (48 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Express = void 0; const http_1 = require("http"); const wrapRequestHandler_1 = require("./express/wrapRequestHandler"); const wrapExport_1 = require("../agent/hooks/wrapExport"); class Express { // Wrap all the functions passed to app.METHOD(...) // Examples: // app.METHOD(path, handler) // app.METHOD(path, middleware, handler) // app.METHOD(path, middleware, middleware, ..., handler) // Wrap all the functions passed to app.use(...) // app.use(middleware) // app.use(middleware, middleware, ...) // app.use(path, middleware) // app.use(path, middleware, middleware, ...) wrapArgs(args) { return args.map((arg) => { // Ignore non-function arguments if (typeof arg !== "function") { return arg; } // Ignore error handlers if (arg.length > 3) { return arg; } return (0, wrapRequestHandler_1.wrapRequestHandler)(arg); }); } wrap(hooks) { const expressMethodNames = http_1.METHODS.map((method) => method.toLowerCase()); hooks .addPackage("express") .withVersion("^4.0.0 || ^5.0.0") .onRequire((exports, pkgInfo) => { for (const method of expressMethodNames) { (0, wrapExport_1.wrapExport)(exports.Route.prototype, method, pkgInfo, { modifyArgs: (args) => this.wrapArgs(args), }); } (0, wrapExport_1.wrapExport)(exports.application, "use", pkgInfo, { modifyArgs: (args) => this.wrapArgs(args), }); }); } } exports.Express = Express;