Skip to content

Commit 9a7693c

Browse files
authored
Merge pull request #942 from webpack/ssl-path
Ssl path
2 parents 662bc31 + 25e1098 commit 9a7693c

File tree

6 files changed

+69
-60
lines changed

6 files changed

+69
-60
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
/client/index.bundle.js
44
/client/sockjs.bundle.js
55
/coverage
6+
*.pem

examples/https/README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
# https
22

3+
By default webpack-dev-server will generate a self-signed, 2048 bit, sha256 SSL
4+
Certificate, which is used to enable https. The certificate will be located in the
5+
`ssl` directory afte the server is started for the first time. The generated
6+
certificate is only good for 30 days, at which point it'll be regenerated.
7+
8+
We highly recommend creating and managing your own certificates. Please see the
9+
following resources for doing so:
10+
11+
* (MacOS) https://certsimple.com/blog/localhost-ssl-fix
12+
* (Windows) https://technet.microsoft.com/itpro/powershell/windows/pkiclient/new-selfsignedcertificate
13+
14+
## Getting Started
15+
316
```shell
417
node ../../bin/webpack-dev-server.js --open --https
518
```
619

7-
A fake certificate is used to enable https.
20+
## Using Your Certificate
21+
22+
Options are available for using your own SSL Certificate in your preferred or
23+
OS-required format.
824

9-
You can provide the following SSL options to override the fake certificate:
25+
Given the base command `node ../../bin/webpack-dev-server.js --open --https`, append
26+
one of the following:
1027

11-
* Certificate options e.g. `node ../../bin/webpack-dev-server.js --open --https --cert=../../ssl/server.pem --key=../../ssl/server.pem`
12-
* PFX and Passphrase e.g. `node ../../bin/webpack-dev-server.js --open --https --pfx=./test_cert.pfx --pfx-passphrase=sample`
28+
* (PEM Files) `--cert=../../ssl/server.pem --key=../../ssl/server.pem`
29+
* (PFX and Passphrase) `--pfx=./test_cert.pfx --pfx-passphrase=sample`
1330

14-
## What should happen
31+
## What To Expect
1532

16-
The script should open `https://localhost:8080/`. Your browser will probably give you a warning about using an invalid certificate. After ignoring this warning, you should see "It's working."
33+
The script should open `https://localhost:8080/`in your default browser. If your
34+
browser displays a warning about a non-trusted certificate, follow the procedure
35+
for your browser of choice to continue. After doing so you should see "It's Working"
36+
displayed on the page.

lib/Server.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
"use strict";
22

3-
const fs = require("fs");
43
const chokidar = require("chokidar");
5-
const path = require("path");
6-
const webpackDevMiddleware = require("webpack-dev-middleware");
7-
const express = require("express");
84
const compress = require("compression");
9-
const sockjs = require("sockjs");
5+
const del = require("del");
6+
const express = require("express");
7+
const fs = require("fs");
108
const http = require("http");
11-
const spdy = require("spdy");
129
const httpProxyMiddleware = require("http-proxy-middleware");
1310
const serveIndex = require("serve-index");
1411
const historyApiFallback = require("connect-history-api-fallback");
12+
const path = require("path");
13+
const selfsigned = require("selfsigned");
14+
const sockjs = require("sockjs");
15+
const spdy = require("spdy");
1516
const webpack = require("webpack");
17+
const webpackDevMiddleware = require("webpack-dev-middleware");
18+
1619
const OptionsValidationError = require("./OptionsValidationError");
1720
const optionsSchema = require("./optionsSchema.json");
1821

@@ -360,8 +363,37 @@ function Server(compiler, options) {
360363
};
361364
}
362365

363-
// Use built-in self-signed certificate if no certificate was configured
364-
const fakeCert = fs.readFileSync(path.join(__dirname, "../ssl/server.pem"));
366+
// Use a self-signed certificate if no certificate was configured.
367+
// Cycle certs every 24 hours
368+
const certPath = path.join(__dirname, "../ssl/server.pem");
369+
let certExists = fs.existsSync(certPath);
370+
371+
if(certExists) {
372+
const certStat = fs.statSync(certPath);
373+
const certTtl = 1000 * 60 * 60 * 24;
374+
const now = new Date();
375+
376+
// cert is more than 30 days old, kill it with fire
377+
if((now - certStat.ctime) / certTtl > 30) {
378+
console.log("SSL Certificate is more than 30 days old. Removing.");
379+
del.sync([certPath], { force: true });
380+
certExists = false;
381+
}
382+
}
383+
384+
if(!certExists) {
385+
console.log("Generating SSL Certificate");
386+
const attrs = [{ name: "commonName", value: "localhost" }];
387+
const pems = selfsigned.generate(attrs, {
388+
algorithm: "sha256",
389+
days: 30,
390+
keySize: 2048
391+
});
392+
393+
fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" });
394+
}
395+
396+
const fakeCert = fs.readFileSync(certPath);
365397
options.https.key = options.https.key || fakeCert;
366398
options.https.cert = options.https.cert || fakeCert;
367399

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"chokidar": "^1.6.0",
1212
"compression": "^1.5.2",
1313
"connect-history-api-fallback": "^1.3.0",
14+
"del": "^3.0.0",
1415
"express": "^4.13.3",
1516
"html-entities": "^1.2.0",
1617
"http-proxy-middleware": "~0.17.4",
1718
"opn": "4.0.2",
1819
"portfinder": "^1.0.9",
20+
"selfsigned": "^1.9.1",
1921
"serve-index": "^1.7.2",
2022
"sockjs": "0.3.18",
2123
"sockjs-client": "1.1.2",

ssl/.gitkeep

Whitespace-only changes.

ssl/server.pem

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)