Skip to content

Commit 234fed2

Browse files
committed
Instrument Mysql
Support FunctionAssignment with sub properties
1 parent 1a7d769 commit 234fed2

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

instrumentation-wasm/src/js_transformer/helpers/get_name_str_for_member_expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use oxc_allocator::Allocator;
1+
use oxc_allocator::{Allocator, CloneIn};
22
use oxc_ast::ast::{Expression, MemberExpression};
33

44
pub fn get_name_str_for_member_expr<'a>(
@@ -26,6 +26,10 @@ pub fn get_name_str_for_member_expr<'a>(
2626

2727
let obj_name_str = match member_expr.object() {
2828
Expression::Identifier(identifier_ref) => identifier_ref.name.as_str(),
29+
Expression::StaticMemberExpression(static_member_expr) => get_name_str_for_member_expr(
30+
allocator,
31+
&MemberExpression::StaticMemberExpression(static_member_expr.clone_in(allocator)),
32+
)?,
2933
_ => {
3034
// Unsupported AST type
3135
return None;

library/agent/hooks/instrumentation/codeTransformation.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,3 +1253,50 @@ t.test("Modify function declaration (CJS)", async (t) => {
12531253
};`
12541254
);
12551255
});
1256+
1257+
t.test(
1258+
"add inspectArgs to multi level function assignment expression (CJS)",
1259+
async (t) => {
1260+
const result = transformCode(
1261+
"express",
1262+
"1.0.0",
1263+
"application.js",
1264+
`
1265+
const app = require("example");
1266+
app.prototype.use = function (fn) {
1267+
console.log("test");
1268+
};
1269+
`,
1270+
"commonjs",
1271+
{
1272+
path: "application.js",
1273+
versionRange: "^1.0.0",
1274+
functions: [
1275+
{
1276+
nodeType: "FunctionAssignment",
1277+
name: "app.prototype.use",
1278+
identifier:
1279+
"express.application.js.app.prototype.use.MethodDefinition.v1.0.0",
1280+
inspectArgs: true,
1281+
modifyArgs: false,
1282+
modifyReturnValue: false,
1283+
modifyArgumentsObject: false,
1284+
},
1285+
],
1286+
}
1287+
);
1288+
1289+
t.same(
1290+
compareCodeStrings(
1291+
result,
1292+
`const { __instrumentInspectArgs } = require("@aikidosec/firewall/instrument/internals");
1293+
const app = require("example");
1294+
app.prototype.use = function (fn) {
1295+
__instrumentInspectArgs("express.application.js.app.prototype.use.MethodDefinition.v1.0.0", arguments, "1.0.0", this);
1296+
console.log("test");
1297+
};`
1298+
),
1299+
true
1300+
);
1301+
}
1302+
);

library/sinks/MySQL.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ export class MySQL implements Wrapper {
5757
kind: "sql_op",
5858
inspectArgs: (args) => this.inspectQuery(args),
5959
});
60+
})
61+
.addFileInstrumentation({
62+
path: "lib/Connection.js",
63+
functions: [
64+
{
65+
name: "Connection.prototype.query",
66+
nodeType: "FunctionAssignment",
67+
operationKind: "sql_op",
68+
inspectArgs: (args) => this.inspectQuery(args),
69+
},
70+
],
6071
});
6172
}
6273
}

0 commit comments

Comments
 (0)