security.js 1.08 KB
Newer Older
1
/* global WIKI */
2

NGPixel's avatar
NGPixel committed
3 4 5 6 7 8 9 10
/**
 * Security Middleware
 *
 * @param      {Express Request}   req     Express request object
 * @param      {Express Response}  res     Express response object
 * @param      {Function}          next    next callback function
 * @return     {any}               void
 */
11 12
module.exports = function (req, res, next) {
  // -> Disable X-Powered-By
13
  req.app.disable('x-powered-by')
NGPixel's avatar
NGPixel committed
14

15
  // -> Disable Frame Embedding
16 17 18
  if (WIKI.config.securityIframe) {
    res.set('X-Frame-Options', 'deny')
  }
NGPixel's avatar
NGPixel committed
19

20 21
  // -> Re-enable XSS Fitler if disabled
  res.set('X-XSS-Protection', '1; mode=block')
NGPixel's avatar
NGPixel committed
22

23 24
  // -> Disable MIME-sniffing
  res.set('X-Content-Type-Options', 'nosniff')
NGPixel's avatar
NGPixel committed
25

26 27
  // -> Disable IE Compatibility Mode
  res.set('X-UA-Compatible', 'IE=edge')
NGPixel's avatar
NGPixel committed
28

29
  // -> Disables referrer header when navigating to a different origin
30 31 32 33 34 35 36 37
  if (WIKI.config.securityReferrerPolicy) {
    res.set('Referrer-Policy', 'same-origin')
  }

  // -> Enforce HSTS
  if (WIKI.config.securityHSTS) {
    res.set('Strict-Transport-Security', `max-age=${WIKI.config.securityHSTSDuration}; includeSubDomains`)
  }
NGPixel's avatar
NGPixel committed
38

39 40
  return next()
}