Skip to content

Make known-host file to tolerate duplicated serverId #111

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/v1/internal/ch-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
require('readline').createInterface({
input: fs.createReadStream(knownHostsPath)
}).on('line', (line) => {
if( line.startsWith( serverId )) {
if( !found && line.startsWith( serverId )) {
found = true;
cb( line.split(" ")[1] );
}
Expand Down
36 changes: 36 additions & 0 deletions test/internal/tls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,42 @@ describe('trust-on-first-use', function() {

var driver;

it('should not throw an error if the host file contains two host duplicates', function(done) {
'use strict';
// Assuming we only run this test on NodeJS with TOFU support
if( !hasFeature("trust_on_first_use") ) {
done();
return;
}

// Given
var knownHostsPath = "build/known_hosts";
if( fs.existsSync(knownHostsPath) ) {
fs.unlinkSync(knownHostsPath);
}

driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"), {
encrypted: true,
trust: "TRUST_ON_FIRST_USE",
knownHosts: knownHostsPath
});

driver.session(); // write into the knownHost file

// duplicate the same serverId twice
setTimeout(function() {
var text = fs.readFileSync(knownHostsPath,'utf8');
fs.writeFileSync(knownHostsPath, text+text);
}, 1000);

// When
driver.session().run("RETURN true AS a").then( function(data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be encapsulated in a setTimeout(2000) in order to wait for the above one to duplicate the file data

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test passed on test server. So maybe the timeout is okay? Though currently we just tested on linux, our windows test server might require longer to see the change. Currently the test failed at a known tck failing test :D I am trying to fix it now so get this PR ready.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but since driver.session().run() will fire off the queue at an arbitrary time, you cannot know if the file has been modified to have duplicated lines yet.

// Then we get to here.
expect( data.records[0].get('a') ).toBe( true );
done();
});
});

it('should accept previously un-seen hosts', function(done) {
// Assuming we only run this test on NodeJS with TOFU support
if( !hasFeature("trust_on_first_use") ) {
Expand Down