-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathapp.js
More file actions
164 lines (142 loc) · 3.6 KB
/
app.js
File metadata and controls
164 lines (142 loc) · 3.6 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
require("dotenv").config();
require("@aikidosec/firewall");
const Cats = require("./Cats");
const restify = require("restify");
const { Client } = require("pg");
require("@aikidosec/firewall/nopp");
function getHTMLBody(cats) {
return `
<html lang="en">
<body>
<p>All cats : ${cats.join(", ")}</p>
<form action="/" method="GET">
<label for="search">Add a new cat</label>
<input type="text" name="petname">
<input type="submit" value="Add" />
</form>
<a href="http://localhost:4000/?petname=Kitty'); DELETE FROM cats_2;-- H">Test injection</a> / <a href="http://localhost:4000/clear">Clear table</a>
</body>
</html>`;
}
async function createConnection() {
const client = new Client({
user: "root",
host: "127.0.0.1",
database: "main_db",
password: "password",
port: 27016,
});
await client.connect();
await client.query(`
CREATE TABLE IF NOT EXISTS cats_2 (
petname varchar(255),
comment varchar(255)
);
`);
return client;
}
async function main(port) {
const db = await createConnection();
const cats = new Cats(db);
const server = restify.createServer({
name: "restify-postgres-sample",
version: "1.0.0",
});
// Use restify plugins
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.gzipResponse());
server.get("/", (req, res, next) => {
let promise = Promise.resolve();
if (req.query["petname"]) {
promise = cats.add(req.query["petname"]);
}
promise
.then(() => {
return cats.getAll();
})
.then((allCats) => {
res.writeHead(200, {
"Content-Type": "text/html",
});
res.end(getHTMLBody(allCats));
return next();
})
.catch((err) => {
res.send(500, err.message);
return next();
});
});
server.post("/string-concat", (req, res, next) => {
if (!req.body.petname) {
res.send(400, "Missing petname");
return next();
}
db.query(
`INSERT INTO cats_2 (petname, comment) VALUES ('${req.body.petname}');`
)
.then(() => {
return cats.getAll();
})
.then((allCats) => {
res.send(allCats);
return next();
})
.catch((err) => {
res.send(500, err.message);
return next();
});
});
server.get("/string-concat", (req, res, next) => {
if (!req.query.petname) {
res.send(400, "Missing petname");
return next();
}
db.query(
`INSERT INTO cats_2 (petname, comment) VALUES ('${req.query.petname}');`
)
.then(() => {
return cats.getAll();
})
.then((allCats) => {
res.send(allCats);
return next();
})
.catch((err) => {
res.send(500, err.message);
return next();
});
});
server.get("/clear", (req, res, next) => {
db.query("DELETE FROM cats_2;")
.then(() => {
res.header("Location", "/");
res.send(302);
return next();
})
.catch((err) => {
res.send(500, "Error clearing table");
return next();
});
});
return new Promise((resolve, reject) => {
try {
server.listen(port, () => {
console.log(`${server.name} listening at ${server.url}`);
resolve();
});
} catch (err) {
reject(err);
}
});
}
function getPort() {
const port = parseInt(process.argv[2], 10) || 4000;
if (isNaN(port)) {
console.error("Invalid port");
process.exit(1);
}
return port;
}
main(getPort());