Skip to content

Commit 51204e1

Browse files
authored
Merge pull request #746 from chris--jones/allow-pem-passphrase
add passphrase option
2 parents 6a360ab + 7c510cf commit 51204e1

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ Then you need to run the server with `-S` for enabling SSL and `-C` for your cer
103103
http-server -S -C cert.pem
104104
```
105105

106+
If you wish to use a passphrase with your private key you can include one in the openssl command via the -passout parameter (using password of foobar)
107+
108+
109+
e.g.
110+
`openssl req -newkey rsa:2048 -passout pass:foobar -keyout key.pem -x509 -days 365 -out cert.pem`
111+
112+
For security reasons, the passphrase will only be read from the `NODE_HTTP_SERVER_SSL_PASSPHRASE` environment variable.
113+
114+
106115
This is what should be output if successful:
107116

108117
``` sh

bin/http-server

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ if (argv.h || argv.help) {
6868
var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
6969
host = argv.a || '0.0.0.0',
7070
tls = argv.S || argv.tls,
71+
sslPassphrase = process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE,
7172
proxy = argv.P || argv.proxy,
7273
proxyOptions = argv['proxy-options'],
7374
utc = argv.U || argv.utc,
@@ -175,7 +176,8 @@ function listen(port) {
175176
if (tls) {
176177
options.https = {
177178
cert: argv.C || argv.cert || 'cert.pem',
178-
key: argv.K || argv.key || 'key.pem'
179+
key: argv.K || argv.key || 'key.pem',
180+
passphrase: sslPassphrase,
179181
};
180182
try {
181183
fs.lstatSync(options.https.cert);

doc/http-server.1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ If not specified, uses cert.pem.
114114
.BI \-K ", " \-\-key " " [\fIFILE\fR]
115115
Path to SSL key file.
116116
If not specified, uses key.pem.
117+
Passphrase will be read from NODE_HTTP_SERVER_SSL_PASSPHRASE (if set)
117118

118119
.TP
119120
.BI \-r ", " \-\-robots " " [\fIUSER\-AGENT\fR]

lib/http-server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ function HttpServer(options) {
173173
serverOptions.https = options.https;
174174
}
175175

176-
this.server = union.createServer(serverOptions);
176+
this.server = serverOptions.https && serverOptions.https.passphrase
177+
// if passphrase is set, shim must be used as union does not support
178+
? require('./shims/https-server-shim')(serverOptions)
179+
: union.createServer(serverOptions);
180+
177181
if (options.timeout !== undefined) {
178182
this.server.setTimeout(options.timeout);
179183
}

lib/shims/https-server-shim.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable no-process-env */
2+
/* eslint-disable no-sync */
3+
var https = require('https');
4+
var fs = require('fs');
5+
var core = require('union/lib/core');
6+
var RoutingStream = require('union/lib/routing-stream');
7+
8+
module.exports = function (options) {
9+
var isArray = Array.isArray(options.after);
10+
var credentials;
11+
12+
if (!options) {
13+
throw new Error('options is required to create a server');
14+
}
15+
16+
function requestHandler(req, res) {
17+
var routingStream = new RoutingStream({
18+
before: options.before,
19+
buffer: options.buffer,
20+
after:
21+
isArray &&
22+
options.after.map(function (After) {
23+
return new After();
24+
}),
25+
request: req,
26+
response: res,
27+
limit: options.limit,
28+
headers: options.headers
29+
});
30+
31+
routingStream.on('error', function (err) {
32+
var fn = options.onError || core.errorHandler;
33+
fn(err, routingStream, routingStream.target, function () {
34+
routingStream.target.emit('next');
35+
});
36+
});
37+
38+
req.pipe(routingStream);
39+
}
40+
41+
var serverOptions;
42+
43+
serverOptions = options.https;
44+
if (!serverOptions.key || !serverOptions.cert) {
45+
throw new Error(
46+
'Both options key and cert are required.'
47+
);
48+
}
49+
50+
credentials = {
51+
key: fs.readFileSync(serverOptions.key),
52+
cert: fs.readFileSync(serverOptions.cert),
53+
passphrase: process.env.NODE_HTTP_SERVER_SSL_PASSPHRASE
54+
};
55+
56+
if (serverOptions.ca) {
57+
serverOptions.ca = !Array.isArray(serverOptions.ca)
58+
? [serverOptions.ca]
59+
: serverOptions.ca;
60+
61+
credentials.ca = serverOptions.ca.map(function (ca) {
62+
return fs.readFileSync(ca);
63+
});
64+
}
65+
66+
return https.createServer(credentials, requestHandler);
67+
};

0 commit comments

Comments
 (0)