@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.
31 lines (30 loc) • 1.25 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapRequestBodyParsing = wrapRequestBodyParsing;
const Context_1 = require("../../agent/Context");
const wrap_1 = require("../../helpers/wrap");
// Wrap the request body parsing functions to update the context with the parsed body, if any of the functions are called.
function wrapRequestBodyParsing(req) {
req.parseBody = wrapBodyParsingFunction(req.parseBody);
req.json = wrapBodyParsingFunction(req.json);
req.text = wrapBodyParsingFunction(req.text);
}
function wrapBodyParsingFunction(func) {
if ((0, wrap_1.isWrapped)(func)) {
return func;
}
return (0, wrap_1.createWrappedFunction)(func, function parse(parser) {
return async function wrap() {
// @ts-expect-error No type for arguments
// eslint-disable-next-line prefer-rest-params
const returnValue = await parser.apply(this, arguments);
if (returnValue) {
const context = (0, Context_1.getContext)();
if (context) {
(0, Context_1.updateContext)(context, "body", returnValue);
}
}
return returnValue;
};
});
}
;