Skip to content

Commit 7e38ec3

Browse files
timokoesslerhansott
andcommitted
Apply suggestions
Co-Authored-By: Hans Ott <3886384+hansott@users.noreply.github.com>
1 parent e516921 commit 7e38ec3

File tree

12 files changed

+75
-75
lines changed

12 files changed

+75
-75
lines changed

docs/esm.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ export NODE_OPTIONS='-r @aikidosec/firewall/instrument'
1818
> [!IMPORTANT]
1919
> Please also check the documentation on how to integrate Zen with your used web framework.
2020
21-
## Blocking mode
22-
23-
By default, the firewall will run in non-blocking mode. When it detects an attack, the attack will be reported to Aikido if the environment variable `AIKIDO_TOKEN` is set and continue executing the call.
24-
25-
You can enable blocking mode by setting the environment variable `AIKIDO_BLOCK` to `true`:
26-
27-
```sh
28-
AIKIDO_BLOCK=true node app.js
29-
```
30-
31-
It's recommended to enable this on your staging environment for a considerable amount of time before enabling it on your production environment (e.g. one week).
32-
3321
## Known issues
3422

3523
- The app might crash on startup if used together with some packages that use the Node.js Asynchronous Module Customization Hooks, like Sentry or OpenTelemetry, due to bugs in Node.js itself.

end2end/tests-new/nestjs-sentry.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const { test, before } = require("node:test");
55
const { equal, fail, match, doesNotMatch } = require("node:assert");
66

77
const pathToAppDir = resolve(__dirname, "../../sample-apps/nestjs-sentry");
8-
const port = "4006";
9-
const port2 = "4007";
8+
const port = "4007";
9+
const port2 = "4008";
1010

1111
before(() => {
1212
const { stderr } = spawnSync(`npm`, ["run", "build"], {
Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
export type WrapPackageInfo = {
2-
/**
3-
* Name of the package.
4-
*/
1+
export type WrapPackageInfo =
2+
| {
3+
// Name of the builtin module
4+
name: string;
5+
type: "builtin";
6+
}
7+
| {
8+
// Name of the external package
9+
name: string;
10+
// Version of the external package
11+
version: string;
12+
type: "external";
13+
// Path information for the external package
14+
path: {
15+
base: string;
16+
// Path of the imported js file relative to the module base directory
17+
relative: string;
18+
};
19+
}
20+
| {
21+
// Name of the global
22+
name: string;
23+
type: "global";
24+
};
25+
26+
export type PartialWrapPackageInfo = {
527
name: string;
6-
/**
7-
* Version of the package, only set if the module is not a builtin module.
8-
*/
9-
version?: string;
10-
/**
11-
* Type of the wrap target.
12-
*/
1328
type: "builtin" | "external" | "global";
14-
/**
15-
* Only set if the module is not a builtin module.
16-
*/
17-
path?: {
18-
base: string;
19-
/**
20-
* Path of the imported js file relative to the module base directory.
21-
*/
22-
relative: string;
23-
};
29+
version?: string;
2430
};

library/agent/hooks/instrumentation/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
ModifyArgsInterceptor,
55
ModifyReturnValueInterceptor,
66
} from "../wrapExport";
7-
import type { WrapPackageInfo } from "../WrapPackageInfo";
7+
import type { PartialWrapPackageInfo } from "../WrapPackageInfo";
88

99
type TypedArray =
1010
| Int8Array
@@ -77,7 +77,7 @@ export type LocalVariableAccessConfig = {
7777
/**
7878
* Callback function to be called with the accessed variable values.
7979
*/
80-
cb: (vars: any[], pkgInfo: WrapPackageInfo) => void;
80+
cb: (vars: any[], pkgInfo: PartialWrapPackageInfo) => void;
8181
};
8282

8383
export type FileCallbackInfoObj = {

library/agent/hooks/onInspectionInterceptorResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { OperationKind } from "../api/Event";
66
import { attackKindHumanName } from "../Attack";
77
import { getContext, updateContext } from "../Context";
88
import type { InterceptorResult } from "./InterceptorResult";
9-
import type { WrapPackageInfo } from "./WrapPackageInfo";
9+
import type { PartialWrapPackageInfo } from "./WrapPackageInfo";
1010
import { cleanError } from "../../helpers/cleanError";
1111

1212
// Used for cleaning up the stack trace
@@ -16,7 +16,7 @@ export function onInspectionInterceptorResult(
1616
context: ReturnType<typeof getContext>,
1717
agent: Agent,
1818
result: InterceptorResult,
19-
pkgInfo: WrapPackageInfo,
19+
pkgInfo: PartialWrapPackageInfo,
2020
start: number,
2121
operation: string,
2222
kind: OperationKind | undefined

library/agent/hooks/wrapExport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getInstance } from "../AgentSingleton";
44
import { OperationKind } from "../api/Event";
55
import { bindContext, getContext } from "../Context";
66
import type { InterceptorResult } from "./InterceptorResult";
7-
import type { WrapPackageInfo } from "./WrapPackageInfo";
7+
import type { PartialWrapPackageInfo } from "./WrapPackageInfo";
88
import { wrapDefaultOrNamed } from "./wrapDefaultOrNamed";
99
import { onInspectionInterceptorResult } from "./onInspectionInterceptorResult";
1010

@@ -44,7 +44,7 @@ export type InterceptorObject = {
4444
export function wrapExport(
4545
subject: unknown,
4646
methodName: string | undefined,
47-
pkgInfo: WrapPackageInfo,
47+
pkgInfo: PartialWrapPackageInfo,
4848
interceptors: InterceptorObject
4949
) {
5050
const agent = getInstance();
@@ -147,7 +147,7 @@ export function inspectArgs(
147147
interceptor: InspectArgsInterceptor,
148148
context: ReturnType<typeof getContext>,
149149
agent: Agent,
150-
pkgInfo: WrapPackageInfo,
150+
pkgInfo: PartialWrapPackageInfo,
151151
methodName: string,
152152
kind: OperationKind | undefined
153153
) {

library/agent/hooks/wrapNewInstance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { getInstance } from "../AgentSingleton";
22
import { wrapDefaultOrNamed } from "./wrapDefaultOrNamed";
3-
import { WrapPackageInfo } from "./WrapPackageInfo";
3+
import { PartialWrapPackageInfo } from "./WrapPackageInfo";
44

55
/**
66
* Intercepts the creation of a new instance of a class, to wrap it's methods and properties.
77
*/
88
export function wrapNewInstance(
99
subject: unknown,
1010
className: string | undefined,
11-
pkgInfo: WrapPackageInfo,
11+
pkgInfo: PartialWrapPackageInfo,
1212
interceptor: (exports: any) => void | unknown
1313
) {
1414
const agent = getInstance();

library/agent/hooks/wrapRequire.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ t.test("Can wrap external package", async (t) => {
2626
exports._test = "aikido";
2727
t.same(pkgInfo.name, "sqlite3");
2828
t.same(pkgInfo.type, "external");
29-
t.ok(pkgInfo.path?.base.endsWith("node_modules/sqlite3"));
30-
t.same(pkgInfo.path?.relative, "lib/sqlite3.js");
29+
if (pkgInfo.type === "external") {
30+
t.ok(pkgInfo.path.base.endsWith("node_modules/sqlite3"));
31+
t.same(pkgInfo.path.relative, "lib/sqlite3.js");
32+
}
3133
});
3234
setPackagesToPatch([pkg]);
3335

@@ -56,8 +58,10 @@ t.test("Can wrap file of external package", async (t) => {
5658
exports._test = "aikido";
5759
t.same(pkgInfo.name, "hono");
5860
t.same(pkgInfo.type, "external");
59-
t.ok(pkgInfo.path?.base.endsWith("node_modules/hono"));
60-
t.same(pkgInfo.path?.relative, "dist/cjs/hono-base.js");
61+
if (pkgInfo.type === "external") {
62+
t.ok(pkgInfo.path.base.endsWith("node_modules/hono"));
63+
t.same(pkgInfo.path.relative, "dist/cjs/hono-base.js");
64+
}
6165
});
6266
setPackagesToPatch([pkg]);
6367

@@ -79,6 +83,7 @@ t.test("Can wrap builtin module", async (t) => {
7983
exports._test = "aikido";
8084
t.same(pkgInfo.name, "fs");
8185
t.same(pkgInfo.type, "builtin");
86+
// @ts-expect-error Test to ensure types are correct
8287
t.same(pkgInfo.path, undefined);
8388
});
8489
setBuiltinModulesToPatch([module]);

library/sinks/MongoDB.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,17 @@ export class MongoDB implements Wrapper {
239239
path: "lib/collection.js",
240240
functions: [
241241
...OPERATIONS_WITH_FILTER.map(
242-
(operation) =>
243-
({
244-
name: operation,
245-
nodeType: "MethodDefinition",
246-
operationKind: "nosql_op",
247-
inspectArgs: (args, agent, collection) =>
248-
this.inspectOperation(
249-
operation,
250-
args,
251-
collection as Collection
252-
),
253-
}) as PackageFunctionInstrumentationInstruction
242+
(operation): PackageFunctionInstrumentationInstruction => ({
243+
name: operation,
244+
nodeType: "MethodDefinition",
245+
operationKind: "nosql_op",
246+
inspectArgs: (args, agent, collection) =>
247+
this.inspectOperation(
248+
operation,
249+
args,
250+
collection as Collection
251+
),
252+
})
254253
),
255254
{
256255
name: "bulkWrite",

library/sinks/Prisma.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Context, getContext } from "../agent/Context";
1212
import { onInspectionInterceptorResult } from "../agent/hooks/onInspectionInterceptorResult";
1313
import { getInstance } from "../agent/AgentSingleton";
1414
import type { Agent } from "../agent/Agent";
15-
import { WrapPackageInfo } from "../agent/hooks/WrapPackageInfo";
15+
import { PartialWrapPackageInfo } from "../agent/hooks/WrapPackageInfo";
1616
import { detectNoSQLInjection } from "../vulnerabilities/nosql-injection/detectNoSQLInjection";
1717
import type { LocalVariableAccessConfig } from "../agent/hooks/instrumentation/types";
1818

@@ -173,7 +173,7 @@ export class Prisma implements Wrapper {
173173
isNoSQLClient: boolean;
174174
sqlDialect?: SQLDialect;
175175
agent: Agent;
176-
pkgInfo: WrapPackageInfo;
176+
pkgInfo: PartialWrapPackageInfo;
177177
}) {
178178
let inspectionResult: InterceptorResult | undefined;
179179
const start = performance.now();
@@ -206,7 +206,10 @@ export class Prisma implements Wrapper {
206206
return query(args);
207207
}
208208

209-
private instrumentPrismaClient(instance: any, pkgInfo: WrapPackageInfo) {
209+
private instrumentPrismaClient(
210+
instance: any,
211+
pkgInfo: PartialWrapPackageInfo
212+
) {
210213
const isNoSQLClient = this.isNoSQLClient(instance);
211214

212215
const agent = getInstance();

0 commit comments

Comments
 (0)