forked from sqlpad/sqlpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
315 lines (279 loc) · 10.1 KB
/
app.js
File metadata and controls
315 lines (279 loc) · 10.1 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
import fs from 'fs';
import path from 'path';
import express from 'express';
import helmet from 'helmet';
import pino from 'pino';
import redis from 'redis';
import session from 'express-session';
import FileStoreFactory from 'session-file-store';
import MemoryStoreFactory from 'memorystore';
import SequelizeStoreFactory from 'connect-session-sequelize';
import RedisStore from 'connect-redis';
import appLog from './lib/app-log.js';
import Webhooks from './lib/webhooks.js';
import bodyParser from 'body-parser';
import favicon from 'serve-favicon';
import passport from 'passport';
import authStrategies from './auth-strategies/index.js';
import sessionlessAuth from './middleware/sessionless-auth.js';
import ResponseUtils from './lib/response-utils.js';
import expressPinoLogger from 'express-pino-logger';
import serverDirname from './server-dirname.cjs';
import routePasswordReset from './routes/password-reset.js';
import routeSignout from './routes/signout.js';
import routeSignup from './routes/signup.js';
import routeSignin from './routes/signin.js';
import routeGoogleAuth from './routes/google-auth.js';
import routeAuthOidc from './routes/auth-oidc.js';
import routeSaml from './routes/saml.js';
import routeStatementResults from './routes/statement-results.js';
import routeQueries from './routes/queries.js';
import routeDrivers from './routes/drivers.js';
import routeUsers from './routes/users.js';
import routeConnections from './routes/connections.js';
import routeConnectionAccesses from './routes/connection-accesses.js';
import routeConnectionClients from './routes/connection-clients.js';
import routeConnectionSchema from './routes/connection-schema.js';
import routeTestConnection from './routes/test-connection.js';
import routeQueryHistory from './routes/query-history.js';
import routeSchemaInfo from './routes/schema-info.js';
import routeTags from './routes/tags.js';
import routeFormatSql from './routes/format-sql.js';
import routeServiceTokens from './routes/service-tokens.js';
import routeBatches from './routes/batches.js';
import routeStatements from './routes/statements.js';
import routeApp from './routes/app.js';
const FileStore = FileStoreFactory(session);
const MemoryStore = MemoryStoreFactory(session);
const SequelizeStore = SequelizeStoreFactory(session.Store);
// This is a workaround till BigInt is fully supported by the standard
// See https://tc39.es/ecma262/#sec-ecmascript-language-types-bigint-type
// and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
// If this is not done, then a JSON.stringify(BigInt) throws
// "TypeError: Do not know how to serialize a BigInt"
/* global BigInt:writable */
/* eslint no-extend-native: ["error", { "exceptions": ["BigInt"] }] */
BigInt.prototype.toJSON = function () {
return this.toString();
};
/**
* Create an express app using config
* @param {object} config
*/
async function makeApp(config, models) {
if (typeof config.get !== 'function') {
throw new Error('config is required to create app');
}
if (!models) {
throw new Error('models is required to create app');
}
const webhooks = new Webhooks(config, models, appLog);
const expressPino = expressPinoLogger({
level: config.get('webLogLevel'),
timestamp: pino.stdTimeFunctions.isoTime,
name: 'sqlpad-web',
// express-pino-logger logs all the headers by default
// Removing these for now but open to adding them back in based on feedback
redact: {
paths: [
'req.headers',
'res.headers',
'req.remoteAddress',
'req.remotePort',
],
remove: true,
},
});
/* Express setup
============================================================================= */
const app = express();
app.set('trust proxy', config.get('trustProxy'));
// Default helmet protections, minus frameguard (becaue of sqlpad iframe embed), adding referrerPolicy
app.use(helmet.dnsPrefetchControl());
app.use(helmet.hidePoweredBy());
app.use(helmet.hsts({}));
app.use(helmet.ieNoOpen());
app.use(helmet.noSniff());
app.use(helmet.xssFilter());
app.use(helmet.referrerPolicy({ policy: 'same-origin' }));
// Decorate req and res with SQLPad objects and utils
app.use(function (req, res, next) {
req.config = config;
req.models = models;
req.appLog = appLog;
req.webhooks = webhooks;
res.utils = new ResponseUtils(res, next);
next();
});
app.use(expressPino);
// Use favicon middleware if favicon exists
// Thist just loads it and serves from memory
const icoPath = path.join(serverDirname, '/public/favicon.ico');
if (fs.existsSync(icoPath)) {
app.use(favicon(icoPath));
}
app.use(
bodyParser.json({
limit: config.get('bodyLimit'),
})
);
app.use(
bodyParser.urlencoded({
extended: true,
})
);
const cookieMaxAgeMs = parseInt(config.get('sessionMinutes'), 10) * 60 * 1000;
const cookieSameSite = config.get('sessionCookieSameSite');
const sessionOptions = {
saveUninitialized: false,
resave: true,
rolling: true,
cookie: { maxAge: cookieMaxAgeMs, sameSite: cookieSameSite },
secret: config.get('cookieSecret'),
name: config.get('cookieName'),
};
sessionOptions.cookie.secure = config.get('cookieSecure');
const sessionStore = config.get('sessionStore').toLowerCase();
switch (sessionStore) {
case 'file': {
const sessionPath = path.join(config.get('dbPath'), '/sessions');
sessionOptions.store = new FileStore({
path: sessionPath,
logFn: () => {},
});
break;
}
case 'memory': {
sessionOptions.store = new MemoryStore({
checkPeriod: cookieMaxAgeMs,
});
break;
}
case 'database': {
sessionOptions.store = new SequelizeStore({
db: models.sequelizeDb.sequelize,
table: 'Sessions',
});
// SequelizeStore supports the touch method so per the express-session docs this should be set to false
sessionOptions.resave = false;
// SequelizeStore docs mention setting this to true if SSL is done outside of Node
// Not sure we have any way of knowing based on current config
// sessionOptions.proxy = true;
break;
}
case 'redis': {
const redisClient = redis.createClient({
url: config.get('redisUri'),
});
redisClient.connect().catch((error) => appLog.error(error));
sessionOptions.store = new RedisStore({ client: redisClient });
sessionOptions.resave = false;
break;
}
default: {
throw new Error(`Invalid session store ${sessionStore}`);
}
}
app.use(session(sessionOptions));
const baseUrl = config.get('baseUrl');
app.use(baseUrl, express.static(path.join(serverDirname, 'public')));
/* Passport setup
============================================================================= */
await authStrategies(config, models);
app.use(passport.initialize());
app.use(passport.session());
/* Routes
============================================================================= */
const preAuthRouters = [
routePasswordReset,
routeSignout,
routeSignup,
routeSignin,
routeGoogleAuth,
routeAuthOidc,
routeSaml,
];
// Add pre-auth routes to app
preAuthRouters.forEach((router) => app.use(baseUrl, router));
// Add sessionless authentication middleware
// This handles things like HTTP basic, auth proxy, disable auth, and JWT service tokens
// These attempt to authenticate the request based on information passed every request
// They do not persist a session
app.use(sessionlessAuth);
const authRequiredRouters = [
routeStatementResults,
routeQueries,
routeDrivers,
routeUsers,
routeConnections,
routeConnectionAccesses,
routeConnectionClients,
routeConnectionSchema,
routeTestConnection,
routeQueryHistory,
routeSchemaInfo,
routeTags,
routeFormatSql,
routeServiceTokens,
routeBatches,
routeStatements,
];
// Add all core routes to the baseUrl except for the */api/app route
authRequiredRouters.forEach((router) => app.use(baseUrl, router));
// Add '*/api/app' route last and without baseUrl
app.use(routeApp);
// For any missing api route, return a 404
// NOTE - this cannot be a general catch-all because it might be a valid non-api route from a front-end perspective
app.use(baseUrl + '/api/', function (req, res) {
req.log.debug('reached catch all api route');
return res.utils.notFound();
});
// Add an error handler for /api
app.use(baseUrl + '/api/', function (err, req, res, next) {
if (res.headersSent) {
return next(err);
}
appLog.error(err);
if (err && err.type === 'entity.too.large') {
return res.status(413).json({
title: 'Payload Too Large',
});
}
return res.status(500).json({
title: 'Internal Server Error',
});
});
// Anything else should render the client-side app
// Client-side routing will take care of things from here
// Because index.html will be served via static plugin,
// we need to rename it to something else and switch out the URLs to consider the baseUrl
const indexPath = path.join(serverDirname, 'public/index.html');
const indexTemplatePath = path.join(
serverDirname,
'public/index-template.html'
);
if (fs.existsSync(indexPath)) {
fs.renameSync(indexPath, indexTemplatePath);
}
if (fs.existsSync(indexTemplatePath)) {
const html = fs.readFileSync(indexTemplatePath, 'utf8');
const baseUrlHtml = html
.replace(/="\/assets/g, `="${baseUrl}/assets`)
.replace(/="\/stylesheets/g, `="${baseUrl}/stylesheets`)
.replace(/="\/javascripts/g, `="${baseUrl}/javascripts`)
.replace(/="\/images/g, `="${baseUrl}/images`)
.replace(/="\/favicon/g, `="${baseUrl}/favicon`)
.replace(/="\/fonts/g, `="${baseUrl}/fonts`)
.replace(/="\/static/g, `="${baseUrl}/static`);
app.use((req, res) => res.send(baseUrlHtml));
} else {
const msg = `No UI template detected. Build client/ and copy files to server/public/`;
appLog.warn(msg);
app.use((req, res) => {
appLog.warn(msg);
res.status(404).send(msg);
});
}
return app;
}
export default makeApp;