-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathfunctions-framework-sqlite3.test.js
More file actions
183 lines (164 loc) · 4.42 KB
/
Copy pathfunctions-framework-sqlite3.test.js
File metadata and controls
183 lines (164 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const t = require("tap");
const { spawn } = require("child_process");
const { resolve } = require("path");
const timeout = require("../timeout");
const appDir = resolve(
__dirname,
"../../sample-apps/functions-framework-sqlite3"
);
t.test("it blocks in blocking mode", (t) => {
const server = spawn(
`./node_modules/.bin/functions-framework`,
["--port", "4010"],
{
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCK: "true" },
cwd: appDir,
}
);
server.on("close", () => {
t.end();
});
server.on("error", (err) => {
t.fail(err.message);
});
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
});
let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
});
// Wait for the server to start
timeout(2000)
.then(() => {
return Promise.all([
fetch("http://127.0.0.1:4010/?petname=Kitty'); DELETE FROM cats;-- H", {
signal: AbortSignal.timeout(5000),
}),
fetch("http://127.0.0.1:4010/?petname=Miau", {
signal: AbortSignal.timeout(5000),
}),
]);
})
.then(([sqlInjection, normalAdd]) => {
t.equal(sqlInjection.status, 500);
t.equal(normalAdd.status, 200);
t.match(stdout, /Starting agent/);
t.match(
stderr,
/Zen has blocked an SQL injection: better-sqlite3.exec\(\.\.\.\) originating from query\.petname/
);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
});
});
t.test("it does not block in dry mode", (t) => {
const server = spawn(
`./node_modules/.bin/functions-framework`,
["--port", "4011"],
{
env: { ...process.env, AIKIDO_DEBUG: "true", AIKIDO_BLOCK: "false" },
cwd: appDir,
}
);
server.on("close", () => {
t.end();
});
server.on("error", (err) => {
t.fail(err.message);
});
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
});
let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
});
// Wait for the server to start
timeout(2000)
.then(() => {
return Promise.all([
fetch("http://127.0.0.1:4011/?petname=Kitty'); DELETE FROM cats;-- H", {
signal: AbortSignal.timeout(5000),
}),
fetch("http://127.0.0.1:4011/?petname=Miau", {
signal: AbortSignal.timeout(5000),
}),
]);
})
.then(([sqlInjection, normalAdd]) => {
t.equal(sqlInjection.status, 200);
t.equal(normalAdd.status, 200);
t.match(stdout, /Starting agent/);
t.notMatch(
stderr,
/Zen has blocked an SQL injection: better-sqlite3.exec\(\.\.\.\) originating from query\.petname/
);
t.notMatch(
stdout,
/AIKIDO: Zen is disabled\. Configure one of the following environment variables to enable it: AIKIDO_BLOCK, AIKIDO_TOKEN, AIKIDO_DEBUG, AIKIDO_INSTRUMENT/
);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
});
});
t.test("it does not enable Zen when no environment variables are set", (t) => {
const server = spawn(
`./node_modules/.bin/functions-framework`,
["--port", "4012"],
{
env: { ...process.env, AIKIDO_CI: false },
cwd: appDir,
}
);
server.on("close", () => {
t.end();
});
server.on("error", (err) => {
t.fail(err.message);
});
let stdout = "";
server.stdout.on("data", (data) => {
stdout += data.toString();
});
let stderr = "";
server.stderr.on("data", (data) => {
stderr += data.toString();
});
// Wait for the server to start
timeout(2000)
.then(() => {
return Promise.all([
fetch("http://127.0.0.1:4012/?petname=Kitty'); DELETE FROM cats;-- H", {
signal: AbortSignal.timeout(5000),
}),
fetch("http://127.0.0.1:4012/?petname=Miau", {
signal: AbortSignal.timeout(5000),
}),
]);
})
.then(([sqlInjection, normalAdd]) => {
t.equal(sqlInjection.status, 200);
t.equal(normalAdd.status, 200);
t.match(
stdout,
/AIKIDO: Zen is disabled\. Configure one of the following environment variables to enable it: AIKIDO_BLOCK, AIKIDO_TOKEN, AIKIDO_DEBUG, AIKIDO_INSTRUMENT/
);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
});
});