@reldens/server-utils 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@ const session = require('express-session');
13
13
  const rateLimit = require('express-rate-limit');
14
14
  const cors = require('cors');
15
15
  const helmet = require('helmet');
16
- const xss = require('xss-clean');
16
+ const sanitizeHtml = require('sanitize-html');
17
17
 
18
18
  class AppServerFactory
19
19
  {
@@ -26,30 +26,29 @@ class AppServerFactory
26
26
  this.appServer = false;
27
27
  this.app = express();
28
28
  this.rateLimit = rateLimit;
29
- this.useCors = 1 === Number(process.env.RELDENS_USE_CORS || 1);
30
- this.useExpressJson = 1 === Number(process.env.RELDENS_USE_EXPRESS_JSON || 1);
31
- this.useUrlencoded = 1 === Number(process.env.RELDENS_USE_URLENCODED || 1);
32
- this.encoding = String(process.env.RELDENS_DEFAULT_ENCODING || 'utf-8');
33
- this.useHttps = 1 === Number(process.env.RELDENS_EXPRESS_USE_HTTPS || 0);
34
- this.passphrase = String(process.env.RELDENS_EXPRESS_HTTPS_PASSPHRASE || '');
35
- this.httpsChain = String(process.env.RELDENS_EXPRESS_HTTPS_CHAIN || '');
36
- this.keyPath = String(process.env.RELDENS_EXPRESS_HTTPS_PRIVATE_KEY || '');
37
- this.certPath = String(process.env.RELDENS_EXPRESS_HTTPS_CERT || '');
38
- this.trustedProxy = String(process.env.RELDENS_EXPRESS_TRUSTED_PROXY || '');
39
- this.windowMs = Number(process.env.RELDENS_EXPRESS_RATE_LIMIT_MS || 60000);
40
- this.maxRequests = Number(process.env.RELDENS_EXPRESS_RATE_LIMIT_MAX_REQUESTS || 30);
41
- this.applyKeyGenerator = 1 === Number(process.env.RELDENS_EXPRESS_RATE_LIMIT_APPLY_KEY_GENERATOR || 0);
42
- this.jsonLimit = String(process.env.RELDENS_EXPRESS_JSON_LIMIT || '1mb');
43
- this.urlencodedLimit = String(process.env.RELDENS_EXPRESS_URLENCODED_LIMIT || '1mb');
44
- this.useHelmet = 1 === Number(process.env.RELDENS_USE_HELMET || 1);
45
- this.useXssProtection = 1 === Number(process.env.RELDENS_USE_XSS_PROTECTION || 1);
46
- this.globalRateLimit = 1 === Number(process.env.RELDENS_GLOBAL_RATE_LIMIT || 0);
47
- this.corsOrigin = String(process.env.RELDENS_CORS_ORIGIN || '*');
48
- this.corsMethods = String(process.env.RELDENS_CORS_METHODS || 'GET,POST').split(',');
49
- this.corsHeaders = String(process.env.RELDENS_CORS_HEADERS || 'Content-Type,Authorization').split(',');
50
- this.tooManyRequestsMessage = String(
51
- process.env.RELDENS_TOO_MANY_REQUESTS_MESSAGE || 'Too many requests, please try again later.'
52
- );
29
+ this.useCors = true;
30
+ this.useExpressJson = true;
31
+ this.useUrlencoded = true;
32
+ this.encoding = 'utf-8';
33
+ this.useHttps = false;
34
+ this.passphrase = '';
35
+ this.httpsChain = '';
36
+ this.keyPath = '';
37
+ this.certPath = '';
38
+ this.trustedProxy = '';
39
+ this.windowMs = 60000;
40
+ this.maxRequests = 30;
41
+ this.applyKeyGenerator = false;
42
+ this.jsonLimit = '1mb';
43
+ this.urlencodedLimit = '1mb';
44
+ this.useHelmet = true;
45
+ this.helmetConfig = false;
46
+ this.useXssProtection = true;
47
+ this.globalRateLimit = 0;
48
+ this.corsOrigin = '*';
49
+ this.corsMethods = ['GET','POST'];
50
+ this.corsHeaders = ['Content-Type','Authorization'];
51
+ this.tooManyRequestsMessage = 'Too many requests, please try again later.';
53
52
  this.error = {};
54
53
  this.processErrorResponse = false;
55
54
  }
@@ -60,7 +59,7 @@ class AppServerFactory
60
59
  Object.assign(this, appServerConfig);
61
60
  }
62
61
  if(this.useHelmet){
63
- this.app.use(helmet());
62
+ this.app.use(this.helmetConfig ? helmet(this.helmetConfig) : helmet());
64
63
  }
65
64
  if(this.useCors){
66
65
  let corsOptions = {
@@ -86,7 +85,19 @@ class AppServerFactory
86
85
  this.app.use(this.rateLimit(limiterParams));
87
86
  }
88
87
  if(this.useXssProtection){
89
- this.app.use(xss());
88
+ this.app.use((req, res, next) => {
89
+ if(!req.body){
90
+ return next();
91
+ }
92
+ if(typeof req.body === 'object'){
93
+ for(let key in req.body){
94
+ if(typeof req.body[key] === 'string'){
95
+ req.body[key] = sanitizeHtml(req.body[key]);
96
+ }
97
+ }
98
+ }
99
+ next();
100
+ });
90
101
  }
91
102
  if(this.useExpressJson){
92
103
  this.app.use(this.applicationFramework.json({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@reldens/server-utils",
3
3
  "scope": "@reldens",
4
- "version": "0.13.0",
4
+ "version": "0.15.0",
5
5
  "description": "Reldens - Server Utils",
6
6
  "author": "Damian A. Pastorini",
7
7
  "license": "MIT",
@@ -42,6 +42,6 @@
42
42
  "express-session": "1.18.1",
43
43
  "helmet": "8.1.0",
44
44
  "multer": "^1.4.5-lts.2",
45
- "xss-clean": "0.1.4"
45
+ "sanitize-html": "^2.16.0"
46
46
  }
47
47
  }