-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
269 lines (176 loc) · 7.32 KB
/
app.js
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
import path, { join } from "path";
import express from "express";
import favicon from "serve-favicon";
import morgan, { token } from "morgan";
import bodyParser from "body-parser";
import session from "express-session";
import compression from "compression";
import { renderFile } from "pug";
import { getReasonPhrase as _getReasonPhrase } from "http-status-codes";
import helmet, { contentSecurityPolicy, referrerPolicy, crossOriginEmbedderPolicy } from "helmet";
import { randomBytes } from "crypto";
import config from "./config.js";
import indexRouter from "./routes/index.js";
import { router as users } from "./routes/users.js";
import articleRouter from "./routes/article.js";
import slackRouter from "./routes/slack.js";
import changesRouter from "./routes/changes.js";
import blogRouter from "./routes/blog.js";
import toolRouter from "./routes/tool.js";
import publicApiRouter from "./routes/api.js";
import layoutRouter from "./routes/layout.js";
import configRouter from "./routes/config.js";
import auth from "./routes/auth.js";
import rateLimit from "express-rate-limit";
import fileUpload from "express-fileupload";
import _debug from "debug";
import { createProxyMiddleware } from "http-proxy-middleware";
import { createSessionStore } from "./routes/sessionStore.js";
const debug = _debug("OSMBC:app");
// Initialise config Module and variables
config.initialise();
const htmlRoot = config.htmlRoot();
config.logger.info("Express Routes set to: SERVER" + htmlRoot);
const limitValues = config.getValue("limitValues", { mustExist: true });
const limitWindowMs = limitValues.limitWindowsMs ?? 15 * 60 * 1000;
const limitMaxCount = limitValues.limitMaxCount ?? 150;
const limiter = rateLimit({
windowMs: limitWindowMs,
max: limitMaxCount,
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false // Disable the `X-RateLimit-*` headers
});
const app = express();
app.locals.htmlroot = config.htmlRoot();
app.locals.appName = config.getValue("AppName", { mustExist: true });
app.locals.stylesheet = config.getValue("style");
app.locals._path = path;
app.use(helmet());
app.use(limiter);
// Initialise Helmet with Nonce for dynamic generated scripts
app.use((req, res, next) => {
res.locals.cspNonce = randomBytes(16).toString("hex");
if (app.get("env") === "test") res.locals.cspNonce = "Fixed Nonce for Test";
const cspMiddleware = contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
objectSrc: ["'none'"],
imgSrc: ["*"],
styleSrc: ["'self' 'unsafe-inline'"],
upgradeInsecureRequests: [],
scriptSrc: ["'self'", "'unsafe-inline'"], //, `'nonce-${res.locals.cspNonce}'`
scriptSrcAttr: ["'self'", "'unsafe-inline'"] //, `'nonce-${res.locals.cspNonce}'`
}
});
cspMiddleware(res, res, next);
});
app.use(referrerPolicy({ policy: "same-origin" }));
app.use(crossOriginEmbedderPolicy({ policy: "credentialless" }));
// view engine setup
app.set("views", join(config.getDirName(), "views"));
app.set("view engine", "pug");
// compress all requests
app.use(compression());
app.use(favicon(join(config.getDirName(), "public", "images", "favicon.ico")));
// check for media folder foreward
const mediaFolder = config.getValue("media folder", { mustExist: true });
if (mediaFolder["externally mirrored"] === false) {
app.use(mediaFolder.local, createProxyMiddleware({ target: mediaFolder.remote, changeOrigin: true }));
}
// app.use(cookieParser());
app.use(fileUpload({ safeFileNames: true, preserveExtension: true, abortOnLimit: true, limits: { fileSize: 50 * 1024 * 1024 } }));
token("OSMUser", function (req) { return (req.user && req.user.OSMUser) ? req.user.OSMUser : "no user"; });
token("remote-addr", function (req) {
return req.headers["x-real-ip"] || req.headers["x-forwarded-for"] || req.connection.remoteAddress;
});
const logInfoTemplate = config.getValue("logInfoTemplate", { mustExist: true });
if (app.get("env") !== "test") {
app.use(morgan(logInfoTemplate, { stream: config.logger.stream }));
}
if ((app.get("env") === "test") && (process.env.MOCHA_WITH_MORGAN === "TRUE")) {
app.use(morgan(logInfoTemplate, { immediate: true }));
app.use(function(req, res, next) {
console.info("Cookies: ", req.cookies);
return next();
});
}
// first register the unsecured path, with no cookie need.
app.use(htmlRoot, express.static(join(config.getDirName(), "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(htmlRoot + "/api", publicApiRouter);
app.use(htmlRoot + "/slack", slackRouter);
// maxAge for not logged in user cookies is 10 minutes
const cookieMaxAge = config.getValue("cookieMaxAge") * 1000 * 60 * 60 * 24;
config.logger.info("Set Max Age of cookies to " + ((!cookieMaxAge) ? "default" : (cookieMaxAge / (1000 * 60 * 60 * 24) + " days")));
const sessionstore = createSessionStore(session);
// only work with secure cookies, but it looks there are problems in production with it
// TheFive April 2023.
let secure = false;
if (process.env.NODE_ENV === "test") secure = false;
app.use(session(
{
store: sessionstore,
name: config.getValue("SessionName", { mustExist: true }),
secret: config.getValue("SessionSecret", { mustExist: true }),
resave: false,
saveUninitialized: false,
cookie: { maxAge: cookieMaxAge, secure: secure }
}
));
// use CSRF to become more secure, but it looks not working
// may be get started together with test module (switch test to httpS) TheFive April 2023
// if (process.env.NODE_ENV !== "test") app.use(csrf());
// Initialise authenication stuff including passport
auth.initialise(app);
// layout does not render, but prepares the res.rendervar variable for
// dynamic contend in layout.pug
app.use(htmlRoot + "/", auth.ensureAuthenticated, layoutRouter);
app.use(htmlRoot + "/", indexRouter);
app.use(htmlRoot + "/usert", users);
app.use(htmlRoot + "/article", articleRouter.router);
app.use(htmlRoot + "/changes", changesRouter);
app.use(htmlRoot + "/blog", blogRouter);
app.use(htmlRoot + "/tool", toolRouter);
app.use(htmlRoot + "/config", configRouter);
// error handlers
// catch 404 and forward to error handler
app.use(function(req, res, next) {
debug("app.use Error Handler");
const err = new Error("Page Not Found " + req.url);
err.status = 404;
return next(err);
});
app.use(function(err, req, res, next) {
debug("Express Error Handler");
if (res.headersSent) {
return next(err);
}
config.logger.error("Logging Error Stack");
config.logger.error("Error Message " + err.message);
if (app.get("env") !== "production") {
config.logger.error(err.stack);
}
res.status(err.status || 500);
if (err.type && err.type === "API") return res.send(err.message);
const prodErr = { message: err.message, status: err.status };
try {
const errHtml = renderFile(join(config.getDirName(), "views", "error.pug"),
{
message: err.message ?? "no err message",
detail: err.detail ?? null,
error: (app.get("env") !== "production") ? err : prodErr,
nonce: res.locals.cspNonce,
layout: layoutRouter.layoutConst,
getReasonPhrase: _getReasonPhrase
});
res.set({
"Content-Type": "text/html"
});
res.send(errHtml);
} catch (err) {
console.error("Application Error Handler failed");
console.error(err);
}
});
export default app;