2019-08-24 22:19:35 -04:00
|
|
|
/* global WIKI */
|
2017-02-08 20:52:37 -05:00
|
|
|
|
2016-08-16 20:56:55 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-02-08 20:52:37 -05:00
|
|
|
module.exports = function (req, res, next) {
|
|
|
|
// -> Disable X-Powered-By
|
2017-07-24 22:37:13 -04:00
|
|
|
req.app.disable('x-powered-by')
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2017-02-08 20:52:37 -05:00
|
|
|
// -> Disable Frame Embedding
|
2020-05-29 18:24:20 -04:00
|
|
|
if (WIKI.config.security.securityIframe) {
|
2019-08-24 22:19:35 -04:00
|
|
|
res.set('X-Frame-Options', 'deny')
|
|
|
|
}
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2017-02-08 20:52:37 -05:00
|
|
|
// -> Re-enable XSS Fitler if disabled
|
|
|
|
res.set('X-XSS-Protection', '1; mode=block')
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2017-02-08 20:52:37 -05:00
|
|
|
// -> Disable MIME-sniffing
|
|
|
|
res.set('X-Content-Type-Options', 'nosniff')
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2017-02-08 20:52:37 -05:00
|
|
|
// -> Disable IE Compatibility Mode
|
|
|
|
res.set('X-UA-Compatible', 'IE=edge')
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2017-10-12 22:24:48 -05:00
|
|
|
// -> Disables referrer header when navigating to a different origin
|
2020-05-29 18:24:20 -04:00
|
|
|
if (WIKI.config.security.securityReferrerPolicy) {
|
2019-08-24 22:19:35 -04:00
|
|
|
res.set('Referrer-Policy', 'same-origin')
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Enforce HSTS
|
2020-05-29 18:24:20 -04:00
|
|
|
if (WIKI.config.security.securityHSTS) {
|
2019-08-24 22:19:35 -04:00
|
|
|
res.set('Strict-Transport-Security', `max-age=${WIKI.config.securityHSTSDuration}; includeSubDomains`)
|
|
|
|
}
|
2016-08-16 20:56:55 -04:00
|
|
|
|
2020-05-29 18:24:20 -04:00
|
|
|
// -> Prevent Open Redirect from user provided URL
|
|
|
|
if (WIKI.config.security.securityOpenRedirect) {
|
|
|
|
// Strips out all repeating / character in the provided URL
|
2020-05-30 16:42:48 -04:00
|
|
|
req.url = req.url.replace(/(\/)(?=\/*\1)/g, '')
|
2020-05-29 18:24:20 -04:00
|
|
|
}
|
|
|
|
|
2017-02-08 20:52:37 -05:00
|
|
|
return next()
|
|
|
|
}
|