-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
307 lines (276 loc) · 9.34 KB
/
server.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
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
// Core dependencies
const crypto = require('crypto')
const path = require('path')
// NPM dependencies
const Sentry = require('@sentry/node')
// Tracing needs to be imported for it to work
// Source: https://docs.sentry.io/platforms/node/performance/
// eslint-disable-next-line no-unused-vars
const Tracing = require('@sentry/tracing')
const compression = require('compression')
const flash = require('connect-flash')
const cookieParser = require('cookie-parser')
const express = require('express')
const session = require('express-session')
const grant = require('grant-express')
const helmet = require('helmet')
const i18nMiddleware = require('i18next-express-middleware')
const morgan = require('morgan')
const responseTime = require('response-time')
const favicon = require('serve-favicon')
// Local dependencies
const healthcheckApp = require('./app/healthcheck')
const locationsApp = require('./app/locations')
const router = require('./app/router')
const metrics = require('./common/lib/metrics')
const breadcrumbs = require('./common/middleware/breadcrumbs')
const checkSession = require('./common/middleware/check-session')
const ensureAuthenticated = require('./common/middleware/ensure-authenticated')
const ensureSelectedLocation = require('./common/middleware/ensure-selected-location')
const errorHandlers = require('./common/middleware/errors')
const locals = require('./common/middleware/locals')
const { setCanAccess } = require('./common/middleware/permissions')
const processOriginalRequestBody = require('./common/middleware/process-original-request-body')
const sentryEnrichScope = require('./common/middleware/sentry-enrich-scope')
const sentryRequestId = require('./common/middleware/sentry-request-id')
const setApiClient = require('./common/middleware/set-api-client')
const setCacheControl = require('./common/middleware/set-cache-control')
const setDevelopmentTools = require('./common/middleware/set-development-tools')
const setLocation = require('./common/middleware/set-location')
const setLocations = require('./common/middleware/set-locations')
const setPrimaryNavigation = require('./common/middleware/set-primary-navigation')
const setServices = require('./common/middleware/set-services')
const setTransactionId = require('./common/middleware/set-transaction-id')
const setUser = require('./common/middleware/set-user')
const config = require('./config')
const i18n = require('./config/i18n').default
const nunjucks = require('./config/nunjucks')
const { getAssetPath } = require('./config/nunjucks/globals')
const configPaths = require('./config/paths')
module.exports = async () => {
let redisStore
if (config.REDIS.SESSION) {
redisStore = await require('./config/redis-store')()
}
require('./common/lib/azure-appinsights').initialiseAppInsights()
// Global constants
const app = express()
if (config.IS_PRODUCTION) {
app.enable('trust proxy')
}
// Ensure we have a useful transaction id
app.use(setTransactionId)
if (config.SENTRY.DSN) {
Sentry.init({
debug: config.SENTRY.DEBUG,
dsn: config.SENTRY.DSN,
environment: config.SENTRY.ENVIRONMENT,
release: config.SENTRY.RELEASE,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],
// 10% of all requests will be used for performance sampling
tracesSampler: samplingContext => {
const transactionName =
samplingContext &&
samplingContext.transactionContext &&
samplingContext.transactionContext.name
if (
(transactionName && transactionName.includes('ping')) ||
transactionName.includes('/healthcheck')
) {
return 0
} else {
return 0.01
}
},
})
app.use(
Sentry.Handlers.requestHandler({
// Ensure we don't include `data` to avoid sending any PPI
request: ['cookies', 'headers', 'method', 'query_string', 'url'],
user: ['id', 'username', 'permissions'],
})
)
app.use(sentryRequestId)
app.use(Sentry.Handlers.tracingHandler())
}
// Configure prometheus to handle metrics
metrics.init(app, config)
// Load the healthcheck app manually before anything
// else to ensure it will return some kind of response
// regardless of the app failing elsewhere
app.use(healthcheckApp.mountpath, healthcheckApp.router)
// view engine setup
app.set('view engine', 'njk')
nunjucks(app, config, configPaths)
// Static files
app.use(compression())
app.use(
favicon(path.join(configPaths.build, getAssetPath('images/favicon.ico')))
)
app.use(
express.static(configPaths.build, {
maxAge: config.STATIC_ASSETS.CACHE_MAX_AGE,
})
)
app.use(
'/assets',
express.static(
path.join(__dirname, '/node_modules/govuk-frontend/dist/govuk/assets'),
{ maxAge: config.STATIC_ASSETS.CACHE_MAX_AGE }
),
express.static(
path.join(
__dirname,
'/node_modules/@ministryofjustice/frontend/moj/assets'
),
{ maxAge: config.STATIC_ASSETS.CACHE_MAX_AGE }
)
)
// Cache control headers should be set after static assets but
// before other requests
app.use(setCacheControl)
// ensure i18n is loaded early as needed for error template
app.use(i18nMiddleware.handle(i18n))
const loggingFormat = config.IS_PRODUCTION ? 'combined' : 'dev'
app.use(morgan(loggingFormat))
app.use(express.json())
app.use(express.urlencoded({ extended: true, limit: '1mb' }))
app.use(cookieParser())
app.use(responseTime())
app.use(
session({
store: redisStore,
secret: config.SESSION.SECRET,
name: config.SESSION.NAME,
saveUninitialized: false,
resave: false,
rolling: true,
cookie: {
secure: config.IS_PRODUCTION,
maxAge: config.SESSION.TTL,
httpOnly: true,
},
})
)
app.use(checkSession)
app.use(setUser)
app.use(sentryEnrichScope)
app.use(flash())
app.use(breadcrumbs.init())
app.use(locals)
app.use(
grant({
defaults: {
origin:
(config.IS_PRODUCTION ? 'https' : 'http') +
`://${config.SERVER_HOST}`,
callback: '/auth/callback',
transport: 'session',
state: true,
},
...config.AUTH_PROVIDERS,
})
)
// Set API client
app.use(setApiClient)
// Set services
app.use(setServices)
// Development environment helpers
if (config.IS_DEV) {
require('axios-debug-log')
const development = require('./common/middleware/development')
app.use(development.bypassAuth(config.AUTH_BYPASS_SSO))
app.use(development.setUserPermissions(config.USER_PERMISSIONS))
app.use(development.setUserLocations(config.USER_LOCATIONS))
app.use(development.setUsername(config.USER_USERNAME))
}
app.use(
ensureAuthenticated({
provider: config.DEFAULT_AUTH_PROVIDER,
whitelist: config.AUTH_WHITELIST_URLS,
expiryMargin: config.AUTH_EXPIRY_MARGIN,
})
)
app.use(
ensureSelectedLocation({
locationsMountpath: locationsApp.mountpath,
whitelist: config.AUTH_WHITELIST_URLS,
})
)
app.use(setLocations)
app.use('.*(?<!image)$', setLocation)
app.use((req, res, next) => {
res.locals.cspNonce = crypto.randomBytes(16).toString('hex')
next()
})
// unsafe-inline is required as govuk-template injects `js-enabled` class via inline script
app.use(
helmet({
crossOriginEmbedderPolicy: false,
crossOriginOpenerPolicy: false,
crossOriginResourcePolicy: false,
originAgentCluster: false,
contentSecurityPolicy: {
useDefaults: false,
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
"'unsafe-inline'",
'cdnjs.cloudflare.com',
'www.googletagmanager.com',
'www.google-analytics.com',
'region1.google-analytics.com',
'cdn.jsdelivr.net',
"'sha256-GUQ5ad8JK5KmEWmROf3LZd9ge94daqNvd8xy9YS1iDw='",
(req, res) => `'nonce-${res.locals.cspNonce}'`,
],
connectSrc: [
"'self'",
"'unsafe-inline'",
'www.googletagmanager.com',
'www.google-analytics.com',
'region1.google-analytics.com',
'api.os.uk',
'region1.analytics.google.com',
'stats.g.doubleclick.net',
],
imgSrc: [
"'self'",
'www.google-analytics.com',
'region1.google-analytics.com',
'api.os.uk',
'data: blob:',
'images.ctfassets.net',
'www.google.co.uk/ads/ga-audiences',
],
fontSrc: ["'self'", 'fonts.googleapis.com'],
styleSrc: ["'self'", "'unsafe-inline'", 'cdn.jsdelivr.net'],
},
},
referrerPolicy: {
policy: 'strict-origin-when-cross-origin',
},
})
)
// Ensure body processed after reauthentication
app.use(processOriginalRequestBody())
// Add permission checker
app.use(setCanAccess)
// Routing
app.use(setPrimaryNavigation)
if (config.ENABLE_DEVELOPMENT_TOOLS) {
app.use(setDevelopmentTools)
}
app.use(router)
// error handling
app.use(Sentry.Handlers.errorHandler())
app.use(errorHandlers.notFound)
app.use(errorHandlers.catchAll(config.IS_DEV))
return app
}