-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
375 lines (331 loc) · 12.5 KB
/
Copy pathapp.js
File metadata and controls
375 lines (331 loc) · 12.5 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
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
const path = require("path");
const fs = require("fs");
const multer = require("multer");
const express = require("express");
const app = express();
const config = require("./config");
const session = require("express-session");
const CustomRethinkStore = require("./lib/sessionStore");
const passport = require("passport");
const cookieParser = require("cookie-parser");
const server = require("http").createServer(app);
const io = require("socket.io")(server);
global.io = io; // Make io globally available for dev mode notifications
// Socket handlers are attached further down, once the session middleware
// exists — they need it to identify the connecting user.
// --- IMPORT NECESSARY MODELS FOR res.locals DATA ---
const Cart = require("./models/cart"); // <-- ADDED: Import Cart model
const Order = require("./models/order"); // <-- ADDED: Import Order model
// --- END MODEL IMPORTS ---
const flash = require("express-flash");
const Billboard = require("./models/billboard");
const util = require("./lib/util.js");
const security = require("./lib/security");
const routes = require("./routes");
/**
* Coerces a user attribute to a plain string for the view layer.
*
* This used to HTML-escape each field before handing it to EJS. EJS then
* escaped it again on `<%= %>`, so a name like O'Brien reached the page as
* `O&#x27;Brien`. Escaping belongs at the point of output — the templates
* use `<%= %>`, and the one place that embeds this data in a script tag now
* serialises it as JSON.
*
* @param {string} value
* @returns {string}
*/
function asText(value) {
if (value === undefined || value === null) return "";
return String(value);
}
// --- Express App Setup ---
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
// Available to every template, including error pages rendered before the
// per-request locals middleware has run.
app.locals.jsonForScript = require("./lib/sanitize").jsonForScript;
// Required for secure cookies and correct req.ip behind a reverse proxy.
app.set("trust proxy", 1);
// Do not advertise the framework.
app.disable("x-powered-by");
// --- Middleware Chain ---
// Security headers go first so they are present on every response, including
// static assets and error pages.
app.use(security.securityHeaders);
// Serve static assets first, including LESS compilation
app.use(require("less-middleware")(path.join(__dirname, "public")));
app.use(
express.static(path.join(__dirname, "public"), {
etag: false,
maxAge: 0,
setHeaders: (res, path) => {
// Disable caching for JS files to ensure latest version is always loaded
if (path.endsWith(".js")) {
res.set(
"Cache-Control",
"no-store, no-cache, must-revalidate, private",
);
}
},
}),
);
// Body Parsers and Cookie Parser.
// The limit was 50mb, which let an unauthenticated client pin CPU in the
// parser. Nothing here posts bodies anywhere near that size; file uploads go
// through multer, which has its own limit.
app.use(express.json({ limit: "1mb" }));
app.use(express.urlencoded({ extended: false, limit: "1mb" }));
app.use(cookieParser());
// Session Middleware
const r = require("./lib/thinky").r; // r is needed for RethinkDB queries (like in Order.filter)
// Use our custom session store that properly uses the configured database
const store = new CustomRethinkStore(session, {
db: config.dbName || "synbioshop",
table: "sessions",
sessionTimeout: 86400000, // 1 day
flushInterval: 60000, // 1 minute
});
const sessionMiddleware = session({
secret: config.secret,
resave: false,
saveUninitialized: false,
store,
// Default name advertises the stack; a neutral one gives away less.
name: "synbio.sid",
cookie: {
httpOnly: true, // No JS access, limiting XSS payoff.
secure: config.secureCookies, // HTTPS-only in production.
sameSite: "lax", // Blocks cross-site POSTs from carrying the session.
maxAge: 86400000, // 1 day, matching the store's session timeout.
},
});
app.use(sessionMiddleware);
// Passport.js Authentication (initialize and session)
const passportInitialize = passport.initialize();
const passportSession = passport.session();
app.use(passportInitialize);
app.use(passportSession);
// --- Socket.IO ---
// Sockets reuse the HTTP session so handlers can identify the user instead of
// trusting a username supplied in the message payload.
io.engine.use(sessionMiddleware);
io.engine.use(passportInitialize);
io.engine.use(passportSession);
require("./sockets")(io); // index file for sockets
// Express-flash for messages
app.use(flash());
// Multer for file uploads (must come AFTER body-parser for req.body, but BEFORE routes that use files)
// Updated for multer 1.4.5+ which requires using .any() for multi-field uploads
const upload = multer({
dest: config.tmpDir,
limits: {
fileSize: config.maxUploadBytes,
files: 10,
// Bound the non-file parts too, so a multipart body cannot be used to
// sidestep the urlencoded body limit.
fields: 100,
fieldSize: 1024 * 1024,
},
});
app.use((req, res, next) => {
// Only parse multipart bodies, and only for authenticated users.
//
// This used to run on every request from everyone. An anonymous client could
// POST a multipart body to any URL — including 404s — and multer would write
// it to disk unbounded, with nothing to clean it up afterwards. Gating on the
// session closes that as a disk-exhaustion vector.
const isMultipart = /^multipart\/form-data/i.test(
req.get("content-type") || "",
);
if (!isMultipart) return next();
if (!req.isAuthenticated || !req.isAuthenticated()) {
return res.status(401).send("Authentication required to upload files.");
}
upload.any()(req, res, (err) => {
if (err) {
if (err.message === "Unexpected end of form") {
console.warn("Upload aborted: Unexpected end of form");
return res.status(400).send("Upload aborted by client.");
}
if (err.code === "LIMIT_FILE_SIZE") {
return res
.status(413)
.send(
`File too large. The maximum upload size is ${Math.floor(
config.maxUploadBytes / (1024 * 1024),
)} MB.`,
);
}
if (err.code === "LIMIT_FILE_COUNT") {
return res.status(413).send("Too many files in a single upload.");
}
return next(err);
}
next();
});
});
// Remove any temp files the handler did not move into place. Without this,
// aborted or rejected uploads accumulate in tmpDir until the disk fills.
app.use((req, res, next) => {
res.on("finish", () => {
const files = req.files;
if (!files) return;
const list = Array.isArray(files) ? files : Object.values(files);
for (const file of list) {
if (!file || !file.path) continue;
fs.promises.unlink(file.path).catch(() => {
// Already moved into place by the handler, which is the success case.
});
}
});
next();
});
// Backwards compatibility middleware: convert req.files array to object
// Old multer (0.1.8) used req.files as object keyed by fieldname
// New multer (1.4.5+) uses req.files as array with fieldname property
app.use((req, res, next) => {
if (req.files && Array.isArray(req.files)) {
const filesObject = {};
req.files.forEach((file) => {
filesObject[file.fieldname] = file;
});
req.files = filesObject;
}
next();
});
// --- CRITICAL: Call Passport Setup Here ---
util.setupPassport();
// CSRF protection. Mounted after the body and multipart parsers so the token
// is visible for both urlencoded and multipart form posts, and after the
// session so it has somewhere to keep the token.
app.use(security.csrfProtection);
// --- res.locals Middleware (NOW ASYNCHRONOUS for DB fetches) ---
app.use(async (req, res, next) => {
// <<< CHANGED: Made this middleware 'async' <<<
// Make general config values available to all views
res.locals.disablePremade = config.disablePremade;
res.locals.disableCart = config.disableCart;
res.locals.isPricingAvailable = config.isPricingAvailable;
res.locals.pricePerUnit = config.pricePerUnit;
res.locals.devMode = config.devMode; // Pass dev mode flag to views
// Make user data available to all EJS templates as `locals.signedInUser`
if (req.user) {
res.locals.signedInUser = {
username: asText(req.user.username),
name: asText(req.user.name),
mail: asText(req.user.mail),
isAdmin: util.isAdmin(req.user.username),
company: asText(req.user.company),
iconURL: req.user.iconURL ? req.user.iconURL : config.defaultUserIcon,
};
const isAdmin = res.locals.signedInUser.isAdmin;
// Both lookups run for every page view, so they go out together rather
// than one after the other. The open-order count is only rendered in the
// admin badge, so non-admins skip that query entirely — it is an unindexed
// scan of the whole Order table.
const [cartResult, incompleteResult] = await Promise.allSettled([
Cart.filter({ username: req.user.username }).getJoin({ items: true }).run(),
isAdmin
? Order.filter(
r.and(r.row("complete").eq(false), r.row("cancelled").eq(false)),
)
.count()
.execute()
: Promise.resolve(0),
]);
// A failure here must not blank the page; fall back to an empty cart.
if (cartResult.status === "fulfilled") {
const carts = cartResult.value;
res.locals.signedInUser.cart =
carts && carts.length === 1 && carts[0].items ? carts[0] : { items: [] };
} else {
console.error("Error fetching cart for locals:", cartResult.reason);
res.locals.signedInUser.cart = { items: [] };
}
if (incompleteResult.status === "fulfilled") {
res.locals.incompleteCount = incompleteResult.value;
} else {
console.error(
"Error fetching incomplete order count for locals:",
incompleteResult.reason,
);
res.locals.incompleteCount = 0;
}
next(); // Proceed to next middleware ONLY after async fetches complete
} else {
// Not logged in
res.locals.signedInUser = null;
res.locals.incompleteCount = 0; // Default 0 if not logged in
next(); // Proceed to next middleware
}
});
// Middleware to load Billboard (non-blocking) - This middleware is separate and doesn't affect main user data loading flow.
app.use((req, res, next) => {
Billboard.run()
.then((billboards) => {
if (billboards && billboards.length) {
res.locals.billboard = billboards[0];
} else {
res.locals.billboard = null;
}
next();
})
.catch((err) => {
console.error("Error loading billboard:", err);
res.locals.billboard = null;
next();
});
});
// Configure pretty JSON responses (optional)
app.set("json spaces", 2);
// --- Main Routes ---
app.use("/", routes);
// Global Error Handler for common web errors like malformed URIs
app.use((err, req, res, next) => {
if (err instanceof URIError) {
console.warn(`Malformed URI requested: ${req.originalUrl}`);
return res.status(400).send("Bad Request: Invalid URI");
}
next(err);
});
// --- Terminal error handler ---
// Without this, Express's default handler serialises err.stack into the
// response body whenever NODE_ENV is not "production", leaking internals to
// whoever triggered the error. This logs the detail server-side and returns a
// generic page instead.
//
// The unused `next` is required: Express only treats a handler as an error
// handler when it declares four parameters.
app.use((err, req, res, next) => {
const status = err.status || err.statusCode || 500;
if (status >= 500) {
console.error(
`Unhandled error on ${req.method} ${req.originalUrl}:`,
err && err.stack ? err.stack : err,
);
} else {
console.warn(
`${status} on ${req.method} ${req.originalUrl}: ${err && err.message}`,
);
}
// A response already in flight cannot be replaced; let Express abort it.
if (res.headersSent) return next(err);
res.status(status);
const message =
status >= 500
? "Something went wrong on our end. The team has been notified."
: err.message || "Request could not be processed.";
if (req.accepts("html")) {
// Fall back to plain text if the error template itself fails to render.
return res.render("error", { error: message }, (renderErr, html) => {
if (renderErr) {
console.error("Failed to render error page:", renderErr);
return res.type("txt").send(message);
}
return res.send(html);
});
}
return res.json({ error: message });
});
// --- Server Start ---
module.exports = server;