-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (66 loc) · 1.96 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
var express = require('express');
var db = require('./db');
var generator = require('./generator');
var app = express();
app.set('view engine', 'ejs');
app.set('trust proxy', 'loopback, linklocal, uniquelocal');
function checkRow(row, id) {
if (row.id != id)
return false;
var generatedUser = generator(id);
if (row.password != generatedUser.password)
return false;
if (row.name != generatedUser.name)
return false;
if (row.email != generatedUser.email)
return false;
return true;
}
function injectMe(req, res) {
var id = getId(req);
if (id == -1) {
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
res.render("index", {
rows: null,
fullUrl: fullUrl
});
return;
}
var referrer = req.get('Referrer');
if (referrer != undefined && referrer.startsWith("http")) {
// Matches http:// and https://
res.end();
return;
}
db.all("SELECT id, name, email, password FROM users WHERE id = " + id + ";", function(err, rows) {
if (err) {
// console.log(err);
res.write("An error occured: " + err);
res.end();
return;
}
if (rows.length == 0) {
// do nothing
} else if (rows.length == 1) {
var row = rows[0];
if (!checkRow(row, id)) {
console.info("Unexpected row data with id=<" + id + "> from " + req.ip);
}
} else {
console.info("Unexpected rows.length:" + rows.length + ", expected 1 with id=<" + id + "> from " + req.ip);
}
res.render("index", {
rows: rows
});
res.end();
});
}
function getId(req) {
return req.query.id || req.query.page || -1;
}
app.get('/', injectMe);
app.get('/index.php', injectMe);
app.get('/page.php', injectMe);
app.get('/page', injectMe);
app.listen(8080);