security.js 1.34 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
  if (WIKI.config.security.securityIframe) {
17 18
    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
  if (WIKI.config.security.securityReferrerPolicy) {
31 32 33 34
    res.set('Referrer-Policy', 'same-origin')
  }

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

39 40 41
  // -> Prevent Open Redirect from user provided URL
  if (WIKI.config.security.securityOpenRedirect) {
    // Strips out all repeating / character in the provided URL
NGPixel's avatar
NGPixel committed
42
    req.url = req.url.replace(/(\/)(?=\/*\1)/g, '')
43 44
  }

45 46
  return next()
}