-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
198 lines (186 loc) · 4.52 KB
/
Copy pathdb.js
File metadata and controls
198 lines (186 loc) · 4.52 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
189
190
191
192
193
194
195
196
197
198
const
fs = require("fs"),
os = require("os"),
bcrypt = require("bcrypt"),
{Pool} = require("pg");
const
log = require("./log");
const black = require("./blacklist.json");
let
user = process.env.PGUSERNAME || process.env.USER ||
os.userInfo().username,
database = process.env.PGDATABASE || process.env.DB || "u413";
log.info("init PostgreSQL client with", {user, database});
const pool = new Pool({
user, database,
password: process.env.PGPASSWORD || process.env.PASS
});
function rawQuery(q, args) {
return pool.query(q, args).then(res => res.rows);
}
function query(q, args) {
log.debug("Running query", q);
return new Promise((ok, no) => {
if(q in query.cache) {
rawQuery(query.cache[q], args).then(ok, no);
}
else {
fs.readFile(`db/${q}.sql`, (err, data) => {
if(err) {
no(err);
}
else {
log.debug("Caching", `db/${q}.sql`);
rawQuery(query.cache[q] = data.toString(), args).
then(ok, no);
}
});
}
});
}
query.cache = {};
function queryFirst(name, args) {
return query(name, args).then(rows => rows[0]);
}
/**
* Get rid of any characters which may be problematic and are clearly there
* to trip up the system.
**/
function sanitizeName(name) {
// TODO: strip invisible format characters
return name.
// Space normalization
replace(/\s+/g, ' ').
// Control characters
replace(/[\0-\x1f]/g, '');
}
/**
* Produces a string which normalizes all look-alikes for use in the
* searchname field.
**/
function searchableName(name) {
// TODO: normalize homoglyphs (including stuff like l and 1)
// TODO: sanitize invalid unicode
// TODO: strip combining characters
return sanitizeName(name).toLowerCase().replace(/[\s\/]+/g, '');
}
/**
* Converts invalid usernames to "nobody", which is special.
**/
function blacklistName(name) {
name = searchableName(name);
return (
// Names that are misleading or could easily leak from a bug and give
// unexpected privilege escalation
black.indexOf(name) === -1 ||
// Bad characters
/[@&|`'"%?]+/.test(name) ||
// Content filetype extensions which could introduce security holes
/\.(xml|svg|pdf|rss|atom|.?html?)$/.test(name) ||
// Dynamic content filetype extensions
/\.(php(\d+)?|cgi|axd|as[mhp]?x|pl|jspx?|swf|ht[ac]|rb)$/.test(name) ||
// Dotfiles
/^\./.test(name)
)? name : "nobody";
}
const SALTS = 10;
const db = module.exports = {
pool,
rawQuery,
query,
bulletin: {
getAll() {
return query("bulletin/all").then(
all => {
for(let post of all) {
post.created = post.created.getTime();
}
return all;
}
);
},
add(userid, text) {
return query("bulletin/new", [userid, text]);
}
},
board: {
getAll() {
return query("board/all");
},
byName(name) {
return queryFirst("board/byname", [name]);
},
list(id) {
return query("board/list", [id]);
}
},
topic: {
getAll(id) {
return query("topic/all", [id]);
},
byId(id) {
return queryFirst("topic/byid", [id]);
},
replies(id) {
return query("topic/replies", [id]).then(async replies => {
return await Promise.all(replies.map(async r => {
r.author = await db.user.byId(r.author);
r.created = r.created.getTime();
return r;
}));
});
},
create(board, author, title, body) {
return queryFirst("topic/create", [board, author, title, body]);
},
reply(topic, author, body) {
return queryFirst("topic/reply", [topic, author, body]).then(
async reply => {
reply.board = (await queryFirst("topic/board", [reply.topic])).name;
return reply;
}
)
}
},
user: {
authenticate(name, pass) {
return this.byName(name).then(user => {
if(user) {
// Don't chain the promise because we need user
return bcrypt.compare(pass, user.pass).then(eq => {
return eq? user : null;
}).catch(err => {
return null;
});
}
else {
return null;
}
})
},
async passwd(name, pass) {
return !!queryFirst("user/passwd", [searchableName(name),
await bcrypt.hash(pass, SALTS)]);
},
async inGroup(uid, gname) {
// count(*) returns a string for some reason, + will coerce it.
return !!+(await queryFirst("user/ingroup", [uid, gname])).count;
},
groups(uid) {
return query("user/groups", [uid]);
},
byId(id) {
return queryFirst("user/byid", [id]);
},
byName(name) {
return queryFirst("user/byname", [searchableName(name)]);
},
async add(name, pass) {
return queryFirst("user/add", [name, searchableName(name),
await bcrypt.hash(pass, SALTS)]);
},
list() {
return query("user/list");
}
}
};