• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / blurts-server / #11898

pending completion
#11898

push

circleci

web-flow
Merge pull request #2770 from mozilla/license

Add license headers in source files

282 of 1138 branches covered (24.78%)

Branch coverage included in aggregate %.

959 of 3049 relevant lines covered (31.45%)

2.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/app.js
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4

5
import express from 'express'
6
import session from 'express-session'
7
import connectRedis from 'connect-redis'
8
import helmet from 'helmet'
9
import accepts from 'accepts'
10
import redis from 'redis'
11
import cookieParser from 'cookie-parser'
12

13
import AppConstants from './app-constants.js'
14
import { localStorage } from './utils/local-storage.js'
15
import { errorHandler } from './middleware/error.js'
16
import { doubleCsrfProtection } from './utils/csrf.js'
17
import { initFluentBundles, updateLocale } from './utils/fluent.js'
18
import { loadBreachesIntoApp } from './utils/hibp.js'
19
import indexRouter from './routes/index.js'
20

21
const app = express()
×
22
const isDev = AppConstants.NODE_ENV === 'dev'
×
23

24
// Determine from where to serve client code/assets:
25
// Build script is triggered for `npm start` and assets are served from /dist.
26
// Build script is NOT run for `npm run dev`, assets are served from /src, and nodemon restarts server without build (faster dev).
27
const staticPath = process.env.npm_lifecycle_event === 'start' ? '../dist' : './client'
×
28

29
await initFluentBundles()
×
30

31
async function getRedisStore () {
32
  const RedisStoreConstructor = connectRedis(session)
×
33
  if (['', 'redis-mock'].includes(AppConstants.REDIS_URL)) {
×
34
    const redisMock = await import('redis-mock') // for devs without local redis
×
35
    return new RedisStoreConstructor({ client: redisMock.default.createClient() })
×
36
  }
37
  return new RedisStoreConstructor({ client: redis.createClient({ url: AppConstants.REDIS_URL }) })
×
38
}
39

40
// middleware
41
app.use(helmet())
×
42

43
// disable forced https to allow localhost on Safari
44
app.use(
×
45
  helmet.contentSecurityPolicy({
46
    directives: {
47
      upgradeInsecureRequests: isDev ? null : []
×
48
    }
49
  })
50
)
51

52
// fallback to default 'no-referrer' only when 'strict-origin-when-cross-origin' not available
53
app.use(
×
54
  helmet.referrerPolicy({
55
    policy: ['no-referrer', 'strict-origin-when-cross-origin']
56
  })
57
)
58

59
// When a text/html request is received, negotiate and store the requested language
60
// Using asyncLocalStorage avoids having to pass req context down through every function (e.g. getMessage())
61
app.use((req, res, next) => {
×
62
  if (!req.headers.accept?.startsWith('text/html')) return next()
×
63

64
  localStorage.run(new Map(), () => {
×
65
    req.locale = updateLocale(accepts(req).languages())
×
66
    localStorage.getStore().set('locale', req.locale)
×
67
    next()
×
68
  })
69
})
70

71
// MNTOR-1009:
72
// Because of heroku's proxy settings, request / cookies are not persisted between calls
73
// Setting the trust proxy to high and securing the cookie allowed the cookie to persist
74
// If cookie.secure is set as true, for nodejs behind proxy, "trust proxy" needs to be set
75
if (AppConstants.NODE_ENV === 'heroku') {
×
76
  app.set('trust proxy', 1)
×
77
}
78

79
// session
80
const SESSION_DURATION_HOURS = AppConstants.SESSION_DURATION_HOURS || 48
×
81
app.use(session({
×
82
  cookie: {
83
    maxAge: SESSION_DURATION_HOURS * 60 * 60 * 1000, // 48 hours
84
    rolling: true,
85
    sameSite: 'lax',
86
    secure: !isDev
87
  },
88
  resave: false,
89
  saveUninitialized: true,
90
  secret: AppConstants.COOKIE_SECRET,
91
  store: await getRedisStore()
92
}))
93

94
// Load breaches into namespaced cache
95
try {
×
96
  await loadBreachesIntoApp(app)
×
97
} catch (error) {
98
  console.error('Error loading breaches into app.locals', error)
×
99
}
100

101
app.use(express.static(staticPath))
×
102
app.use(express.json())
×
103
app.use(cookieParser(AppConstants.COOKIE_SECRET))
×
104
app.use(doubleCsrfProtection)
×
105

106
// routing
107
app.use('/', indexRouter)
×
108
app.use(errorHandler)
×
109

110
// start server
111
app.listen(AppConstants.PORT, function () {
×
112
  console.log(`MONITOR V2: Server listening at ${this.address().port}`)
×
113
  console.log(`Static files served from ${staticPath}`)
×
114
})
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc