yes-https 3.0.1 → 4.0.3

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.
package/test/test.js CHANGED
@@ -1,91 +1,244 @@
1
- import https from 'node:https';
2
1
  import fs from 'node:fs';
3
- import process from 'node:process';
2
+ import https from 'node:https';
3
+ import { describe, it } from 'node:test';
4
4
  import express from 'express';
5
- import {describe, it} from 'mocha';
6
5
  import request from 'supertest';
7
6
  import yes from '../lib/index.js';
8
7
 
9
- process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
8
+ const TEST_SERVER_CERT = fs.readFileSync('./test/certs/server.crt');
10
9
 
11
10
  describe('yes', () => {
12
- it('should perform the 301 for an http request', done => {
13
- // Configure a minimal web server with the defaults
14
- const app = express();
15
- app.use(yes());
16
- app.get('/test', (request_, response) => {
17
- response.sendStatus(200);
18
- });
19
-
20
- // Verify the request returns a 301
21
- request(app)
22
- .get('/test')
23
- .expect(301)
24
- .end(error => {
25
- if (error) {
26
- throw error;
27
- }
28
-
29
- done();
30
- });
31
- });
32
-
33
- it('should use the correct defaults', done => {
34
- // Configure a minimal web server with the defaults
35
- const app = express();
36
- app.use(yes());
37
- app.get('/test', (_request, response) => {
38
- response.sendStatus(200);
39
- });
40
-
41
- // Verify the request returns the right header when using https
42
- const server = createSecureServer(app);
43
- request('https://localhost:8443')
44
- .get('/test')
45
- .expect('Strict-Transport-Security', 'max-age=86400; includeSubDomains')
46
- .expect(200)
47
- .end(error => {
48
- if (error) {
49
- throw error;
50
- }
51
-
52
- server.close();
53
- done();
54
- });
55
- }).timeout(60_000);
56
-
57
- it('should ignore filtered requests', done => {
58
- // Configure a minimal web server with the defaults
59
- const app = express();
60
- app.use(yes({
61
- ignoreFilter: request_ => (request_.url.includes('/_ah/health')),
62
- }));
63
-
64
- app.get('/_ah/health', (_request, response) => {
65
- response.sendStatus(200);
66
- });
67
-
68
- // Verify the request returns a 200 for health checks
69
- request(app)
70
- .get('/_ah/health')
71
- .expect(200)
72
- .end(error => {
73
- if (error) {
74
- throw error;
75
- }
76
-
77
- done();
78
- });
79
- });
11
+ it('should perform the 301 for an http request', async () => {
12
+ // Configure a minimal web server with the defaults
13
+ const app = express();
14
+ app.use(yes());
15
+ app.get('/test', (_request_, response) => {
16
+ response.sendStatus(200);
17
+ });
18
+
19
+ // Verify the request returns a 301
20
+ await request(app).get('/test').expect(301);
21
+ });
22
+
23
+ it('should use the correct defaults', { timeout: 60_000 }, async () => {
24
+ // Configure a minimal web server with the defaults
25
+ const app = express();
26
+ app.use(yes());
27
+ app.get('/test', (_request, response) => {
28
+ response.sendStatus(200);
29
+ });
30
+
31
+ // Verify the request returns the right header when using https
32
+ const server = createSecureServer(app);
33
+ await request(server)
34
+ .get('/test')
35
+ .ca(TEST_SERVER_CERT)
36
+ .expect('Strict-Transport-Security', 'max-age=86400; includeSubDomains')
37
+ .expect(200);
38
+ });
39
+
40
+ it('should allow disabling includeSubDomains with camel case options', {
41
+ timeout: 60_000,
42
+ }, async () => {
43
+ const app = express();
44
+ app.use(
45
+ yes({
46
+ includeSubDomains: false,
47
+ }),
48
+ );
49
+ app.get('/test', (_request, response) => {
50
+ response.sendStatus(200);
51
+ });
52
+
53
+ const server = createSecureServer(app);
54
+ await request(server)
55
+ .get('/test')
56
+ .ca(TEST_SERVER_CERT)
57
+ .expect('Strict-Transport-Security', 'max-age=86400')
58
+ .expect(200);
59
+ });
60
+
61
+ it('should allow disabling includeSubDomains with the legacy lowercase alias', {
62
+ timeout: 60_000,
63
+ }, async () => {
64
+ const app = express();
65
+ app.use(
66
+ yes({
67
+ includeSubdomains: false,
68
+ }),
69
+ );
70
+ app.get('/test', (_request, response) => {
71
+ response.sendStatus(200);
72
+ });
73
+
74
+ const server = createSecureServer(app);
75
+ await request(server)
76
+ .get('/test')
77
+ .ca(TEST_SERVER_CERT)
78
+ .expect('Strict-Transport-Security', 'max-age=86400')
79
+ .expect(200);
80
+ });
81
+
82
+ it('should ignore filtered requests', async () => {
83
+ // Configure a minimal web server with the defaults
84
+ const app = express();
85
+ app.use(
86
+ yes({
87
+ ignoreFilter: (request_) => request_.url.includes('/_ah/health'),
88
+ }),
89
+ );
90
+
91
+ app.get('/_ah/health', (_request, response) => {
92
+ response.sendStatus(200);
93
+ });
94
+
95
+ // Verify the request returns a 200 for health checks
96
+ await request(app).get('/_ah/health').expect(200);
97
+ });
98
+
99
+ it('should include preload when configured', {
100
+ timeout: 60_000,
101
+ }, async () => {
102
+ const app = express();
103
+ app.use(yes({ preload: true }));
104
+ app.get('/test', (_request, response) => {
105
+ response.sendStatus(200);
106
+ });
107
+
108
+ const server = createSecureServer(app);
109
+ await request(server)
110
+ .get('/test')
111
+ .ca(TEST_SERVER_CERT)
112
+ .expect(
113
+ 'Strict-Transport-Security',
114
+ 'max-age=86400; includeSubDomains; preload',
115
+ )
116
+ .expect(200);
117
+ });
118
+
119
+ it('should omit includeSubDomains when disabled', {
120
+ timeout: 60_000,
121
+ }, async () => {
122
+ const app = express();
123
+ app.use(yes({ includeSubDomains: false }));
124
+ app.get('/test', (_request, response) => {
125
+ response.sendStatus(200);
126
+ });
127
+
128
+ const server = createSecureServer(app);
129
+ await request(server)
130
+ .get('/test')
131
+ .ca(TEST_SERVER_CERT)
132
+ .expect('Strict-Transport-Security', 'max-age=86400')
133
+ .expect(200);
134
+ });
135
+
136
+ it('should include includeSubDomains when explicitly enabled', {
137
+ timeout: 60_000,
138
+ }, async () => {
139
+ const app = express();
140
+ app.use(yes({ includeSubDomains: true }));
141
+ app.get('/test', (_request, response) => {
142
+ response.sendStatus(200);
143
+ });
144
+
145
+ const server = createSecureServer(app);
146
+ await request(server)
147
+ .get('/test')
148
+ .ca(TEST_SERVER_CERT)
149
+ .expect('Strict-Transport-Security', 'max-age=86400; includeSubDomains')
150
+ .expect(200);
151
+ });
152
+
153
+ describe('includeSubDomains', () => {
154
+ it('should include the directive by default over a secure connection', () => {
155
+ return expectSecureHeader({}, 'max-age=86400; includeSubDomains');
156
+ });
157
+
158
+ it('should include the directive when explicitly enabled over a secure connection', () => {
159
+ return expectSecureHeader(
160
+ { includeSubDomains: true },
161
+ 'max-age=86400; includeSubDomains',
162
+ );
163
+ });
164
+
165
+ it('should omit the directive when disabled over a secure connection', () => {
166
+ return expectSecureHeader({ includeSubDomains: false }, 'max-age=86400');
167
+ });
168
+
169
+ it('should compose correctly with preload and maxAge when enabled', () => {
170
+ return expectSecureHeader(
171
+ { includeSubDomains: true, preload: true, maxAge: 31_536_000 },
172
+ 'max-age=31536000; includeSubDomains; preload',
173
+ );
174
+ });
175
+
176
+ it('should compose correctly with preload and maxAge when disabled', () => {
177
+ return expectSecureHeader(
178
+ { includeSubDomains: false, preload: true, maxAge: 31_536_000 },
179
+ 'max-age=31536000; preload',
180
+ );
181
+ });
182
+
183
+ it('should include the directive by default for forwarded https requests', () => {
184
+ return expectForwardedSecureHeader(
185
+ {},
186
+ 'max-age=86400; includeSubDomains',
187
+ );
188
+ });
189
+
190
+ it('should honor an explicit true value for forwarded https requests', () => {
191
+ return expectForwardedSecureHeader(
192
+ { includeSubDomains: true },
193
+ 'max-age=86400; includeSubDomains',
194
+ );
195
+ });
196
+
197
+ it('should honor an explicit false value for forwarded https requests', () => {
198
+ return expectForwardedSecureHeader(
199
+ { includeSubDomains: false },
200
+ 'max-age=86400',
201
+ );
202
+ });
203
+ });
80
204
  });
81
205
 
82
206
  function createSecureServer(app) {
83
- // Server the app over https
84
- return https.createServer({
85
- key: fs.readFileSync('./test/certs/server.key'),
86
- cert: fs.readFileSync('./test/certs/server.crt'),
87
- ca: fs.readFileSync('./test/certs/ca.crt'),
88
- requestCert: true,
89
- rejectUnauthorized: false,
90
- }, app).listen('8443');
207
+ return https.createServer(
208
+ {
209
+ key: fs.readFileSync('./test/certs/server.key'),
210
+ cert: fs.readFileSync('./test/certs/server.crt'),
211
+ },
212
+ app,
213
+ );
214
+ }
215
+
216
+ function expectSecureHeader(options, expectedHeader) {
217
+ const app = express();
218
+ app.use(yes(options));
219
+ app.get('/test', (_request, response) => {
220
+ response.sendStatus(200);
221
+ });
222
+
223
+ const server = createSecureServer(app);
224
+ return request(server)
225
+ .get('/test')
226
+ .ca(TEST_SERVER_CERT)
227
+ .expect('Strict-Transport-Security', expectedHeader)
228
+ .expect(200);
229
+ }
230
+
231
+ function expectForwardedSecureHeader(options, expectedHeader) {
232
+ const app = express();
233
+ app.use(yes(options));
234
+ app.get('/test', (_request, response) => {
235
+ response.sendStatus(200);
236
+ });
237
+
238
+ return request(app)
239
+ .get('/test')
240
+ .set('X-Forwarded-Proto', 'https')
241
+ .set('Host', 'example.com')
242
+ .expect('Strict-Transport-Security', expectedHeader)
243
+ .expect(200);
91
244
  }
package/.releaserc.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "branches": ["main"]
3
- }