Skip to content

Commit 7efa778

Browse files
authored
Merge pull request #562 from AikidoSec/feature/add-context-warning-to-shouldBlockRequest
feat: add context warning to shouldBlockRequest function
2 parents 605e085 + 2a9305c commit 7efa778

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

library/agent/context/markUnsafe.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ t.test("it works", async () => {
109109
});
110110
markUnsafe("id = 1");
111111
t.same(logs, [
112-
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
112+
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
113113
]);
114114

115115
// Warning logged only once
@@ -124,15 +124,15 @@ t.test("it works", async () => {
124124
markUnsafe(obj);
125125
});
126126
t.same(logs, [
127-
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
127+
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
128128
"markUnsafe(...) failed to serialize the data",
129129
]);
130130

131131
runWithContext(createContext(), () => {
132132
markUnsafe();
133133
});
134134
t.same(logs, [
135-
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
135+
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
136136
"markUnsafe(...) failed to serialize the data",
137137
"markUnsafe(...) was called without any data.",
138138
]);
@@ -141,7 +141,7 @@ t.test("it works", async () => {
141141
markUnsafe(1, true, null, undefined, () => {}, Symbol("test"));
142142
});
143143
t.same(logs, [
144-
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
144+
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
145145
"markUnsafe(...) failed to serialize the data",
146146
"markUnsafe(...) was called without any data.",
147147
"markUnsafe(...) expects an object, array, or string. Received: number",

library/agent/context/markUnsafe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function logWarningMarkUnsafeWithoutContext() {
7272

7373
// eslint-disable-next-line no-console
7474
console.warn(
75-
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen."
75+
"markUnsafe(...) was called without a context. The data will not be tracked. Make sure to call markUnsafe(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports)."
7676
);
7777

7878
loggedWarningMarkUnsafeWithoutContext = true;

library/agent/context/user.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ t.test("usage outside of context", async (t) => {
4242
setUser({ id: "id" });
4343

4444
t.same(logs, [
45-
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen.",
45+
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
4646
]);
4747

4848
// Should not log again

library/agent/context/user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function logWarningSetUserCalledWithoutContext() {
8989

9090
// eslint-disable-next-line no-console
9191
console.warn(
92-
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen."
92+
"setUser(...) was called without a context. The user will not be tracked. Make sure to call setUser(...) within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports)."
9393
);
9494

9595
loggedWarningSetUserCalledWithoutContext = true;

library/middleware/shouldBlockRequest.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as t from "tap";
22
import { shouldBlockRequest } from "./shouldBlockRequest";
33
import { runWithContext, type Context } from "../agent/Context";
44
import { createTestAgent } from "../helpers/createTestAgent";
5+
import { wrap } from "../helpers/wrap";
56

67
const sampleContext: Context = {
78
remoteAddress: "::1",
@@ -19,7 +20,19 @@ const sampleContext: Context = {
1920
};
2021

2122
t.test("without context", async (t) => {
22-
t.same(shouldBlockRequest(), { block: false });
23+
const logs: string[] = [];
24+
wrap(console, "warn", function warn() {
25+
return function warn(message: string) {
26+
logs.push(message);
27+
};
28+
});
29+
30+
const result = shouldBlockRequest();
31+
shouldBlockRequest();
32+
t.same(result, { block: false });
33+
t.same(logs, [
34+
"shouldBlockRequest() was called without a context. The request will not be blocked. Make sure to call shouldBlockRequest() within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports).",
35+
]);
2336
});
2437

2538
t.test("without agent", async (t) => {

library/middleware/shouldBlockRequest.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Result = {
1212
export function shouldBlockRequest(): Result {
1313
const context = getContext();
1414
if (!context) {
15+
logWarningShouldBlockRequestCalledWithoutContext();
1516
return { block: false };
1617
}
1718

@@ -39,3 +40,18 @@ export function shouldBlockRequest(): Result {
3940

4041
return { block: false };
4142
}
43+
44+
let loggedWarningShouldBlockRequestCalledWithoutContext = false;
45+
46+
function logWarningShouldBlockRequestCalledWithoutContext() {
47+
if (loggedWarningShouldBlockRequestCalledWithoutContext) {
48+
return;
49+
}
50+
51+
// eslint-disable-next-line no-console
52+
console.warn(
53+
"shouldBlockRequest() was called without a context. The request will not be blocked. Make sure to call shouldBlockRequest() within an HTTP request. If you're using serverless functions, make sure to use the handler wrapper provided by Zen. Also ensure you import Zen at the top of your main app file (before any other imports)."
54+
);
55+
56+
loggedWarningShouldBlockRequestCalledWithoutContext = true;
57+
}

0 commit comments

Comments
 (0)