@middy/http-security-headers 2.5.6 → 2.5.7

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.
Files changed (2) hide show
  1. package/index.js +86 -80
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,6 +1,9 @@
1
- const { normalizeHttpResponse } = require('@middy/util')
1
+ "use strict";
2
+
3
+ const {
4
+ normalizeHttpResponse
5
+ } = require('@middy/util'); // Code and Defaults heavily based off https://helmetjs.github.io/
2
6
 
3
- // Code and Defaults heavily based off https://helmetjs.github.io/
4
7
 
5
8
  const defaults = {
6
9
  // contentDisposition: {
@@ -33,6 +36,7 @@ const defaults = {
33
36
  },
34
37
  permittedCrossDomainPolicies: {
35
38
  policy: 'none' // none, master-only, by-content-type, by-ftp-filename, all
39
+
36
40
  },
37
41
  referrerPolicy: {
38
42
  policy: 'no-referrer'
@@ -40,126 +44,128 @@ const defaults = {
40
44
  xssFilter: {
41
45
  reportUri: ''
42
46
  }
43
- }
44
-
45
- const helmet = {}
46
- const helmetHtmlOnly = {}
47
-
48
- // OWASP ASVS 14.4.2
47
+ };
48
+ const helmet = {};
49
+ const helmetHtmlOnly = {}; // OWASP ASVS 14.4.2
49
50
  // API Gateway strips out this header :(
50
51
  // helmet.content = (headers, config) => {
51
52
  // const filename = config.filename ?? `api.${headers?.['Content-Type'].split(/[/;]/)[1] ?? 'json'}`
52
53
  // headers['Content-Disposition'] = `attachment; filename="${filename}"`
53
54
  // }
54
-
55
55
  // contentSecurityPolicy - N/A - no HTML
56
56
  // featurePolicy - N/A - no HTML
57
-
58
57
  // crossdomain - N/A - For Adobe products
59
-
60
58
  // https://github.com/helmetjs/dns-Prefetch-control
59
+
61
60
  helmet.dnsPrefetchControl = (headers, config) => {
62
- headers['X-DNS-Prefetch-Control'] = config.allow ? 'on' : 'off'
63
- return headers
64
- }
61
+ headers['X-DNS-Prefetch-Control'] = config.allow ? 'on' : 'off';
62
+ return headers;
63
+ }; // expectCt - in-progress spec
64
+ // https://github.com/helmetjs/frameguard
65
65
 
66
- // expectCt - in-progress spec
67
66
 
68
- // https://github.com/helmetjs/frameguard
69
67
  helmetHtmlOnly.frameguard = (headers, config) => {
70
- headers['X-Frame-Options'] = config.action.toUpperCase()
71
- return headers
72
- }
68
+ headers['X-Frame-Options'] = config.action.toUpperCase();
69
+ return headers;
70
+ }; // https://github.com/helmetjs/hide-powered-by
71
+
73
72
 
74
- // https://github.com/helmetjs/hide-powered-by
75
73
  helmet.hidePoweredBy = (headers, config) => {
76
74
  if (config.setTo) {
77
- headers['X-Powered-By'] = config.setTo
75
+ headers['X-Powered-By'] = config.setTo;
78
76
  } else {
79
- Reflect.deleteProperty(headers, 'Server')
80
- Reflect.deleteProperty(headers, 'X-Powered-By')
77
+ Reflect.deleteProperty(headers, 'Server');
78
+ Reflect.deleteProperty(headers, 'X-Powered-By');
81
79
  }
82
- return headers
83
- }
84
-
85
- // hpkp - deprecated
86
80
 
81
+ return headers;
82
+ }; // hpkp - deprecated
87
83
  // https://github.com/helmetjs/hsts
84
+
85
+
88
86
  helmet.hsts = (headers, config) => {
89
- let header = 'max-age=' + Math.round(config.maxAge)
87
+ let header = 'max-age=' + Math.round(config.maxAge);
88
+
90
89
  if (config.includeSubDomains) {
91
- header += '; includeSubDomains'
90
+ header += '; includeSubDomains';
92
91
  }
92
+
93
93
  if (config.preload) {
94
- header += '; preload'
94
+ header += '; preload';
95
95
  }
96
- headers['Strict-Transport-Security'] = header
97
- return headers
98
- }
99
96
 
100
- // https://github.com/helmetjs/ienoopen
101
- helmet.ieNoOpen = (headers, config) => {
102
- headers['X-Download-Options'] = config.action
103
- return headers
104
- }
97
+ headers['Strict-Transport-Security'] = header;
98
+ return headers;
99
+ }; // https://github.com/helmetjs/ienoopen
105
100
 
106
- // noCache - N/A - separate middleware
107
101
 
102
+ helmet.ieNoOpen = (headers, config) => {
103
+ headers['X-Download-Options'] = config.action;
104
+ return headers;
105
+ }; // noCache - N/A - separate middleware
108
106
  // https://github.com/helmetjs/dont-sniff-mimetype
107
+
108
+
109
109
  helmet.noSniff = (headers, config) => {
110
- headers['X-Content-Type-Options'] = config.action
111
- return headers
112
- }
110
+ headers['X-Content-Type-Options'] = config.action;
111
+ return headers;
112
+ }; // https://github.com/helmetjs/referrer-policy
113
+
113
114
 
114
- // https://github.com/helmetjs/referrer-policy
115
115
  helmet.referrerPolicy = (headers, config) => {
116
- headers['Referrer-Policy'] = config.policy
117
- return headers
118
- }
116
+ headers['Referrer-Policy'] = config.policy;
117
+ return headers;
118
+ }; // https://github.com/helmetjs/crossdomain
119
+
119
120
 
120
- // https://github.com/helmetjs/crossdomain
121
121
  helmet.permittedCrossDomainPolicies = (headers, config) => {
122
- headers['X-Permitted-Cross-Domain-Policies'] = config.policy
123
- return headers
124
- }
122
+ headers['X-Permitted-Cross-Domain-Policies'] = config.policy;
123
+ return headers;
124
+ }; // https://github.com/helmetjs/x-xss-protection
125
+
125
126
 
126
- // https://github.com/helmetjs/x-xss-protection
127
127
  helmetHtmlOnly.xssFilter = (headers, config) => {
128
- let header = '1; mode=block'
128
+ let header = '1; mode=block';
129
+
129
130
  if (config.reportUri) {
130
- header += '; report=' + config.reportUri
131
+ header += '; report=' + config.reportUri;
131
132
  }
132
- headers['X-XSS-Protection'] = header
133
- return headers
134
- }
133
+
134
+ headers['X-XSS-Protection'] = header;
135
+ return headers;
136
+ };
135
137
 
136
138
  const httpSecurityHeadersMiddleware = (opts = {}) => {
137
- const options = { ...defaults, ...opts }
138
-
139
- const httpSecurityHeadersMiddlewareAfter = async (request) => {
140
- request.response = normalizeHttpResponse(request.response)
141
-
142
- Object.keys(helmet).forEach((key) => {
143
- const config = { ...defaults[key], ...options[key] }
144
- request.response.headers = helmet[key](request.response.headers, config)
145
- })
146
-
147
- if (request.response.headers?.['Content-Type']?.includes('text/html')) {
148
- Object.keys(helmetHtmlOnly).forEach((key) => {
149
- const config = { ...defaults[key], ...options[key] }
150
- request.response.headers = helmetHtmlOnly[key](
151
- request.response.headers,
152
- config
153
- )
154
- })
139
+ const options = { ...defaults,
140
+ ...opts
141
+ };
142
+
143
+ const httpSecurityHeadersMiddlewareAfter = async request => {
144
+ var _request$response$hea, _request$response$hea2;
145
+
146
+ request.response = normalizeHttpResponse(request.response);
147
+ Object.keys(helmet).forEach(key => {
148
+ const config = { ...defaults[key],
149
+ ...options[key]
150
+ };
151
+ request.response.headers = helmet[key](request.response.headers, config);
152
+ });
153
+
154
+ if ((_request$response$hea = request.response.headers) !== null && _request$response$hea !== void 0 && (_request$response$hea2 = _request$response$hea['Content-Type']) !== null && _request$response$hea2 !== void 0 && _request$response$hea2.includes('text/html')) {
155
+ Object.keys(helmetHtmlOnly).forEach(key => {
156
+ const config = { ...defaults[key],
157
+ ...options[key]
158
+ };
159
+ request.response.headers = helmetHtmlOnly[key](request.response.headers, config);
160
+ });
155
161
  }
156
- }
157
-
158
- const httpSecurityHeadersMiddlewareOnError = httpSecurityHeadersMiddlewareAfter
162
+ };
159
163
 
164
+ const httpSecurityHeadersMiddlewareOnError = httpSecurityHeadersMiddlewareAfter;
160
165
  return {
161
166
  after: httpSecurityHeadersMiddlewareAfter,
162
167
  onError: httpSecurityHeadersMiddlewareOnError
163
- }
164
- }
165
- module.exports = httpSecurityHeadersMiddleware
168
+ };
169
+ };
170
+
171
+ module.exports = httpSecurityHeadersMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-security-headers",
3
- "version": "2.5.6",
3
+ "version": "2.5.7",
4
4
  "description": "Applies best practice security headers to responses. It's a simplified port of HelmetJS",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -48,11 +48,11 @@
48
48
  "url": "https://github.com/middyjs/middy/issues"
49
49
  },
50
50
  "homepage": "https://github.com/middyjs/middy#readme",
51
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b",
51
+ "gitHead": "3983c4b138e1a4d7fcb3ed805d3b8832fff06fc1",
52
52
  "dependencies": {
53
- "@middy/util": "^2.5.6"
53
+ "@middy/util": "^2.5.7"
54
54
  },
55
55
  "devDependencies": {
56
- "@middy/core": "^2.5.6"
56
+ "@middy/core": "^2.5.7"
57
57
  }
58
58
  }