-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
69 lines (57 loc) · 1.62 KB
/
index.js
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
const protect = require("@aikidosec/firewall/lambda");
const { MongoClient } = require("mongodb");
const { Users, User } = require("./users");
require("@aikidosec/firewall/nopp");
async function main(client, event) {
const users = new Users(client);
const user = await users.findBy("hans@aikido.dev", "password");
if (!user) {
// Ensure a user exists for testing
await users.persist(new User("hans@aikido.dev", "password"));
}
if (!event.body || event.httpMethod !== "POST") {
return {
statusCode: 405,
body: "Method Not Allowed",
};
}
const body = JSON.parse(event.body);
if (!body.username || !body.password) {
return {
statusCode: 400,
body: "Bad Request",
};
}
// This is just for demo purposes, normally you'd use bcrypt or something
// This is a vulnerability, which can be abused for demo purposes
// If password is { $gt: "" } then it will match any password
const actualUser = await users.findBy(body.username, body.password);
if (!actualUser) {
return {
statusCode: 401,
body: "Unauthorized",
};
}
return {
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token: "123",
success: true,
}),
};
}
exports.handler = protect(async function (event, context) {
// Normally you'd use environment variables for this
const client = new MongoClient("mongodb://root:password@127.0.0.1:27017");
await client.connect();
try {
return await main(client, event, context);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
});