Repository navigation
Expand file tree
/
Copy pathserver.mjs
More file actions
155 lines (133 loc) · 6.16 KB
/
Copy pathserver.mjs
File metadata and controls
155 lines (133 loc) · 6.16 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
import { readFile } from "node:fs/promises";
import { createServer } from "node:http";
import { fileURLToPath } from "node:url";
import { dirname, resolve, sep } from "node:path";
import { HttpError, handleEdit, readJsonBody } from "./lib/edit-core.mjs";
import { clearedCookie, createSessionToken, matchesPassword, readCookie, SESSION_COOKIE, sessionCookie, sessionIsValid } from "./lib/auth.mjs";
const rootDirectory = dirname(fileURLToPath(import.meta.url));
const port = Number(process.env.PORT || 3000);
const host = process.env.HOST || (process.env.NODE_ENV === "production" ? "0.0.0.0" : "127.0.0.1");
const accessPassword = process.env.ACCESS_PASSWORD || "";
const secureCookies = process.env.NODE_ENV === "production";
const attemptWindowMs = 15 * 60 * 1000;
const maximumAttempts = 8;
const loginAttempts = new Map();
const loginAssets = new Set(["/login", "/login.html", "/login.js"]);
const publicFiles = new Map([
["/", "index.html"],
["/index.html", "index.html"],
["/login", "login.html"],
["/login.html", "login.html"],
["/login.js", "login.js"],
["/styles.css", "styles.css"],
["/app.js", "app.js"],
]);
const contentTypes = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8" };
function setSecurityHeaders(response) {
response.setHeader("X-Content-Type-Options", "nosniff");
response.setHeader("Referrer-Policy", "no-referrer");
response.setHeader("X-Frame-Options", "DENY");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Content-Security-Policy", "default-src 'self'; img-src 'self' blob: data:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; script-src 'self'; connect-src 'self'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'");
}
function sendJson(response, status, body) {
setSecurityHeaders(response);
response.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
response.end(JSON.stringify(body));
}
function requestIp(request) {
if (process.env.TRUST_PROXY === "true") {
const forwarded = request.headers["x-forwarded-for"];
if (typeof forwarded === "string" && forwarded.length < 512) return forwarded.split(",")[0].trim();
}
return request.socket.remoteAddress || "unknown";
}
function isAuthenticated(request) {
if (!accessPassword) return true;
return sessionIsValid(readCookie(request.headers.cookie, SESSION_COOKIE), accessPassword);
}
function hasExhaustedAttempts(ip) {
const now = Date.now();
const record = loginAttempts.get(ip);
if (!record || now - record.startedAt > attemptWindowMs) {
loginAttempts.set(ip, { startedAt: now, count: 0 });
return false;
}
return record.count >= maximumAttempts;
}
function recordFailedAttempt(ip) {
const record = loginAttempts.get(ip);
if (record) record.count += 1;
}
async function handleAuth(request, response) {
if (request.method === "GET") return sendJson(response, 200, { authEnabled: Boolean(accessPassword) });
if (request.method === "DELETE") {
setSecurityHeaders(response);
response.setHeader("Set-Cookie", clearedCookie({ secure: secureCookies }));
return sendJson(response, 200, { ok: true });
}
if (request.method !== "POST") {
setSecurityHeaders(response);
response.setHeader("Allow", "GET, POST, DELETE");
return sendJson(response, 405, { error: "Method not allowed." });
}
if (!accessPassword) return sendJson(response, 200, { ok: true });
const ip = requestIp(request);
if (hasExhaustedAttempts(ip)) return sendJson(response, 429, { error: "Too many attempts. Wait a few minutes and try again." });
let body = null;
try {
body = await readJsonBody(request);
} catch {
body = null;
}
if (!matchesPassword(body?.password, accessPassword)) {
recordFailedAttempt(ip);
return sendJson(response, 401, { error: "That password is not correct." });
}
loginAttempts.delete(ip);
setSecurityHeaders(response);
response.setHeader("Set-Cookie", sessionCookie(createSessionToken(accessPassword), { secure: secureCookies }));
return sendJson(response, 200, { ok: true });
}
async function editImage(request, response) {
try {
const body = await readJsonBody(request);
const { status, body: payload } = await handleEdit({ body, clientIp: requestIp(request) });
return sendJson(response, status, payload);
} catch (error) {
return sendJson(response, error.status || 500, { error: error.status ? error.message : "Something went wrong. Please try again." });
}
}
async function serveFile(request, response, filename) {
const filePath = resolve(rootDirectory, filename);
if (!filePath.startsWith(`${rootDirectory}${sep}`)) return sendJson(response, 404, { error: "Not found." });
try {
const file = await readFile(filePath);
setSecurityHeaders(response);
response.writeHead(200, { "Content-Type": contentTypes[filePath.slice(filePath.lastIndexOf("."))] || "application/octet-stream" });
response.end(request.method === "HEAD" ? undefined : file);
} catch {
sendJson(response, 404, { error: "Not found." });
}
}
const server = createServer(async (request, response) => {
const requestUrl = new URL(request.url, "http://localhost");
if (requestUrl.pathname === "/api/auth") return handleAuth(request, response);
if (requestUrl.pathname === "/api/edit") {
if (request.method !== "POST") return sendJson(response, 405, { error: "Use POST to create an edit." });
if (!isAuthenticated(request)) return sendJson(response, 401, { error: "Sign in to continue." });
return editImage(request, response);
}
if (request.method !== "GET" && request.method !== "HEAD") return sendJson(response, 405, { error: "Method not allowed." });
const filename = publicFiles.get(requestUrl.pathname);
if (!filename) return sendJson(response, 404, { error: "Not found." });
if (accessPassword && !loginAssets.has(requestUrl.pathname) && !isAuthenticated(request)) {
setSecurityHeaders(response);
response.writeHead(302, { Location: "/login" });
return response.end();
}
return serveFile(request, response, filename);
});
server.listen(port, host, () => {
console.log(`Morphly is running at http://${host}:${port}`);
});