-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
180 lines (142 loc) · 3.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
This module exposes factory function to construct middleware
This factory function accepts options has to configure behaviour
of middleware.
The most important part is `resolver` which is a function
`boolean resolver(ctx : Ctx)` -- Java
`resolver :: Ctx -> Bool` -- Haskell
This function is being called with ctx object of request
- If `true` is returned connection is considered secure
- If `false` is returned connection is considered not being secured
*/
const url = require('url');
/**
* Default configuration
*/
const defaults = {
resolver: httpsResolver,
hostname: undefined,
port: 443,
skipDefaultPort: true,
ignoreUrl: false,
temporary: false,
redirectMethods: ['GET', 'HEAD'],
disallowStatus: 405
};
// Merge default options with overwrites and returns new object
function applyOptions(options) {
const settings = {};
options = options || {};
Object.assign(settings, defaults, options);
return settings;
}
// skip 443 ports in urls
function portToUrlString(options) {
return (options.skipDefaultPort && options.port === 443) ? '' : ':' + options.port;
}
// middleware itself
function redirect(options, ctx) {
// Check if method should be disallowed
if (options.redirectMethods.indexOf(ctx.method) === -1) {
ctx.response.status = options.disallowStatus;
if (options.disallowStatus === 405) {
ctx.response.set('Allow', options.redirectMethods.join(', '));
}
ctx.response.body = '';
return;
}
const currentHostname = url.parse('http://' + ctx.request.header.host).hostname;
// build redirect url
const httpsHost = (typeof options.hostname === 'function' && options.hostname(currentHostname))
|| currentHostname;
let redirectTo = `https://${httpsHost}${portToUrlString(options)}`;
if(!options.ignoreUrl) {
redirectTo += ctx.request.url;
}
// redirect to secure
ctx.response.status = options.temporary ? 307 : 301;
ctx.response.redirect(redirectTo);
}
/**
* enforceHTTPS
*
* @param {Hash} options
* @param {Function} options[resolver]
* @param {Integer} options[port]
* @param {Function} options[hostname]
* @param {Boolean} options[ignoreUrl]
* @param {Boolean} options[temporary]
* @param {Array} options[redirectMethods]
* @param {Integer} options[disallowStatus]
* @return {Function}
* @api public
*/
function factory(options) {
options = applyOptions(options);
return (ctx, next) => {
// Next if secure
if (options.resolver(ctx)) {
return next();
}
// Redirect to HTTPS
else {
redirect(options, ctx);
}
}
};
/*
Resolvers
*/
// Default HTTPS resolver
// This works when using node.js TLS support
function httpsResolver(ctx) {
return ctx.secure;
}
// x-forwarded-proto header resolver
// common for heroku gcp (ingress) etc
function xForwardedProtoResolver(ctx) {
return ctx.request.header['x-forwarded-proto'] === 'https';
}
// Azure resolver
// Azure is using `x-att-ssl` header
function azureResolver(ctx) {
return Boolean(ctx.request.header["x-arr-ssl"]);
}
// Custom proto header factory
function customProtoHeaderResolver(header) {
return (ctx) => {
return ctx.request.header[header] === 'https';
}
}
// parse Forwarded header
function parseForwarded(value) {
const forwarded = {}
value.trim().split(';').forEach((part) => {
const pair = part.trim().split('=');
forwarded[pair[0]] = pair[1];
});
return forwarded;
}
// Resolver for `Forwarded` header
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
function forwardedResolver(ctx) {
const header = ctx.request.header['forwarded'];
if (!header) {
return false;
} else {
const forwarded = parseForwarded(header);
return forwarded.proto === 'https';
}
}
/*
Exports
*/
module.exports = {
__esModule: true,
default: factory,
httpsResolver,
xForwardedProtoResolver,
azureResolver,
customProtoHeaderResolver,
forwardedResolver
};