-
Notifications
You must be signed in to change notification settings - Fork 0
/
paste.ts
725 lines (645 loc) · 21.7 KB
/
paste.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/**
* A Pastebin-like backend using Zippy
*
* @author John L. Carveth <[email protected]>
* @version 1.12.2
* @namespace nutty
*
* Provides basic authentication via /api/login and /api/register routes.
* Tokens are provided with the "X-Access-Token" header
* Post a text file to /api/paste and a UUID is returned on success.
* GET /api/:uuid to retrieve that text file.
* GET /api/paste to return the UUIDs of all stored pastes
*/
import { addRoute, get, listen, post } from "./server.ts";
import {
extname,
resolve,
SEP,
} from "https://deno.land/[email protected]/path/mod.ts";
import { serveFile } from "https://deno.land/[email protected]/http/file_server.ts";
import { verify } from "./auth.ts";
import { SQLiteService as service } from "./db.ts";
import { highlightText } from "https://deno.land/x/[email protected]/dist/index.js";
import { detectLanguage } from "https://deno.land/x/[email protected]/dist/detect.js";
import { Layout, LayoutData } from "./templates/layout.ts";
import { Index } from "./templates/index.ts";
import { Login } from "./templates/login.ts";
import { Register } from "./templates/register.ts";
import { _404 } from "./templates/404.ts";
import { Burn } from "./templates/burn.ts";
import { About } from "./templates/about.ts";
const SQLiteService = service.getInstance();
const TARGET_DIR = Deno.env.get("TARGET_DIR") || "/opt/paste/";
const BASE_URL = Deno.env.get("BASE_URL");
const PUBLIC_PASTES = Deno.env.get("PUBLIC_PASTES") || false;
const MAX_SIZE = Number(Deno.env.get("MAX_SIZE")) || 1e6;
const DOMAIN = Deno.env.get("DOMAIN");
export const PORT = Number.parseInt(<string> Deno.env.get("PORT") ?? 5335);
export const version = "1.12.2";
function getCookieValue(cookieString: string, cookieName: string) {
const cookies = cookieString.split("; ");
for (let i = 0; i < cookies.length; i++) {
const cookieParts = cookies[i].split("=");
if (cookieParts[0] === cookieName) {
return cookieParts[1];
}
}
return null;
}
function serveIndex() {
const data: LayoutData = {
title: "Paste.ts",
content: Index(),
version,
scripts: [
`<script src="/js/login-check.js" type="module"></script>`,
`<script src="/js/recent-posts.js" type="module"></script>`,
],
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
}
function serveAbout() {
const data: LayoutData = {
title: "About Paste.ts",
content: About(),
version,
stylesheets: [`<link rel="stylesheet" href="/css/about.css"/>`],
scripts: [
`<script src="/js/login-check.js" type="module"></script>`,
],
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
}
/* Serve HTML webpages */
get("/index.html", serveIndex);
get("/", serveIndex);
get("/about", serveAbout);
get("/login", () => {
const data: LayoutData = {
title: "Paste.ts",
content: Login(),
version,
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
});
get("/register", () => {
const data: LayoutData = {
title: "Paste.ts",
content: Register(),
version,
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
});
get("/burn/:uuid", (req, _path, params) => {
const filename = params?.uuid;
if (!filename) return new Response("Bad Request", { status: 400 });
const data: LayoutData = {
title: "Paste.ts",
version,
content: Burn(`${DOMAIN}/paste/${filename}`),
scripts: [
'<script src="/js/login-check.js" type="module"></script>',
'<script src="/js/burnable-clip.js" type="module"></script>',
],
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
});
get("/paste/:uuid", async (req, _path, params) => {
const filename = params?.uuid;
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
if (!filename) return new Response("Bad Request", { status: 400 });
/* Before checking token, see if a public paste w/ this UUID exists */
if (PUBLIC_PASTES) {
try {
await Deno.lstat(`${TARGET_DIR}/public/${filename}`);
const text = await Deno.readTextFile(`${TARGET_DIR}/public/${filename}`);
const language = detectLanguage(text);
const highlighted = await highlightText(text, language, false);
const data: LayoutData = {
title: "Paste.ts",
content:
`<div class="code-block" id="code-block"><pre><code>${highlighted}</code></pre><button class="copy" id="copy"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-copy" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"></path>
</svg></button></div>`,
version,
stylesheets: ['<link rel="stylesheet" href="/css/highlight.css"/>'],
scripts: [
'<script src="/js/login-check.js" type="module"></script>',
'<script src="/js/clipboard.js" type="module"></script>',
],
};
/* Check if burnable, delete file if so */
if (SQLiteService.isBurnable(filename)) {
await Deno.remove(`${TARGET_DIR}/public/${filename}`);
SQLiteService.removeBurnable(filename);
}
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
} catch (_err) {
/* Public paste not found, continue checking authentication */
}
}
if (!token) {
/* No token provided, PUBLIC_PASTES is either false or no public paste was found */
return new Response("Unauthorized", { status: 401 });
}
let uuid = "";
try {
const payload = await verify(token);
uuid = payload.userid as string;
} catch (_err) {
/* Invalid / Expired token was provided */
return new Response("Unauthorized", { status: 401 });
}
/* Check that the directory exists */
try {
await Deno.lstat(`${TARGET_DIR}/${uuid}/${filename}`);
const text = await Deno.readTextFile(`${TARGET_DIR}/${uuid}/${filename}`);
const language = detectLanguage(text);
const highlighted = await highlightText(text, language, false);
const data: LayoutData = {
title: "Paste.ts",
content:
`<div class="code-block"><pre><code>${highlighted}</code></pre><button class="copy">Copy to Clipboard</button></div>`,
version,
stylesheets: ['<link rel="stylesheet" href="/css/highlight.css"/>'],
scripts: ['<script src="/js/login-check.js" type="module"></script>'],
};
/* Check if burnable, delete file if so */
if (SQLiteService.isBurnable(filename)) {
await Deno.remove(`${TARGET_DIR}/${uuid}/${filename}`);
SQLiteService.removeBurnable(filename);
}
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
} catch (_err) {
return new Response("Not Found", { status: 404 });
}
});
/**
* Serve static CSS files
* @function
* @name GET-/css/*
* @memberof nutty
* @returns the requested CSS file, or an HTTP error
*/
get("/css/*", async (_req, _path, params) => {
if (!params) return new Response("Bad Request", { status: 400 });
const filepath = `static/css/${params[0]}`;
const resolvedPath = resolve(Deno.cwd(), filepath);
/* Ensure the requested file is contained within the static directory */
if (!resolvedPath.startsWith(`${Deno.cwd()}${SEP}static${SEP}css`)) {
return new Response("Bad Request", { status: 400 });
}
/* Ensure the file has a .css extension to prevent serving non-css files */
if (extname(resolvedPath) !== ".css") {
return new Response("Bad Request", { status: 400 });
}
/* Check for existence of params[0] within /static/css/ */
try {
await Deno.lstat(filepath);
} catch (_err) {
return new Response("Not found.", { status: 404 });
}
try {
const css = await Deno.readTextFile(filepath);
return new Response(css, { headers: { "Content-Type": "text/css" } });
} catch (_err) {
return new Response("Server Error", { status: 500 });
}
});
/**
* Serve static JS files
* @function
* @name GET-/js/*
* @memberof nutty
* @returns the requested file, or an HTTP error
*/
get("/js/*", async (_req, _path, params) => {
if (!params) return new Response("Bad Request", { status: 400 });
const filepath = `static/js/${params[0]}`;
const resolvedPath = resolve(Deno.cwd(), filepath);
/* Ensure the requested file is contained within the static directory */
if (!resolvedPath.startsWith(`${Deno.cwd()}${SEP}static${SEP}js`)) {
return new Response("Bad Request", { status: 400 });
}
/* Check for existence of params[0] within static/js */
try {
await Deno.lstat(filepath);
} catch (_err) {
return new Response("Not Found", { status: 404 });
}
/* Finally, serve the file */
try {
const js = await Deno.readTextFile(filepath);
return new Response(js, { headers: { "Content-Type": "text/javascript" } });
} catch (_err) {
return new Response("Server Error", { status: 500 });
}
});
/**
* Authenticate with the API to recieve an access token
* @function
* @name POST-/api/login
* @memberof nutty
* @param {string} uuid - the UUID that was obtained through registration
* @param {string} password - the valid password for the account
* @returns {string} a jsonwebtoken on success
*/
post("/api/login", async (req, _path, _params) => {
let body;
if (req.headers.get("Content-Type")?.includes("x-www-form-urlencoded")) {
const query = await req.text();
const params = new URLSearchParams(query);
body = {
email: params.get("email"),
password: params.get("password"),
};
} else {
body = await req.json();
}
const email = body.email;
const password = body.password;
if (!email || !password) {
return new Response("Invalid request. Missing parameters.", {
status: 400,
});
}
try {
const token = await SQLiteService.login(email, password);
const headers = {
"Set-Cookie": `token=${token}; Max-Age=86400; Path=/`,
};
if (req.headers.get("Accept")?.includes("text/html")) {
headers["Location"] = `/`;
return new Response(token, { headers, status: 302 });
}
return new Response(token, { headers });
} catch (_err) {
if (req.headers.get("Content-Type")?.includes("x-www-form-urlencoded")) {
const headers = { "Location": "/login#failed" };
return new Response(null, { headers, status: 302 });
}
return new Response("Unauthorized", { status: 401 });
}
});
/**
* Removes any existing tokens stored as a cookie
* @function
* @name POST-/api/logout
* @memberof nutty
* @returns A 302 redirect
*/
post("/api/logout", (_req, _path, _params) => {
const headers = {
"Set-Cookie": `token=""; Max-Age=0; Path=/`,
"Location": "/",
};
return new Response(null, { status: 302, headers });
});
/**
* Registers a new account
* @function
* @name POST-/api/regiser
* @memberof nutty
* @param {string} password - the valid password for the account
* @returns {string} a UUID
*/
post("/api/register", async (req, _path, _params) => {
let body;
if (req.headers.get("Content-Type")?.includes("x-www-form-urlencoded")) {
const query = await req.text();
const params = new URLSearchParams(query);
body = {
email: params.get("email"),
password: params.get("password"),
};
} else {
body = await req.json();
}
const email = body.email;
const password = body.password;
if (!email || !password) {
return new Response("Missing parameters", { status: 400 });
}
try {
const uuid = SQLiteService.register(email, password);
if (req.headers.get("Accept")?.includes("text/html")) {
const headers = { "Location": "/login" };
return new Response(null, { headers, status: 302 });
}
return new Response(uuid);
} catch (err) {
if (err.message === "UNIQUE constraint failed: users.email") {
return new Response("Conflict", { status: 409 });
}
return new Response("Server Error", { status: 500 });
}
});
/**
* Allows the client to check the validity of their login token
* @function
* @name GET-/api/auth/status
* @memberof nutty
* @returns {string} 200 OK if the token is valid, 401 Unauthorized if not.
*/
get("/api/auth/status", async (req, _path, _params) => {
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
if (!token) return new Response("Unauthorized", { status: 401 });
try {
// verify() throws an error if token is invalid
await verify(token);
return new Response("OK", { status: 200 });
} catch (_err) {
return new Response("Unauthorized", { status: 401 });
}
});
/**
* Stores the provided text file on the filesystem, and returns a UUID to the client
* for later access.
* @function
* @name POST-/api/paste
* @memberof nutty
* @param {string} text - the text content to be stored
* @returns {string} a UUID identifying the new paste
*/
post("/api/paste", async (req, _path, _params) => {
const contentLength = req.headers.get("Content-Length");
if (contentLength && Number(contentLength) > MAX_SIZE) {
return new Response("Payload too large.", { status: 413 });
}
const filename = crypto.randomUUID();
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
const accepts = req.headers.get("Accept");
const contentType = req.headers.get("Content-Type");
const html = (accepts !== null) ? accepts.includes("text/html") : false;
let text;
/* Add the UUID to the burn_on_read table if param set */
let burn;
if (contentType) {
if (contentType.includes("application/x-www-form-urlencoded") && html) {
const formData = await req.formData();
text = formData.get("text") as string;
burn = formData.get("burn") as string;
} else {
text = await req.text();
const queryParams = new URL(req.url).searchParams;
burn = queryParams.get("burn");
}
}
if (burn) {
console.log(`BURN`);
SQLiteService.createBurnable(filename);
}
const data = (new TextEncoder()).encode(text);
if (!token) {
if (!PUBLIC_PASTES) {
return new Response(
"Unauthorized",
{
status: 401,
},
);
} else {
await Deno.writeFile(`${TARGET_DIR}/public/${filename}`, data);
if (accepts !== null && accepts.includes("text/html")) {
return new Response(null, {
status: 302,
headers: {
"Location": (burn ? `/burn/${filename}` : `/paste/${filename}`),
},
});
}
return new Response(filename);
}
}
let uuid = "";
try {
const payload = await verify(token);
uuid = payload.userid as string;
} catch (_err) {
return new Response("Unauthorized", { status: 401 });
}
await Deno.mkdir(`${TARGET_DIR}/${uuid}`, { recursive: true });
await Deno.writeFile(`${TARGET_DIR}/${uuid}/${filename}`, data);
if (accepts !== null && accepts.includes("text/html")) {
return new Response(null, {
status: 302,
headers: {
"Location": (burn ? `/burn/${filename}` : `/paste/${filename}`),
},
});
}
return new Response(filename);
});
/**
* Returns an array of UUIDs of all pastes belonging to the user
* @function
* @name GET-/api/paste
* @memberof nutty
* @returns {Array} an array of UUIDs associated to pastes
*/
get("/api/paste", async (req, _path, _params) => {
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
if (token) {
let uuid = "";
try {
const payload = await verify(token);
uuid = payload.userid as string;
} catch (_err) {
return new Response("Unauthorized", { status: 401 });
}
/* Check that directory exists */
try {
await Deno.lstat(`${TARGET_DIR}/${uuid}`);
} catch (_err) {
return new Response(JSON.stringify([]));
}
const files = [];
for await (const filename of Deno.readDir(`${TARGET_DIR}/${uuid}`)) {
files.push(filename.name);
}
return new Response(JSON.stringify(files));
} else {
/* Return top 5 most recent public pastes, if PUBLIC_PASTES=1 */
if (!PUBLIC_PASTES) {
return new Response("Unauthorized", { status: 401 });
}
const publicFiles = [];
let recentFiles = [];
try {
const publicDir = `${TARGET_DIR}/public`;
for await (const dirEntry of Deno.readDir(publicDir)) {
const file = await Deno.stat(`${publicDir}/${dirEntry.name}`);
file.name = dirEntry.name;
publicFiles.push(file);
}
recentFiles = publicFiles.sort((a, b) => {
return b.mtime!.getTime() - a.mtime!.getTime();
}).slice(0, 5);
} catch (_err) {
return new Response("Server Error", { status: 500 });
}
return new Response(JSON.stringify(recentFiles.map((item) => item.name)));
}
});
/**
* Returns the current version of Nutty
* @function
* @name GET-/api/version
* @memberof nutty
* @returns {string} the current version of Nutty
*/
get("/api/version", (_req, _path, _params) => {
return new Response(version);
});
/**
* Symlinks a paste to the public folder, allowing it to be accessed by anyone
* without a login token.
* TODO: Is GET the best method for this route? PUT instead?
* TODO: What if paste is already public?
* @function
* @name GET-/api/share/:uuid
* @param {string} uuid - the uuid of the paste to be made public
* @memberof nutty
* @returns {string} the a URL pointing to the public paste
*/
get("/api/share/:uuid", async (req, _path, params) => {
const uuid = params?.uuid;
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
let userid = "";
if (!token) return new Response("Unauthorized");
try {
const payload = await verify(token);
userid = payload.userid as string;
} catch (_err) {
return new Response("Unauthorized");
}
// check if file exists
try {
await Deno.lstat(`${TARGET_DIR}/${userid}/${uuid}`);
} catch (_err) {
return new Response("File not found.");
}
//File exists, create symlink
await Deno.symlink(
`${TARGET_DIR}/${userid}/${uuid}`,
`${TARGET_DIR}/public/${uuid}`,
);
// Return https://api.jlcarveth.dev/api/:uuid
return new Response(`${BASE_URL}/${uuid}`);
});
// Dynamic URLs have to be matched last
/**
* Returns the paste with the given UUID, if the user has access/the paste is public.
* @function
* @name GET-/api/:uuid
* @param {string} uuid - the uuid of the paste to retrieve
* @memberof nutty
* @returns {string} the contents of the paste with given UUID
*/
get("/api/:uuid", async (req, _path, params) => {
const filename = params?.uuid;
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
if (!filename) return new Response("Bad Request", { status: 400 });
let uuid = "";
// Before checking token, look in TARGET_DIR/public for the uuid
try {
await Deno.lstat(`${TARGET_DIR}/public/${filename}`);
const response = await serveFile(req, TARGET_DIR + "/public/" + filename);
/* Check if burnable, delete file if so */
if (SQLiteService.isBurnable(filename)) {
await Deno.remove(`${TARGET_DIR}/public/${filename}`);
SQLiteService.removeBurnable(filename);
}
return response;
} catch (_err) {
// File not found in public/, continue with authentication
}
if (!token) return new Response("Unauthorized", { status: 401 });
try {
const payload = await verify(token);
uuid = payload.userid as string;
} catch (_err) {
return new Response("Unauthorized", { status: 401 });
}
try {
await Deno.lstat(`${TARGET_DIR}/${uuid}/${filename}`);
} catch (_err) {
return new Response("Not Found", { status: 404 });
}
const response = serveFile(req, TARGET_DIR + "/" + uuid + "/" + filename);
/* Check if burnable, delete file if so */
if (SQLiteService.isBurnable(filename)) {
await Deno.remove(`${TARGET_DIR}/${uuid}/${filename}`);
SQLiteService.removeBurnable(filename);
}
return response;
});
/**
* Removes the paste with the given UUID, if the user has access.
* @function
* @name DELETE-/api/:uuid
* @param {string} uuid - the uuid of the paste to remove
* @memberof nutty
* @returns {string} OK
*/
addRoute("/api/:uuid", "DELETE", async (req, _path, params) => {
const cookie = getCookieValue(req.headers.get("Cookie") ?? "", "token");
const token = req.headers.get("X-Access-Token") || cookie;
if (!token) return new Response("Unauthorized", { status: 401 });
let userID = "";
try {
const payload = await verify(token);
userID = payload.userid as string;
} catch (_err) {
return new Response("Unauthorized", { status: 401 });
}
const uuid = params?.uuid;
// Try to remove public/uuid if it exists
try {
await Deno.remove(`${TARGET_DIR}/public/${uuid}`);
} catch (_err) {
// Do nothing, symlink not found
}
try {
await Deno.remove(`${TARGET_DIR}/${userID}/${uuid}`);
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return new Response("Not Found", { status: 404 });
}
return new Response("An unexpected error has occurred.", { status: 500 });
}
return new Response("OK");
});
/* A catch-all 404 page if no other route matches. */
get("*", () => {
const data: LayoutData = {
title: "Not Found",
content: _404(),
version,
};
return new Response(Layout(data), {
headers: { "Content-Type": "text/html" },
});
});
listen(PORT);