-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
90 lines (81 loc) · 1.9 KB
/
tools.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
var compression = require('compression')
// const info = require("debug")('info')
const express = require('express');
const rateLimit = require("express-rate-limit");
const { NODE_ENV } = process.env
/**
@function https
@param {Object} req Express request
@param {Object} res Express response
@param {Object} next Callback
@returns {Function} The callback
*/
function https(req, res, next) {
if (NODE_ENV !== "production") {
return next()
}
if (req.secure) {
return next()
}
console.log(req)
return res.redirect("https://" + req.hostname + req.url);
}
/**
@function headers
@param {Object} req Express request
@param {Object} res Express response
@param {Object} next Callback
@returns {Function} The callback
*/
function headers(req, res, next) {
res.set({
"X-Download-Options": "noopen",
"X-XSS-Protection": '0', // CSP instead
"Content-Security-Policy":
// Main form
"form-action 'self'; " +
// https
"upgrade-insecure-requests; " +
// bootstrap and local
"default-src 'self' stackpath.bootstrapcdn.com"
})
next();
}
/**
@function security
@param {Object} app Express app
@returns {Function} Middleware func
*/
function security(app) {
// no DoS/DDoS shutdown
app.use(rateLimit({
windowMs: 15 * 60 * 1000,
max: 200
}))
app.disable('x-powered-by')
// Safety headers
app.use(headers)
// HTTPS only
app.use(https)
}
/**
@function config
@param {Object} app Express app
@returns {null} Returns config for app
*/
function config(app) {
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
// Heroku doesn't lie
app.set("trust proxy");
// Minify files
app.use(compression())
}
exports.security = security;
exports.https = https;
// eslint-disable-next-line no-multi-assign
exports = module.exports = app => {
config(app)
security(app)
}
exports.config = config;