Skip to content

Commit b644372

Browse files
committed
Add inspectArgs for Hono
1 parent 84f7a99 commit b644372

File tree

7 files changed

+65
-16
lines changed

7 files changed

+65
-16
lines changed

instrumentation-wasm/src/js_transformer/instructions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
44
#[serde(rename_all = "camelCase")]
55
pub struct FileInstructions {
66
pub path: String,
7+
pub version_range: String,
78
pub functions: Vec<FunctionInstructions>,
89
}
910

@@ -12,6 +13,8 @@ pub struct FileInstructions {
1213
pub struct FunctionInstructions {
1314
pub node_type: String,
1415
pub name: String,
16+
pub identifier: String,
1517
pub inspect_args: bool,
18+
pub modify_args: bool,
1619
pub modify_return_value: bool,
1720
}

instrumentation-wasm/src/js_transformer/transformer.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ pub fn transform_code_str(
5252
let t = &mut Transformer {
5353
allocator: &allocator,
5454
file_instructions: &file_instructions,
55-
current_function_identifier: None,
56-
modify_return_value: false,
55+
//current_function_identifier: None,
56+
//modify_return_value: false,
5757
module_name,
58-
sub_function_counter: 0,
58+
//sub_function_counter: 0,
5959
};
6060

6161
traverse_mut(t, &allocator, program, symbols, scopes);
@@ -90,9 +90,9 @@ pub fn transform_code_str(
9090
struct Transformer<'a> {
9191
allocator: &'a Allocator,
9292
file_instructions: &'a FileInstructions,
93-
current_function_identifier: Option<String>, // Only set if we want to instrument the current function
94-
sub_function_counter: i32, // Counter to keep track of how many sub functions we are in
95-
modify_return_value: bool,
93+
//current_function_identifier: Option<String>, // Only set if we want to instrument the current function
94+
//sub_function_counter: i32, // Counter to keep track of how many sub functions we are in
95+
//modify_return_value: bool,
9696
module_name: &'a str,
9797
}
9898

@@ -117,23 +117,23 @@ impl<'a> Traverse<'a> for Transformer<'a> {
117117
.find(|f| f.node_type == "MethodDefinition" && f.name == method_name);
118118

119119
if found_instruction.is_none() {
120-
self.current_function_identifier = None;
121-
self.modify_return_value = false;
120+
//self.current_function_identifier = None;
121+
//self.modify_return_value = false;
122122
return;
123123
}
124124

125125
let instruction = found_instruction.unwrap();
126126

127-
let function_identifier = format!("{}.{}", self.module_name, method_name);
127+
/*let function_identifier = format!("{}.{}", self.module_name, method_name);
128128
self.current_function_identifier = Some(function_identifier.clone());
129-
self.modify_return_value = instruction.modify_return_value;
129+
self.modify_return_value = instruction.modify_return_value;*/
130130

131131
let body = node.value.body.as_mut().unwrap();
132132

133133
if instruction.inspect_args {
134134
let source_text: &'a str = self.allocator.alloc_str(&format!(
135135
"__instrumentInspectArgs('{}', false, arguments);",
136-
self.current_function_identifier.as_ref().unwrap()
136+
instruction.identifier
137137
));
138138

139139
body.statements.insert(
@@ -146,7 +146,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
146146
}
147147
}
148148

149-
fn exit_method_definition(
149+
/*fn exit_method_definition(
150150
&mut self,
151151
_node: &mut MethodDefinition<'a>,
152152
ctx: &mut TraverseCtx<'a>,
@@ -183,5 +183,5 @@ impl<'a> Traverse<'a> for Transformer<'a> {
183183
184184
// Todo support modifying return value
185185
//AstBuilder::new(self.allocator).return_statement(node.span, argument)
186-
}
186+
}*/
187187
}
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
import { getInstance } from "../../AgentSingleton";
2+
import { getPackageCallbacks } from "./instructions";
3+
14
export function __instrumentInspectArgs(
25
id: string,
36
isBuiltin: boolean,
47
args: unknown[]
58
): void {
6-
// Todo do something
9+
const agent = getInstance();
10+
if (!agent) {
11+
return;
12+
}
13+
14+
if (isBuiltin) {
15+
// Todo
16+
return;
17+
}
18+
19+
const cbFuncs = getPackageCallbacks(id);
20+
21+
if (typeof cbFuncs.inspectArgs === "function") {
22+
// Todo support subject?
23+
cbFuncs.inspectArgs(args, agent, undefined);
24+
}
725
}

library/agent/hooks/instrumentation/instructions.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export function setPackagesToInstrument(_packages: Package[]) {
3636
functions: file.functions.map((func) => {
3737
const identifier = `${pkg.getName()}.${file.path}.${func.name}.${versionedPackage.getRange()}`;
3838

39+
packageFileCallbacks.set(identifier, {
40+
inspectArgs: func.inspectArgs,
41+
modifyArgs: func.modifyArgs,
42+
modifyReturnValue: func.modifyReturnValue,
43+
});
44+
3945
return {
4046
nodeType: func.nodeType,
4147
name: func.name,
@@ -51,7 +57,9 @@ export function setPackagesToInstrument(_packages: Package[]) {
5157
})
5258
.flat();
5359

54-
packages.set(pkg.getName(), packageInstructions);
60+
if (packageInstructions.length !== 0) {
61+
packages.set(pkg.getName(), packageInstructions);
62+
}
5563
}
5664
}
5765

@@ -124,3 +132,9 @@ export function getPackageFileInstrumentationInstructions(
124132

125133
return instructions.find((f) => satisfiesVersion(f.versionRange, version));
126134
}
135+
136+
export function getPackageCallbacks(
137+
identifier: string
138+
): IntereptorFunctionsObj {
139+
return packageFileCallbacks.get(identifier) || {};
140+
}

library/agent/hooks/instrumentation/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export type PackageFileInstrumentationInstructionJSON = {
9494
functions: {
9595
nodeType: "MethodDefinition";
9696
name: string;
97+
identifier: string;
9798
inspectArgs: boolean;
9899
modifyArgs: boolean;
99100
modifyReturnValue: boolean;

library/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"./context": "./context/index.js",
1515
"./lambda": "./lambda/index.js",
1616
"./nopp": "./nopp/index.js",
17-
"./cloud-function": "./cloud-function/index.js"
17+
"./cloud-function": "./cloud-function/index.js",
18+
"./bundler": "./bundler/index.js"
1819
},
1920
"homepage": "https://aikido.dev/zen",
2021
"author": "Aikido Security",

library/sources/Hono.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ export class Hono implements Wrapper {
5353
});
5454

5555
return newExports;
56+
})
57+
.addFileInstrumentation({
58+
path: "lib/hono-base.js",
59+
functions: [
60+
{
61+
nodeType: "MethodDefinition",
62+
name: "addRoute",
63+
inspectArgs: (args) => {
64+
console.log(args);
65+
},
66+
},
67+
],
5668
});
5769
}
5870
}

0 commit comments

Comments
 (0)