Skip to content

Commit

Permalink
fix: allow ca, key and cert will be string (regression) (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Feb 21, 2019
1 parent df113eb commit b8d5c1e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
19 changes: 16 additions & 3 deletions lib/Server.js
Expand Up @@ -570,8 +570,21 @@ class Server {
const value = options.https[property];
const isBuffer = value instanceof Buffer;

if (value && !isBuffer && fs.lstatSync(value).isFile()) {
options.https[property] = fs.readFileSync(path.resolve(value));
if (value && !isBuffer) {
let stats = null;

try {
stats = fs.lstatSync(value).isFile();
} catch (error) {
// ignore error
}

if (stats) {
// It is file
options.https[property] = fs.readFileSync(path.resolve(value));
} else {
options.https[property] = value;
}
}
}

Expand Down Expand Up @@ -610,7 +623,7 @@ class Server {
const pems = createCertificate(attrs);

fs.writeFileSync(certPath, pems.private + pems.cert, {
encoding: 'utf-8',
encoding: 'utf8',
});
}

Expand Down
44 changes: 41 additions & 3 deletions test/Https.test.js
Expand Up @@ -18,9 +18,8 @@ const contentBasePublic = path.join(
describe('HTTPS', () => {
let server;
let req;
afterEach(helper.close);

describe('to directory', () => {
describe('is boolean', () => {
beforeAll((done) => {
server = helper.start(
config,
Expand All @@ -36,6 +35,10 @@ describe('HTTPS', () => {
it('Request to index', (done) => {
req.get('/').expect(200, /Heyo/, done);
});

afterAll(() => {
helper.close();
});
});

describe('ca, pfx, key and cert are buffer', () => {
Expand Down Expand Up @@ -68,7 +71,7 @@ describe('HTTPS', () => {
});
});

describe('ca, pfx, key and cert are string', () => {
describe('ca, pfx, key and cert are paths', () => {
beforeAll((done) => {
server = helper.start(
config,
Expand All @@ -91,4 +94,39 @@ describe('HTTPS', () => {
req.get('/').expect(200, /Heyo/, done);
});
});

describe('ca, pfx, key and cert are raw strings', () => {
beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
https: {
ca: fs
.readFileSync(path.join(httpsCertificateDirectory, 'ca.pem'))
.toString(),
// pfx can't be string because it is binary format
pfx: fs.readFileSync(
path.join(httpsCertificateDirectory, 'server.pfx')
),
key: fs
.readFileSync(path.join(httpsCertificateDirectory, 'server.key'))
.toString(),
cert: fs
.readFileSync(path.join(httpsCertificateDirectory, 'server.crt'))
.toString(),
passphrase: 'webpack-dev-server',
},
},
done
);
req = request(server.app);
});

it('Request to index', (done) => {
req.get('/').expect(200, /Heyo/, done);
});
});

afterEach(helper.close);
});

0 comments on commit b8d5c1e

Please sign in to comment.