Skip to content

Commit c5c9f7d

Browse files
committed
Fix cert path resolution with absolute paths
In our application, we're using `require.resolve` for getting the path of development environment certificates present in an npm module. This causes issues, at least in Windows, because `require.resolve` produces an absolute path, and `path.join` is creating two versions of the root path, for example `C:\C:\Path\To\cert.crt`. By using `path.resolve` instead, both relative and absolute paths are supported for certs.
1 parent 2f096ba commit c5c9f7d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ results
1313

1414
npm-debug.log
1515
node_modules
16+
17+
.idea

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ module.exports = function createServers(options, listening) {
146146
//
147147
// Load default SSL key, cert and ca(s).
148148
//
149-
key: fs.readFileSync(path.join(ssl.root, ssl.key)),
150-
cert: fs.readFileSync(path.join(ssl.root, ssl.cert)),
149+
key: fs.readFileSync(path.resolve(ssl.root, ssl.key)),
150+
cert: fs.readFileSync(path.resolve(ssl.root, ssl.cert)),
151151
ca: ssl.ca && ssl.ca.map(
152152
function (file) {
153-
return fs.readFileSync(path.join(ssl.root, file));
153+
return fs.readFileSync(path.resolve(ssl.root, file));
154154
}
155155
),
156156
//

test/create-servers-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ test('only https', function (t) {
5252
});
5353
});
5454

55+
test('absolute cert path resolution', function (t) {
56+
t.plan(3);
57+
createServers({
58+
log: console.log,
59+
https: {
60+
port: 3456,
61+
root: '/',
62+
cert: path.resolve(__dirname, 'fixtures', 'agent2-cert.pem'),
63+
key: path.resolve(__dirname, 'fixtures', 'agent2-key.pem')
64+
},
65+
handler: fend
66+
}, function (err, servers) {
67+
console.dir(err);
68+
t.error(err);
69+
t.equals(typeof servers, 'object');
70+
t.equals(typeof servers.https, 'object');
71+
servers.https.close();
72+
});
73+
});
74+
5575
test('http && https', function (t) {
5676
t.plan(4);
5777
createServers({

0 commit comments

Comments
 (0)