Skip to content

Commit 14ef9c1

Browse files
committed
Add e2e test for hono-sqlite3-esm
1 parent 995d8b0 commit 14ef9c1

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const t = require("tap");
2+
const { spawn } = require("child_process");
3+
const { resolve } = require("path");
4+
const timeout = require("../timeout");
5+
6+
const appDir = resolve(__dirname, "../../sample-apps/hono-sqlite3-esm");
7+
8+
const pathToApp = resolve(appDir, "app.js");
9+
10+
t.test("it blocks in blocking mode", (t) => {
11+
const server = spawn(
12+
`node`,
13+
[
14+
"--preserve-symlinks",
15+
"--import",
16+
"@aikidosec/firewall/esm",
17+
pathToApp,
18+
"4002",
19+
],
20+
{
21+
cwd: appDir,
22+
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCKING: "true" },
23+
}
24+
);
25+
26+
server.on("close", () => {
27+
t.end();
28+
});
29+
30+
server.on("error", (err) => {
31+
t.fail(err.message);
32+
});
33+
34+
let stdout = "";
35+
server.stdout.on("data", (data) => {
36+
stdout += data.toString();
37+
});
38+
39+
let stderr = "";
40+
server.stderr.on("data", (data) => {
41+
stderr += data.toString();
42+
});
43+
44+
// Wait for the server to start
45+
timeout(2000)
46+
.then(() => {
47+
return Promise.all([
48+
fetch("http://127.0.0.1:4002/add", {
49+
method: "POST",
50+
body: JSON.stringify({ name: "Test'), ('Test2');--" }),
51+
headers: {
52+
"Content-Type": "application/json",
53+
},
54+
signal: AbortSignal.timeout(5000),
55+
}),
56+
fetch("http://127.0.0.1:4002/add", {
57+
method: "POST",
58+
body: JSON.stringify({ name: "Miau" }),
59+
headers: {
60+
"Content-Type": "application/json",
61+
},
62+
signal: AbortSignal.timeout(5000),
63+
}),
64+
]);
65+
})
66+
.then(([sqlInjection, normalAdd]) => {
67+
t.equal(sqlInjection.status, 500);
68+
t.equal(normalAdd.status, 200);
69+
t.match(stdout, /Starting agent/);
70+
t.match(stderr, /Zen has blocked an SQL injection/);
71+
})
72+
.catch((error) => {
73+
t.fail(error.message);
74+
})
75+
.finally(() => {
76+
server.kill();
77+
});
78+
});
79+
80+
t.test("it does not block in dry mode", (t) => {
81+
const server = spawn(
82+
`node`,
83+
[
84+
"--preserve-symlinks",
85+
"--import",
86+
"@aikidosec/firewall/esm",
87+
pathToApp,
88+
"4003",
89+
],
90+
{
91+
cwd: appDir,
92+
env: { ...process.env, AIKIDO_DEBUG: "true" },
93+
}
94+
);
95+
96+
server.on("close", () => {
97+
t.end();
98+
});
99+
100+
let stdout = "";
101+
server.stdout.on("data", (data) => {
102+
stdout += data.toString();
103+
});
104+
105+
let stderr = "";
106+
server.stderr.on("data", (data) => {
107+
stderr += data.toString();
108+
});
109+
110+
// Wait for the server to start
111+
timeout(2000)
112+
.then(() =>
113+
Promise.all([
114+
fetch("http://127.0.0.1:4003/add", {
115+
method: "POST",
116+
body: JSON.stringify({ name: "Test'), ('Test2');--" }),
117+
headers: {
118+
"Content-Type": "application/json",
119+
},
120+
signal: AbortSignal.timeout(5000),
121+
}),
122+
fetch("http://127.0.0.1:4003/add", {
123+
method: "POST",
124+
body: JSON.stringify({ name: "Miau" }),
125+
headers: {
126+
"Content-Type": "application/json",
127+
},
128+
signal: AbortSignal.timeout(5000),
129+
}),
130+
])
131+
)
132+
.then(([sqlInjection, normalAdd]) => {
133+
t.equal(sqlInjection.status, 200);
134+
t.equal(normalAdd.status, 200);
135+
t.match(stdout, /Starting agent/);
136+
t.notMatch(stderr, /Zen has blocked an SQL injection/);
137+
})
138+
.catch((error) => {
139+
t.fail(error.message);
140+
})
141+
.finally(() => {
142+
server.kill();
143+
});
144+
});

library/sinks/SQLite3.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ export class SQLite3 implements Wrapper {
7676
.addPackage("sqlite3")
7777
.withVersion("^5.0.0")
7878
.onRequire((exports, pkgInfo) => {
79-
const db = exports.Database.prototype;
79+
let db: any;
80+
if (pkgInfo.isESMImport) {
81+
db = exports.default.Database.prototype;
82+
} else {
83+
db = exports.Database.prototype;
84+
}
8085

8186
for (const func of sqlFunctions) {
8287
wrapExport(db, func, pkgInfo, {

0 commit comments

Comments
 (0)