Skip to content

Commit 264b5c4

Browse files
authored
Merge pull request #455 from AikidoSec/dd-trace-compat
Add end2end test for compatibility with dd-trace
2 parents 6a0920c + 54ae21a commit 264b5c4

File tree

4 files changed

+600
-2
lines changed

4 files changed

+600
-2
lines changed

end2end/tests/express-postgres.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,76 @@ t.test("it does not block in dry mode", (t) => {
140140
server.kill();
141141
});
142142
});
143+
144+
t.test("it blocks in blocking mode (with dd-trace)", (t) => {
145+
const server = spawn(
146+
`node`,
147+
["--preserve-symlinks", "--require", "dd-trace/init", pathToApp, "4002"],
148+
{
149+
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCKING: "true" },
150+
cwd: resolve(__dirname, "../../sample-apps/express-postgres"),
151+
}
152+
);
153+
154+
server.on("close", () => {
155+
t.end();
156+
});
157+
158+
server.on("error", (err) => {
159+
t.fail(err.message);
160+
});
161+
162+
let stdout = "";
163+
server.stdout.on("data", (data) => {
164+
stdout += data.toString();
165+
});
166+
167+
let stderr = "";
168+
server.stderr.on("data", (data) => {
169+
stderr += data.toString();
170+
});
171+
172+
// Wait for the server to start
173+
timeout(2000)
174+
.then(() => {
175+
return Promise.all([
176+
fetch(
177+
`http://localhost:4002/?petname=${encodeURIComponent("Njuska'); DELETE FROM cats_2;-- H")}`,
178+
{
179+
signal: AbortSignal.timeout(5000),
180+
}
181+
),
182+
fetch(`http://localhost:4002/string-concat`, {
183+
method: "POST",
184+
headers: { "Content-Type": "application/json" },
185+
body: JSON.stringify({ petname: ["'", "1)", "(0,1)", "(1", "'"] }),
186+
signal: AbortSignal.timeout(5000),
187+
}),
188+
fetch(
189+
`http://localhost:4002/string-concat?petname='&petname=1)&petname=(0,1)&petname=(1&petname='`,
190+
{
191+
signal: AbortSignal.timeout(5000),
192+
}
193+
),
194+
fetch("http://localhost:4002/?petname=Njuska", {
195+
signal: AbortSignal.timeout(5000),
196+
}),
197+
]);
198+
})
199+
.then(
200+
async ([sqlInjection, sqlInjection2, sqlInjection3, normalSearch]) => {
201+
t.equal(sqlInjection.status, 500);
202+
t.equal(sqlInjection2.status, 500);
203+
t.equal(sqlInjection3.status, 500);
204+
t.equal(normalSearch.status, 200);
205+
t.match(stdout, /Starting agent/);
206+
t.match(stderr, /Zen has blocked an SQL injection/);
207+
}
208+
)
209+
.catch((error) => {
210+
t.fail(error);
211+
})
212+
.finally(() => {
213+
server.kill();
214+
});
215+
});

library/sinks/Fetch.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ export class Fetch implements Wrapper {
132132
if (typeof globalThis.fetch === "function") {
133133
// Fetch is lazy loaded in Node.js
134134
// By calling fetch() we ensure that the global dispatcher is available
135-
// @ts-expect-error Type is not defined
136-
globalThis.fetch().catch(() => {});
135+
try {
136+
// @ts-expect-error Type is not defined
137+
globalThis.fetch().catch(() => {});
138+
} catch (error) {
139+
// Ignore errors
140+
}
137141
}
138142

139143
hooks.addGlobal("fetch", {

0 commit comments

Comments
 (0)