@middy/http-security-headers 3.0.0-alpha.0 → 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 (2) hide show
  1. package/index.js +145 -0
  2. package/package.json +5 -4
package/index.js ADDED
@@ -0,0 +1,145 @@
1
+ import { normalizeHttpResponse } from '@middy/util';
2
+ const defaults = {
3
+ dnsPrefetchControl: {
4
+ allow: false
5
+ },
6
+ expectCT: {
7
+ enforce: true,
8
+ maxAge: 30,
9
+ reportUri: ''
10
+ },
11
+ frameguard: {
12
+ action: 'deny'
13
+ },
14
+ hidePoweredBy: {
15
+ setTo: null
16
+ },
17
+ hsts: {
18
+ maxAge: 180 * 24 * 60 * 60,
19
+ includeSubDomains: true,
20
+ preload: true
21
+ },
22
+ ieNoOpen: {
23
+ action: 'noopen'
24
+ },
25
+ noSniff: {
26
+ action: 'nosniff'
27
+ },
28
+ permittedCrossDomainPolicies: {
29
+ policy: 'none'
30
+ },
31
+ referrerPolicy: {
32
+ policy: 'no-referrer'
33
+ },
34
+ xssFilter: {
35
+ reportUri: ''
36
+ }
37
+ };
38
+ const helmet = {};
39
+ const helmetHtmlOnly = {};
40
+
41
+ helmet.dnsPrefetchControl = (headers, config) => {
42
+ headers['X-DNS-Prefetch-Control'] = config.allow ? 'on' : 'off';
43
+ return headers;
44
+ };
45
+
46
+ helmetHtmlOnly.frameguard = (headers, config) => {
47
+ headers['X-Frame-Options'] = config.action.toUpperCase();
48
+ return headers;
49
+ };
50
+
51
+ helmet.hidePoweredBy = (headers, config) => {
52
+ if (config.setTo) {
53
+ headers['X-Powered-By'] = config.setTo;
54
+ } else {
55
+ delete headers.Server;
56
+ delete headers['X-Powered-By'];
57
+ }
58
+
59
+ return headers;
60
+ };
61
+
62
+ helmet.hsts = (headers, config) => {
63
+ let header = 'max-age=' + Math.round(config.maxAge);
64
+
65
+ if (config.includeSubDomains) {
66
+ header += '; includeSubDomains';
67
+ }
68
+
69
+ if (config.preload) {
70
+ header += '; preload';
71
+ }
72
+
73
+ headers['Strict-Transport-Security'] = header;
74
+ return headers;
75
+ };
76
+
77
+ helmet.ieNoOpen = (headers, config) => {
78
+ headers['X-Download-Options'] = config.action;
79
+ return headers;
80
+ };
81
+
82
+ helmet.noSniff = (headers, config) => {
83
+ headers['X-Content-Type-Options'] = config.action;
84
+ return headers;
85
+ };
86
+
87
+ helmet.referrerPolicy = (headers, config) => {
88
+ headers['Referrer-Policy'] = config.policy;
89
+ return headers;
90
+ };
91
+
92
+ helmet.permittedCrossDomainPolicies = (headers, config) => {
93
+ headers['X-Permitted-Cross-Domain-Policies'] = config.policy;
94
+ return headers;
95
+ };
96
+
97
+ helmetHtmlOnly.xssFilter = (headers, config) => {
98
+ let header = '1; mode=block';
99
+
100
+ if (config.reportUri) {
101
+ header += '; report=' + config.reportUri;
102
+ }
103
+
104
+ headers['X-XSS-Protection'] = header;
105
+ return headers;
106
+ };
107
+
108
+ const httpSecurityHeadersMiddleware = (opts = {}) => {
109
+ const options = { ...defaults,
110
+ ...opts
111
+ };
112
+
113
+ const httpSecurityHeadersMiddlewareAfter = async request => {
114
+ var _request$response$hea;
115
+
116
+ normalizeHttpResponse(request);
117
+ Object.keys(helmet).forEach(key => {
118
+ const config = { ...defaults[key],
119
+ ...options[key]
120
+ };
121
+ request.response.headers = helmet[key](request.response.headers, config);
122
+ });
123
+
124
+ if ((_request$response$hea = request.response.headers['Content-Type']) !== null && _request$response$hea !== void 0 && _request$response$hea.includes('text/html')) {
125
+ Object.keys(helmetHtmlOnly).forEach(key => {
126
+ const config = { ...defaults[key],
127
+ ...options[key]
128
+ };
129
+ request.response.headers = helmetHtmlOnly[key](request.response.headers, config);
130
+ });
131
+ }
132
+ };
133
+
134
+ const httpSecurityHeadersMiddlewareOnError = async request => {
135
+ if (request.response === undefined) return;
136
+ return httpSecurityHeadersMiddlewareAfter(request);
137
+ };
138
+
139
+ return {
140
+ after: httpSecurityHeadersMiddlewareAfter,
141
+ onError: httpSecurityHeadersMiddlewareOnError
142
+ };
143
+ };
144
+
145
+ export default httpSecurityHeadersMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-security-headers",
3
- "version": "3.0.0-alpha.0",
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
5
  "type": "module",
6
6
  "engines": {
@@ -13,6 +13,7 @@
13
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": "c533f62841c8a39d061d7b94f30ba178f002c8db",
52
+ "gitHead": "a14125c6b2e21b181824f9985a919a47f1e4711f",
52
53
  "dependencies": {
53
- "@middy/util": "^3.0.0-alpha.0"
54
+ "@middy/util": "^3.0.0-alpha.1"
54
55
  },
55
56
  "devDependencies": {
56
- "@middy/core": "^3.0.0-alpha.0"
57
+ "@middy/core": "^3.0.0-alpha.1"
57
58
  }
58
59
  }