Skip to content

Commit af63e15

Browse files
committed
Make creation of known_hosts path more robust
1 parent 4c99761 commit af63e15

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/v1/internal/ch-node.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,21 @@ function userHome() {
3737
}
3838

3939
function mkFullPath(pathToCreate) {
40-
let joinedDirs = [];
41-
let pathToKnownHostsParts = pathToCreate.split(path.sep)
42-
pathToKnownHostsParts = pathToKnownHostsParts.map((part) => {
43-
if( !part.length ) return path.sep
44-
return part;
45-
});
46-
pathToKnownHostsParts.forEach((dir) => {
47-
joinedDirs.push(dir);
48-
let newPath = path.join.apply(null, joinedDirs)
49-
try {
50-
fs.accessSync( newPath );
40+
try {
41+
fs.mkdirSync( pathToCreate );
42+
} catch (e) {
43+
if(e.code === 'ENOENT') {
44+
// Create parent dir
45+
mkFullPath(path.dirname( pathToCreate ));
46+
// And now try again
47+
mkFullPath( pathToCreate );
5148
return;
5249
}
53-
catch (_) {
54-
try {
55-
fs.mkdirSync( newPath );
56-
} catch(e) {
57-
if ( e.code != 'EEXIST' ) throw e;
58-
}
50+
if (e.code === 'EEXIST') {
51+
return;
5952
}
60-
});
53+
throw e;
54+
}
6155
}
6256

6357
function loadFingerprint( serverId, knownHostsPath, cb ) {
@@ -86,8 +80,7 @@ function storeFingerprint(serverId, knownHostsPath, fingerprint) {
8680
try {
8781
fs.accessSync(knownHostsPath);
8882
} catch (_) {
89-
let pathWithoutFile = knownHostsPath.split(path.sep).slice(0, -1).join(path.sep);
90-
mkFullPath(pathWithoutFile);
83+
mkFullPath(path.dirname(knownHostsPath));
9184
}
9285
fs.appendFile(knownHostsPath, serverId + " " + fingerprint + EOL, "utf8", (err) => {
9386
if (err) {

test/internal/tls.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
var NodeChannel = require('../../lib/v1/internal/ch-node.js');
2020
var neo4j = require("../../lib/v1");
2121
var fs = require("fs");
22+
var path = require('path');
2223
var hasFeature = require("../../lib/v1/internal/features");
2324

2425
describe('trust-signed-certificates', function() {
@@ -85,8 +86,8 @@ describe('trust-on-first-use', function() {
8586

8687
// Given
8788
// Non existing directory
88-
var knownHostsDir = "build/hosts"
89-
var knownHostsPath = knownHostsDir + "/known_hosts";
89+
var knownHostsDir = path.join("build", "hosts");
90+
var knownHostsPath = path.join(knownHostsDir, "known_hosts");
9091
try {
9192
fs.unlinkSync(knownHostsPath);
9293
} catch (_) { }
@@ -106,6 +107,11 @@ describe('trust-on-first-use', function() {
106107
// And then the known_hosts file should have been created
107108
expect( function() { fs.accessSync(knownHostsPath) }).not.toThrow()
108109
done();
110+
}).catch( function(){
111+
// Just here to gracefully exit test on failure so we don't get timeouts
112+
// when done() isn't called.
113+
expect( 'this' ).toBe( 'to never happen' );
114+
done();
109115
});
110116
});
111117

0 commit comments

Comments
 (0)