-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
437 lines (415 loc) · 12.3 KB
/
index.ts
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import express, {Response} from "express";
import exphbs from "express-handlebars";
import cookieParser from "cookie-parser";
import {hash, formatDuration, sendAndReceive} from "./util.js";
import {User, Note} from "./types.js";
import {v4 as uuid} from "uuid";
import fetch from "node-fetch";
const app = express();
app.engine(
"hbs",
exphbs({
extname: "hbs",
})
);
app.set("view engine", "hbs");
if (!process.env.PORT) {
throw new Error("Must put port or unix socket in PORT env var.");
}
const PORT = process.env.PORT;
const EXPIRATION_TIME =
Number(process.env.EXPIRATION_TIME) || 24 * 60 * 60 * 1000;
const LEVELS_TO_NAME: Record<string, string> = {
STATIC: "static note",
DYNAMIC: "dynamic note",
IMAGE: "image note",
FILTERED: "filtered note",
};
const LEVELS = Object.keys(LEVELS_TO_NAME);
if (!process.env.FLAGS) {
console.error(
"WARNING: Should put JSON object mapping levels to flags or JSON string with flag in FLAGS env var."
);
}
const FLAGS: Record<string, string> = {};
for (const level of LEVELS) {
FLAGS[level] = "flag{placeholder_flag}";
}
let parsedFlag: any;
try {
parsedFlag = JSON.parse(process.env.FLAGS!);
if (typeof parsedFlag === "string") {
for (const level of LEVELS) {
FLAGS[level] = parsedFlag;
}
} else if (typeof parsedFlag === "object") {
for (const level of LEVELS) {
if (!(level in parsedFlag)) {
console.error(
`WARNING: Level ${level} not specified in FLAGS. Using placeholder flag instead.`
);
} else {
FLAGS[level] = parsedFlag[level];
}
}
} else {
console.error(
"WARNING: FLAGS env var is neither JSON object nor string. Using placeholder flags instead."
);
}
} catch (err) {
console.error("WARNING: FLAGS env var isn't valid JSON. Ignoring.");
}
let COOKIE_FLAG: string | null = null;
if (process.env.COOKIE_FLAG) {
COOKIE_FLAG = process.env.COOKIE_FLAG;
}
let RECAPTCHA_KEYS: {
site: string;
secret: string;
} | null = null;
if (process.env.RECAPTCHA_KEYS) {
const [site, secret] = process.env.RECAPTCHA_KEYS.split(":");
if (!site || !secret) {
console.error(
"WARNING: RECAPTCHA_KEYS needs to be in the format site_key:secret_key. Ignoring."
);
} else {
RECAPTCHA_KEYS = {
site,
secret,
};
}
}
const LB_HOST = process.env.LB_HOST;
if (!LB_HOST) {
throw new Error("Must include LB_HOST environment variable.");
}
const LB_PORT = Number(process.env.LB_PORT);
if (!LB_PORT) {
throw new Error("Must include LB_PORT environment variable.");
}
let SELF_LOC = process.env.SELF_LOC;
if (!SELF_LOC) {
throw new Error("Must include SELF_LOC environment variable.");
}
if (!SELF_LOC.endsWith("/")) {
SELF_LOC += "/";
}
const SELF_DOMAIN = SELF_LOC.split("://")[1].split("/")[0];
const adminIds: Record<string, string> = {};
const revAdminIds: Record<string, string> = {};
const users: Record<string, User> = {};
const notes: Record<string, Note> = {};
for (const level of LEVELS) {
const id = uuid();
adminIds[level] = id;
revAdminIds[id] = level;
users[id] = {
notes: {},
created: new Date(),
id,
};
}
app.use(cookieParser());
app.use(express.urlencoded({extended: false}));
app.use(function (req, res, next) {
const token: string | undefined = req.cookies.sessid;
let user: User;
if (!token || !(token in users)) {
const newToken = uuid();
user = {
notes: {},
created: new Date(),
id: newToken,
};
users[newToken] = user;
res.cookie("sessid", newToken, {
httpOnly: true,
sameSite: "strict",
expires: new Date(Date.now() + EXPIRATION_TIME),
});
} else {
user = users[token];
}
res.locals.user = user;
next();
});
function renderHome(res: Response, error?: string, text: string = "") {
res.render("home", {
levels: Object.entries(LEVELS_TO_NAME),
notes: Object.entries((res.locals.user as User).notes)
.sort((a, b) => b[1].created.getTime() - a[1].created.getTime())
.map((x) => ({
type: x[1].type,
typename: LEVELS_TO_NAME[x[1].type],
id: x[1].id,
time: formatDuration(x[1].created, new Date()),
})),
error,
text,
});
}
function getNoteContent(note: Note): string {
if (note.type === "IMAGE") {
return `<img src="${note.content}">`;
} else {
return note.content;
}
}
app.get("/", function (req, res) {
renderHome(res);
});
app.get("/flag", function (req, res) {
const adminLevel = revAdminIds[req.cookies.sessid];
if (adminLevel) {
res.type("text/plain").send(FLAGS[adminLevel]);
} else {
res.type("text/plain").status(401).send("no flag for you :)");
}
});
app.post("/note", function (req, res) {
if (!req.body.content || typeof req.body.content !== "string") {
res.status(400);
renderHome(res, "Must include a non-empty note content.");
return;
}
if (
!req.body.type ||
typeof req.body.type !== "string" ||
!(req.body.type in LEVELS_TO_NAME)
) {
res.status(400);
renderHome(res, "Must include a valid note type.");
return;
}
if (req.body.content.length > 500) {
res.status(400);
renderHome(res, "Content must be at most 500 chars.");
return;
}
const user: User = res.locals.user;
let content = req.body.content.trim();
const noteHash = hash(req.body.type + " " + content);
if (noteHash in user.notes) {
res.status(400);
renderHome(res, "Note with identical type and content already exists.");
return;
}
const id = uuid();
const note: Note = {
type: req.body.type,
content: content,
created: new Date(),
id,
hash: noteHash,
};
if (note.type === "FILTERED") {
const normalized = note.content.toLowerCase();
for (const ban of [
"script",
"img",
"onerror",
"iframe",
"fetch",
"window",
"eval",
"function",
"then",
"{",
"}",
"xmlhttprequest",
"navigator",
"location",
"self",
]) {
if (normalized.includes(ban)) {
res.status(400);
renderHome(
res,
`Filtered notes can't contain \"${ban}\".`,
note.content
);
return;
}
}
}
notes[note.id] = note;
user.notes[note.hash] = note;
res.redirect("/");
});
app.get("/api/notes/:noteid", function (req, res) {
if (!(req.params.noteid in notes)) {
res.status(400).send("Could not find note.");
return;
}
const note = notes[req.params.noteid];
res.send(getNoteContent(note));
});
app.get("/api/notes/raw/:noteid", function (req, res) {
if (!(req.params.noteid in notes)) {
res.status(400).send("Could not find note.");
return;
}
const note = notes[req.params.noteid];
res.send(note.content);
});
app.get("/notes/:noteid", function (req, res) {
if (!(req.params.noteid in notes)) {
res.status(400).render("error_page", {
error: "Could not find note.",
});
return;
}
const note = notes[req.params.noteid];
if (note.type === "STATIC") {
res.render("static_note", {
content: getNoteContent(note),
id: note.id,
});
} else if (["DYNAMIC", "IMAGE", "FILTERED"].includes(note.type)) {
res.render("dynamic_note", {
id: note.id,
});
} else {
res.status(400);
res.render("error_page", {
error: "Unrenderable note type.",
});
return;
}
});
app.get("/report/:noteid", function (req, res) {
if (!(req.params.noteid in notes)) {
res.status(400).render("error_page", {
error: "Could not find note.",
});
return;
}
res.render("report", {
recaptcha: RECAPTCHA_KEYS,
id: req.params.noteid,
});
});
app.post("/report/:noteid", async function (req, res) {
if (!(req.params.noteid in notes)) {
res.status(400).render("error_page", {
error: "Could not find note.",
});
return;
}
const note = notes[req.params.noteid];
const captcha = req.body["g-recaptcha-response"];
if (RECAPTCHA_KEYS && (!captcha || typeof captcha !== "string")) {
res.status(400).render("report", {
recaptcha: RECAPTCHA_KEYS,
id: req.params.noteid,
error: "Missing RECAPTCHA. Is Javascript enabled?",
});
return;
}
if (RECAPTCHA_KEYS) {
const validation = (await fetch(
"https://www.google.com/recaptcha/api/siteverify",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
secret: RECAPTCHA_KEYS.secret,
response: captcha,
}).toString(),
}
).then((res) => res.json())) as {success: boolean};
if (!validation.success) {
res.status(400).render("report", {
recaptcha: RECAPTCHA_KEYS,
id: req.params.noteid,
error: "Invalid RECAPTCHA. Try submitting again?",
});
return;
}
}
const visitRes = await sendAndReceive(
LB_PORT,
LB_HOST,
"c" +
JSON.stringify({
type: "VISIT",
value: {
url: SELF_LOC + "notes/" + req.params.noteid,
cookies: [
{
name: "sessid",
value: adminIds[note.type],
sameSite: "Strict",
httpOnly: true,
domain: SELF_DOMAIN,
},
...(COOKIE_FLAG
? [
{
name: "cookie_flag",
value: COOKIE_FLAG,
sameSite: "Strict",
httpOnly: false,
domain: SELF_DOMAIN,
},
]
: []),
],
},
}) +
"\n",
true
);
const parsedVisit = JSON.parse(visitRes.trim());
if (parsedVisit.type === "VISIT_RESULT" && parsedVisit.value) {
res.redirect("/");
} else {
res.status(400).render("report", {
recaptcha: RECAPTCHA_KEYS,
id: req.params.noteid,
error: "The report failed. Try reporting again?",
});
}
});
app.use(express.static("./static"));
// cleanup
setInterval(function () {
const threshold = Date.now() - EXPIRATION_TIME;
for (const key in users) {
// don't remove admins
if (key in revAdminIds) {
continue;
}
if (users[key].created.getTime() < threshold) {
delete users[key];
}
}
for (const key in notes) {
if (notes[key].created.getTime() < threshold) {
delete notes[key];
}
}
}, 15000);
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}.`);
if (COOKIE_FLAG) {
console.log("Using cookie flag: " + COOKIE_FLAG);
} else {
console.log("Cookie flag disabled.");
}
console.log("Loaded flags:");
for (const level of LEVELS) {
console.log(` - ${level}: ${FLAGS[level]}`);
}
if (RECAPTCHA_KEYS) {
console.log(
`Using RECAPTCHA site key "${RECAPTCHA_KEYS.site}" and secret key "${RECAPTCHA_KEYS.secret}".`
);
} else {
console.log("RECAPTCHA disabled.");
}
});