@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.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BetterSQLite3 = void 0;
const Context_1 = require("../agent/Context");
const wrapExport_1 = require("../agent/hooks/wrapExport");
const checkContextForPathTraversal_1 = require("../vulnerabilities/path-traversal/checkContextForPathTraversal");
const checkContextForSqlInjection_1 = require("../vulnerabilities/sql-injection/checkContextForSqlInjection");
const SQLDialectSQLite_1 = require("../vulnerabilities/sql-injection/dialects/SQLDialectSQLite");
class BetterSQLite3 {
constructor() {
this.dialect = new SQLDialectSQLite_1.SQLDialectSQLite();
}
inspectQuery(operation, args) {
const context = (0, Context_1.getContext)();
if (!context) {
return undefined;
}
if (args.length > 0) {
if (typeof args[0] === "string" && args[0].length > 0) {
const sql = args[0];
return (0, checkContextForSqlInjection_1.checkContextForSqlInjection)({
operation: operation,
sql: sql,
context: context,
dialect: this.dialect,
});
}
}
return undefined;
}
/**
* Inspect path of sqlite3.backup for path traversal
*/
inspectPath(operation, args) {
const context = (0, Context_1.getContext)();
if (!context) {
return undefined;
}
if (args.length === 0 || typeof args[0] !== "string") {
return undefined;
}
const filename = args[0];
const result = (0, checkContextForPathTraversal_1.checkContextForPathTraversal)({
filename: filename,
operation: operation,
context: context,
checkPathStart: true,
});
if (result) {
return result;
}
return undefined;
}
wrap(hooks) {
const sqlFunctions = ["prepare", "exec", "pragma"];
const fsPathFunctions = ["backup", "loadExtension"];
hooks
.addPackage("better-sqlite3")
.withVersion("^11.0.0 || ^10.0.0 || ^9.0.0 || ^8.0.0")
.onRequire((exports, pkgInfo) => {
for (const func of sqlFunctions) {
(0, wrapExport_1.wrapExport)(exports.prototype, func, pkgInfo, {
kind: "sql_op",
inspectArgs: (args) => {
return this.inspectQuery(`better-sqlite3.${func}`, args);
},
});
}
for (const func of fsPathFunctions) {
(0, wrapExport_1.wrapExport)(exports.prototype, func, pkgInfo, {
kind: "fs_op",
inspectArgs: (args) => {
return this.inspectPath(`better-sqlite3.${func}`, args);
},
});
}
});
}
}
exports.BetterSQLite3 = BetterSQLite3;