Skip to content

Commit 124047f

Browse files
author
Stefan-Gabriel Muscalu
committed
Fix: if there are duplicate hosts in the known_hosts file, we return on the first one and not call the callback on loadFingerprint more than once
1 parent 9349d9e commit 124047f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/v1/internal/ch-node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
4545
require('readline').createInterface({
4646
input: fs.createReadStream(knownHostsPath)
4747
}).on('line', (line) => {
48-
if( line.startsWith( serverId )) {
48+
if( !found && line.startsWith( serverId )) {
4949
found = true;
5050
cb( line.split(" ")[1] );
5151
}

test/internal/tls.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ describe('trust-on-first-use', function() {
7676

7777
var driver;
7878

79+
it('should not throw an error if the host file contains two host duplicates', function(done) {
80+
'use strict';
81+
// Assuming we only run this test on NodeJS with TOFU support
82+
if( !hasFeature("trust_on_first_use") ) {
83+
done();
84+
return;
85+
}
86+
87+
// Given
88+
var knownHostsPath = "build/known_hosts";
89+
if( fs.existsSync(knownHostsPath) ) {
90+
fs.unlinkSync(knownHostsPath);
91+
}
92+
93+
fs.writeFileSync(
94+
knownHostsPath,
95+
'localhost:7687 abcd\n' +
96+
'localhost:7687 abcd'
97+
);
98+
99+
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {
100+
encrypted: true,
101+
trust: "TRUST_ON_FIRST_USE",
102+
knownHosts: knownHostsPath
103+
});
104+
105+
// When
106+
driver.session().run("RETURN true AS a").then( function(data) {
107+
// Then we get to here.
108+
// And then the known_hosts file should have correct contents
109+
expect( data.records[0].get('a') ).toBe( true );
110+
done();
111+
});
112+
});
113+
79114
it('should accept previously un-seen hosts', function(done) {
80115
// Assuming we only run this test on NodeJS with TOFU support
81116
if( !hasFeature("trust_on_first_use") ) {

0 commit comments

Comments
 (0)