-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathapp.js
More file actions
188 lines (157 loc) · 4.51 KB
/
app.js
File metadata and controls
188 lines (157 loc) · 4.51 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
184
185
186
187
188
const Zen = require("@aikidosec/firewall");
const xml2js = require("xml2js");
const { serve } = require("@hono/node-server");
const { Hono } = require("hono");
const { createConnection } = require("./db");
const Cats = require("./Cats");
const { XMLParser } = require("fast-xml-parser");
async function main() {
const app = new Hono();
const db = await createConnection();
const cats = new Cats(db);
app.use(async (c, next) => {
const userId = c.req.header("x-user-id");
if (userId) {
Zen.setUser({
id: userId,
});
}
await next();
});
Zen.addHonoMiddleware(app);
app.get("/", async (c) => {
const catNames = await cats.getAll();
return c.html(
`
<html lang="en">
<body>
<h1>Vulnerable app using XML</h1>
<ul id="list">
${catNames.map((name) => `<li>${name}</li>`).join("")}
</ul>
<form id="add-cat">
<label for="search">Add a new cat</label>
<input type="text" name="petname">
<input type="submit" value="Add" />
</form>
<p>SQL Injection: '); DELETE FROM cats;-- H</p>
<a href="/clear">Clear all cats</a>
<script>
document.addEventListener("DOMContentLoaded", () => {
const form = document.getElementById("add-cat");
form.addEventListener("submit", async (event) => {
event.preventDefault();
fetch("/add", {
method: "POST",
body: "<cat><name>" + form.petname.value + "</name></cat>",
}).then(response => response.json())
.then(data => {
if(!data.success) {
throw new Error("Response was not successful");
}
window.location.reload();
})
.catch(error => document.getElementById("list").innerHTML = "<li>" + error.message + "</li>");
});
});
</script>
</body>
</html>
`
);
});
app.post("/add", async (c) => {
const body = await c.req.text();
let result;
try {
const parser = new xml2js.Parser();
result = await parser.parseStringPromise(body);
} catch (err) {
return c.json({ error: "Invalid XML" }, 400);
}
await cats.add(result.cat.name[0]);
return c.json({ success: true });
});
app.post("/add-attribute", async (c) => {
const body = await c.req.text();
let result;
try {
const parser = new xml2js.Parser();
result = await parser.parseStringPromise(body);
} catch (err) {
return c.json({ error: "Invalid XML" }, 400);
}
await cats.add(result.cat.$.name);
return c.json({ success: true });
});
app.get("/admin", async (c) => {
return c.html(
`<html lang="en">
<body>
<h1>Admin panel</h1>
</body>
</html>`
);
});
app.get("/admin/public", async (c) => {
return c.html(
`<html lang="en">
<body>
<h1>Public subpage of the admin panel</h1>
</body>
</html>`
);
});
app.post("/add-fast", async (c) => {
const body = await c.req.text();
let result;
try {
const parser = new XMLParser();
result = await parser.parse(body);
} catch (err) {
return c.json({ error: "Invalid XML" }, 400);
}
await cats.add(result.cat.name);
return c.json({ success: true });
});
app.post("/add-fast-attribute", async (c) => {
const body = await c.req.text();
let result;
try {
const parser = new XMLParser({
ignoreAttributes: false,
});
result = await parser.parse(body);
} catch (err) {
return c.json({ error: "Invalid XML" }, 400);
}
await cats.add(result.cat["@_name"]);
return c.json({ success: true });
});
app.get("/clear", async (c) => {
try {
await db.execute("DELETE FROM cats;");
return c.redirect("/", 302);
} catch (err) {
return c.json({ error: "Failed to clear cats" }, 500);
}
});
return app;
}
function getPort() {
const port = parseInt(process.argv[2], 10) || 4000;
if (isNaN(port)) {
console.error("Invalid port");
process.exit(1);
}
return port;
}
main().then((app) => {
const port = getPort();
serve({
fetch: app.fetch,
port: port,
}).on("listening", () => {
console.log(`Server is running on port ${port}`);
});
});