Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow ca, key and cert will be string (regression) #1676

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/Server.js
Original file line number Diff line number Diff line change
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t this have utf8 encoding as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alan-agius4 hm, maybe, can you provide example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need
https://github.com/nodejs/node/blob/master/lib/fs.js#L392

buffer.toString :

encoding <string> The character encoding to use. Default: 'utf8'.

buf.toString([encoding[, start[, end]]])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So default is utf8

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for clarifying

} 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',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
}

Expand Down
44 changes: 41 additions & 3 deletions test/Https.test.js
Original file line number Diff line number Diff line change
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);
});