Skip to content

Commit 61f7c77

Browse files
fix: normalize includeSubDomains option handling (#134)
1 parent 4db4eff commit 61f7c77

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ You can also set a few settings with the middleware to control the header:
4141
```js
4242
app.use(yes({
4343
maxAge: 86400, // defaults `86400`
44-
includeSubdomains: true, // defaults `true`
44+
includeSubDomains: true, // defaults `true`
4545
preload: true // defaults `true`
4646
}));
4747
```
4848

49+
`includeSubDomains` is the canonical option name. For backwards
50+
compatibility, `includeSubdomains` is also accepted, and both spellings
51+
default to `true`.
52+
4953
### Ignoring specific requests
5054

5155
In some cases, you may want to ignore a request and not force the redirect. You can use the `ignoreFilter` option to opt out of redirects on a case by case basis. This is useful if you want to ignore a specific route:

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function (options) {
55
options ||= {};
66
const maxAge = options.maxAge || 86_400;
77
const includeSubDomains =
8-
options.includeSubDomains === undefined ? true : options.includeSubdomains;
8+
options.includeSubDomains ?? options.includeSubdomains ?? true;
99

1010
return (request, response, next) => {
1111
let ignoreRequest = process.env.NODE_ENV !== 'production';

test/test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,58 @@ describe('yes', () => {
5454
});
5555
}).timeout(60_000);
5656

57+
it('should allow disabling includeSubDomains with camel case options', (done) => {
58+
const app = express();
59+
app.use(
60+
yes({
61+
includeSubDomains: false,
62+
}),
63+
);
64+
app.get('/test', (_request, response) => {
65+
response.sendStatus(200);
66+
});
67+
68+
const server = createSecureServer(app);
69+
request('https://localhost:8443')
70+
.get('/test')
71+
.expect('Strict-Transport-Security', 'max-age=86400')
72+
.expect(200)
73+
.end((error) => {
74+
if (error) {
75+
throw error;
76+
}
77+
78+
server.close();
79+
done();
80+
});
81+
}).timeout(60_000);
82+
83+
it('should allow disabling includeSubDomains with the legacy lowercase alias', (done) => {
84+
const app = express();
85+
app.use(
86+
yes({
87+
includeSubdomains: false,
88+
}),
89+
);
90+
app.get('/test', (_request, response) => {
91+
response.sendStatus(200);
92+
});
93+
94+
const server = createSecureServer(app);
95+
request('https://localhost:8443')
96+
.get('/test')
97+
.expect('Strict-Transport-Security', 'max-age=86400')
98+
.expect(200)
99+
.end((error) => {
100+
if (error) {
101+
throw error;
102+
}
103+
104+
server.close();
105+
done();
106+
});
107+
}).timeout(60_000);
108+
57109
it('should ignore filtered requests', (done) => {
58110
// Configure a minimal web server with the defaults
59111
const app = express();

0 commit comments

Comments
 (0)