@middy/http-security-headers 2.5.2 → 3.0.0-alpha.1

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 (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/index.js +22 -48
  4. package/package.json +8 -7
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
3
+ Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -77,7 +77,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
77
77
 
78
78
  ## License
79
79
 
80
- Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
80
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
81
81
 
82
82
  <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
83
83
  <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
package/index.js CHANGED
@@ -1,14 +1,5 @@
1
- "use strict";
2
-
3
- const {
4
- normalizeHttpResponse
5
- } = require('@middy/util'); // Code and Defaults heavily based off https://helmetjs.github.io/
6
-
7
-
1
+ import { normalizeHttpResponse } from '@middy/util';
8
2
  const defaults = {
9
- // contentDisposition: {
10
- // filename: undefined
11
- // },
12
3
  dnsPrefetchControl: {
13
4
  allow: false
14
5
  },
@@ -35,8 +26,7 @@ const defaults = {
35
26
  action: 'nosniff'
36
27
  },
37
28
  permittedCrossDomainPolicies: {
38
- policy: 'none' // none, master-only, by-content-type, by-ftp-filename, all
39
-
29
+ policy: 'none'
40
30
  },
41
31
  referrerPolicy: {
42
32
  policy: 'no-referrer'
@@ -46,42 +36,28 @@ const defaults = {
46
36
  }
47
37
  };
48
38
  const helmet = {};
49
- const helmetHtmlOnly = {}; // OWASP ASVS 14.4.2
50
- // API Gateway strips out this header :(
51
- // helmet.content = (headers, config) => {
52
- // const filename = config.filename ?? `api.${headers?.['Content-Type'].split(/[/;]/)[1] ?? 'json'}`
53
- // headers['Content-Disposition'] = `attachment; filename="${filename}"`
54
- // }
55
- // contentSecurityPolicy - N/A - no HTML
56
- // featurePolicy - N/A - no HTML
57
- // crossdomain - N/A - For Adobe products
58
- // https://github.com/helmetjs/dns-Prefetch-control
39
+ const helmetHtmlOnly = {};
59
40
 
60
41
  helmet.dnsPrefetchControl = (headers, config) => {
61
42
  headers['X-DNS-Prefetch-Control'] = config.allow ? 'on' : 'off';
62
43
  return headers;
63
- }; // expectCt - in-progress spec
64
- // https://github.com/helmetjs/frameguard
65
-
44
+ };
66
45
 
67
46
  helmetHtmlOnly.frameguard = (headers, config) => {
68
47
  headers['X-Frame-Options'] = config.action.toUpperCase();
69
48
  return headers;
70
- }; // https://github.com/helmetjs/hide-powered-by
71
-
49
+ };
72
50
 
73
51
  helmet.hidePoweredBy = (headers, config) => {
74
52
  if (config.setTo) {
75
53
  headers['X-Powered-By'] = config.setTo;
76
54
  } else {
77
- Reflect.deleteProperty(headers, 'Server');
78
- Reflect.deleteProperty(headers, 'X-Powered-By');
55
+ delete headers.Server;
56
+ delete headers['X-Powered-By'];
79
57
  }
80
58
 
81
59
  return headers;
82
- }; // hpkp - deprecated
83
- // https://github.com/helmetjs/hsts
84
-
60
+ };
85
61
 
86
62
  helmet.hsts = (headers, config) => {
87
63
  let header = 'max-age=' + Math.round(config.maxAge);
@@ -96,33 +72,27 @@ helmet.hsts = (headers, config) => {
96
72
 
97
73
  headers['Strict-Transport-Security'] = header;
98
74
  return headers;
99
- }; // https://github.com/helmetjs/ienoopen
100
-
75
+ };
101
76
 
102
77
  helmet.ieNoOpen = (headers, config) => {
103
78
  headers['X-Download-Options'] = config.action;
104
79
  return headers;
105
- }; // noCache - N/A - separate middleware
106
- // https://github.com/helmetjs/dont-sniff-mimetype
107
-
80
+ };
108
81
 
109
82
  helmet.noSniff = (headers, config) => {
110
83
  headers['X-Content-Type-Options'] = config.action;
111
84
  return headers;
112
- }; // https://github.com/helmetjs/referrer-policy
113
-
85
+ };
114
86
 
115
87
  helmet.referrerPolicy = (headers, config) => {
116
88
  headers['Referrer-Policy'] = config.policy;
117
89
  return headers;
118
- }; // https://github.com/helmetjs/crossdomain
119
-
90
+ };
120
91
 
121
92
  helmet.permittedCrossDomainPolicies = (headers, config) => {
122
93
  headers['X-Permitted-Cross-Domain-Policies'] = config.policy;
123
94
  return headers;
124
- }; // https://github.com/helmetjs/x-xss-protection
125
-
95
+ };
126
96
 
127
97
  helmetHtmlOnly.xssFilter = (headers, config) => {
128
98
  let header = '1; mode=block';
@@ -141,9 +111,9 @@ const httpSecurityHeadersMiddleware = (opts = {}) => {
141
111
  };
142
112
 
143
113
  const httpSecurityHeadersMiddlewareAfter = async request => {
144
- var _request$response$hea, _request$response$hea2;
114
+ var _request$response$hea;
145
115
 
146
- request.response = normalizeHttpResponse(request.response);
116
+ normalizeHttpResponse(request);
147
117
  Object.keys(helmet).forEach(key => {
148
118
  const config = { ...defaults[key],
149
119
  ...options[key]
@@ -151,7 +121,7 @@ const httpSecurityHeadersMiddleware = (opts = {}) => {
151
121
  request.response.headers = helmet[key](request.response.headers, config);
152
122
  });
153
123
 
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')) {
124
+ if ((_request$response$hea = request.response.headers['Content-Type']) !== null && _request$response$hea !== void 0 && _request$response$hea.includes('text/html')) {
155
125
  Object.keys(helmetHtmlOnly).forEach(key => {
156
126
  const config = { ...defaults[key],
157
127
  ...options[key]
@@ -161,11 +131,15 @@ const httpSecurityHeadersMiddleware = (opts = {}) => {
161
131
  }
162
132
  };
163
133
 
164
- const httpSecurityHeadersMiddlewareOnError = httpSecurityHeadersMiddlewareAfter;
134
+ const httpSecurityHeadersMiddlewareOnError = async request => {
135
+ if (request.response === undefined) return;
136
+ return httpSecurityHeadersMiddlewareAfter(request);
137
+ };
138
+
165
139
  return {
166
140
  after: httpSecurityHeadersMiddlewareAfter,
167
141
  onError: httpSecurityHeadersMiddlewareOnError
168
142
  };
169
143
  };
170
144
 
171
- module.exports = httpSecurityHeadersMiddleware;
145
+ export default httpSecurityHeadersMiddleware;
package/package.json CHANGED
@@ -1,18 +1,19 @@
1
1
  {
2
2
  "name": "@middy/http-security-headers",
3
- "version": "2.5.2",
3
+ "version": "3.0.0-alpha.1",
4
4
  "description": "Applies best practice security headers to responses. It's a simplified port of HelmetJS",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
@@ -48,11 +49,11 @@
48
49
  "url": "https://github.com/middyjs/middy/issues"
49
50
  },
50
51
  "homepage": "https://github.com/middyjs/middy#readme",
51
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4",
52
+ "gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f",
52
53
  "dependencies": {
53
- "@middy/util": "^2.5.2"
54
+ "@middy/util": "^3.0.0-alpha.1"
54
55
  },
55
56
  "devDependencies": {
56
- "@middy/core": "^2.5.2"
57
+ "@middy/core": "^3.0.0-alpha.1"
57
58
  }
58
59
  }