@aikidosec/firewall
Version:
Zen by Aikido is an embedded Application Firewall that autonomously protects Node.js apps against common and critical attacks, provides rate limiting, detects malicious traffic (including bots), and more.
131 lines (130 loc) • 4.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Fastify = void 0;
const isPlainObject_1 = require("../helpers/isPlainObject");
const wrapHandler_1 = require("./fastify/wrapHandler");
const wrapNewInstance_1 = require("../agent/hooks/wrapNewInstance");
const wrapExport_1 = require("../agent/hooks/wrapExport");
class Fastify {
wrapRequestArgs(args) {
return args.map((arg) => {
if (typeof arg === "function") {
return (0, wrapHandler_1.wrapHandler)(arg);
}
return arg;
});
}
wrapAddHookArgs(args) {
if (args.length < 2 || typeof args[0] !== "string") {
return args;
}
const hooksToWrap = [
"onRequest",
"preParsing",
"preValidation",
"preHandler",
"preSerialization",
"onError",
"onSend",
"onResponse",
"onTimeout",
"onRequestAbort",
];
const hookName = args[0];
if (!hooksToWrap.includes(hookName)) {
return args;
}
return args.map((arg) => {
if (typeof arg === "function") {
return (0, wrapHandler_1.wrapHandler)(arg);
}
return arg;
});
}
wrapRouteMethod(args) {
if (args.length < 1) {
return args;
}
const options = args[0];
if (!(0, isPlainObject_1.isPlainObject)(options)) {
return args;
}
for (const [key, value] of Object.entries(options)) {
if (typeof value === "function") {
options[key] = (0, wrapHandler_1.wrapHandler)(value);
}
}
return args;
}
/**
* Wrap a new route method that was added by using `addHttpMethod`
*/
wrapNewRouteMethod(args, appInstance, pkgInfo) {
if (!args.length ||
typeof args[0] !== "string" ||
!appInstance ||
typeof appInstance !== "object") {
return appInstance;
}
const method = args[0].toLowerCase();
if (typeof appInstance[method] !== "function") {
return appInstance;
}
// Wrap the new route method
(0, wrapExport_1.wrapExport)(appInstance, method, pkgInfo, {
kind: undefined,
modifyArgs: this.wrapRequestArgs,
});
return appInstance;
}
wrapFastifyInstance(instance, pkgInfo) {
const requestFunctions = [
"get",
"head",
"post",
"put",
"delete",
"options",
"patch",
"all",
];
for (const func of requestFunctions) {
// Check if the function exists - new functions in Fastify 5
if (typeof instance[func] === "function") {
(0, wrapExport_1.wrapExport)(instance, func, pkgInfo, {
kind: undefined,
modifyArgs: this.wrapRequestArgs,
});
}
}
(0, wrapExport_1.wrapExport)(instance, "route", pkgInfo, {
kind: undefined,
modifyArgs: this.wrapRouteMethod,
});
(0, wrapExport_1.wrapExport)(instance, "addHook", pkgInfo, {
kind: undefined,
modifyArgs: this.wrapAddHookArgs,
});
// Added in Fastify 5
if (typeof instance.addHttpMethod === "function") {
(0, wrapExport_1.wrapExport)(instance, "addHttpMethod", pkgInfo, {
kind: undefined,
modifyReturnValue: (args, returnValue) => this.wrapNewRouteMethod(args, returnValue, pkgInfo),
});
}
}
wrap(hooks) {
hooks
.addPackage("fastify")
.withVersion("^4.0.0 || ^5.0.0")
.onRequire((exports, pkgInfo) => {
// Wrap export with the name "fastify"
(0, wrapNewInstance_1.wrapNewInstance)(exports, "fastify", pkgInfo, (exports) => this.wrapFastifyInstance(exports, pkgInfo));
// Wrap export with the name "default"
(0, wrapNewInstance_1.wrapNewInstance)(exports, "default", pkgInfo, (exports) => this.wrapFastifyInstance(exports, pkgInfo));
// Wrap default export
return (0, wrapNewInstance_1.wrapNewInstance)(exports, undefined, pkgInfo, (exports) => this.wrapFastifyInstance(exports, pkgInfo));
});
}
}
exports.Fastify = Fastify;
;