Skip to content

Commit 1992b02

Browse files
committed
Instrument fs/promises readFile
1 parent b644372 commit 1992b02

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

library/agent/hooks/instrumentation/builtinShim.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export function generateBuildinShim(
2323
(m) => !methods.some((method) => method.name === m.name)
2424
);
2525

26+
// Todos: Copy over properties of patched methods?
2627
return `
2728
const orig = process.getBuiltinModule(${JSON.stringify(builtinName)});
2829
const { __instrumentInspectArgs } = require('@aikidosec/firewall/instrument/internals');

library/agent/hooks/instrumentation/injectedFunctions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getInstance } from "../../AgentSingleton";
2-
import { getPackageCallbacks } from "./instructions";
2+
import { getBuiltinCallbacks, getPackageCallbacks } from "./instructions";
33

44
export function __instrumentInspectArgs(
55
id: string,
@@ -12,7 +12,13 @@ export function __instrumentInspectArgs(
1212
}
1313

1414
if (isBuiltin) {
15-
// Todo
15+
const callbacks = getBuiltinCallbacks(id);
16+
17+
if (typeof callbacks.inspectArgs === "function") {
18+
// Todo support subject?
19+
callbacks.inspectArgs(args, agent, undefined);
20+
}
21+
1622
return;
1723
}
1824

library/agent/hooks/instrumentation/instructions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,9 @@ export function getPackageCallbacks(
138138
): IntereptorFunctionsObj {
139139
return packageFileCallbacks.get(identifier) || {};
140140
}
141+
142+
export function getBuiltinCallbacks(
143+
identifier: string
144+
): IntereptorFunctionsObj {
145+
return builtinCallbacks.get(identifier) || {};
146+
}

library/sinks/FileSystem.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,19 @@ export class FileSystem implements Wrapper {
153153
this.wrapPromises(exports.promises, pkgInfo);
154154
});
155155

156-
hooks
157-
.addBuiltinModule("fs/promises")
158-
.onRequire((exports, pkgInfo) => this.wrapPromises(exports, pkgInfo));
156+
const fsPromises = hooks.addBuiltinModule("fs/promises");
157+
fsPromises.onRequire((exports, pkgInfo) =>
158+
this.wrapPromises(exports, pkgInfo)
159+
);
160+
fsPromises.setInstrumentationInstruction({
161+
functions: [
162+
{
163+
name: "readFile",
164+
inspectArgs: (args) => {
165+
console.log("readFile", args);
166+
},
167+
},
168+
],
169+
});
159170
}
160171
}

0 commit comments

Comments
 (0)