@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.
110 lines (109 loc) • 3.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.__instrumentInspectArgs = __instrumentInspectArgs;
exports.__instrumentModifyArgs = __instrumentModifyArgs;
exports.__instrumentModifyReturnValue = __instrumentModifyReturnValue;
exports.__instrumentAccessLocalVariables = __instrumentAccessLocalVariables;
const AgentSingleton_1 = require("../../AgentSingleton");
const Context_1 = require("../../Context");
const wrapExport_1 = require("../wrapExport");
const instructions_1 = require("./instructions");
function __instrumentInspectArgs(id, args, pkgVersion, subject // "This" of the method being called
) {
const agent = (0, AgentSingleton_1.getInstance)();
if (!agent) {
return;
}
const context = (0, Context_1.getContext)();
const cbInfo = (0, instructions_1.getFunctionCallbackInfo)(id);
if (!cbInfo) {
return;
}
if (typeof cbInfo.funcs.inspectArgs === "function") {
wrapExport_1.inspectArgs.call(subject, Array.from(args), cbInfo.funcs.inspectArgs, context, agent, {
name: cbInfo.pkgName,
version: pkgVersion,
type: "external",
}, cbInfo.methodName, cbInfo.operationKind);
}
}
function __instrumentModifyArgs(id, args, subject // "This" of the method being called
) {
var _a;
if (!Array.isArray(args)) {
return [];
}
const agent = (0, AgentSingleton_1.getInstance)();
if (!agent) {
return args;
}
const cbInfo = (0, instructions_1.getFunctionCallbackInfo)(id);
if (!cbInfo ||
!cbInfo.funcs ||
typeof cbInfo.funcs.modifyArgs !== "function") {
return args;
}
try {
const newArgs = cbInfo.funcs.modifyArgs(args, agent, subject);
// Only return the new arguments if they are an array
if (Array.isArray(newArgs)) {
if (!cbInfo.funcs.bindContext) {
return newArgs;
}
// Ensure that all functions in the new arguments are bound to the current execution context (only if bindContext is true)
return newArgs.map((arg) => {
if (typeof arg === "function") {
return (0, Context_1.bindContext)(arg);
}
return arg;
});
}
}
catch (error) {
if (error instanceof Error) {
(_a = (0, AgentSingleton_1.getInstance)()) === null || _a === void 0 ? void 0 : _a.onFailedToWrapMethod(cbInfo.pkgName, cbInfo.methodName, error);
}
}
return args;
}
function __instrumentModifyReturnValue(id, args, returnValue, subject // "This" of the method being called
) {
var _a;
const agent = (0, AgentSingleton_1.getInstance)();
if (!agent) {
return args;
}
const cbInfo = (0, instructions_1.getFunctionCallbackInfo)(id);
if (!cbInfo ||
!cbInfo.funcs ||
typeof cbInfo.funcs.modifyReturnValue !== "function") {
return returnValue;
}
try {
return cbInfo.funcs.modifyReturnValue(Array.from(args), returnValue, agent, subject);
}
catch (error) {
if (error instanceof Error) {
(_a = (0, AgentSingleton_1.getInstance)()) === null || _a === void 0 ? void 0 : _a.onFailedToWrapMethod(cbInfo.pkgName, cbInfo.methodName, error);
}
}
return returnValue;
}
function __instrumentAccessLocalVariables(id, vars) {
var _a;
const cbInfo = (0, instructions_1.getFileCallbackInfo)(id);
if (!cbInfo || typeof cbInfo.localVariableAccessCb !== "function") {
return;
}
try {
cbInfo.localVariableAccessCb(vars, {
name: cbInfo.pkgName,
type: "external",
});
}
catch (error) {
if (error instanceof Error) {
(_a = (0, AgentSingleton_1.getInstance)()) === null || _a === void 0 ? void 0 : _a.onFailedToWrapModule(cbInfo.pkgName, error);
}
}
}