Skip to content

Fix: regarding PR #110 edge cases #113

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

Merged
merged 3 commits into from
Aug 4, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/v1/internal/ch-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,27 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
});
}

function storeFingerprint(serverId, knownHostsPath, fingerprint) {
const _lockFingerprintFromAppending = {};
function storeFingerprint( serverId, knownHostsPath, fingerprint, cb ) {
// we check if the serverId has been appended
if(!!_lockFingerprintFromAppending[serverId]){
// if it has, we ignore it
return cb(null);
}

// If file doesn't exist, create full path to it
try {
fs.accessSync(knownHostsPath);
} catch (_) {
mkFullPath(path.dirname(knownHostsPath));
}

fs.appendFile(knownHostsPath, serverId + " " + fingerprint + EOL, "utf8", (err) => {
delete _lockFingerprintFromAppending[serverId];
if (err) {
console.log(err);
}
return cb(err);
});
}

Expand Down Expand Up @@ -152,8 +162,12 @@ const TrustStrategy = {
if( knownFingerprint === serverFingerprint ) {
onSuccess();
} else if( knownFingerprint == null ) {
storeFingerprint( serverId, knownHostsPath, serverFingerprint );
onSuccess();
storeFingerprint( serverId, knownHostsPath, serverFingerprint, (err) => {
if (err) {
return onFailure(err);
}
return onSuccess();
});
} else {
onFailure(newError("Database encryption certificate has changed, and no longer " +
"matches the certificate stored for " + serverId + " in `" + knownHostsPath +
Expand Down
54 changes: 54 additions & 0 deletions test/internal/tls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,60 @@ describe('trust-on-first-use', function() {
done();
});
});

it('should not duplicate fingerprint entries', function(done) {
// 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);
}
fs.writeFileSync(knownHostsPath, '');

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

// When
driver.session();
driver.session();

// Then
setTimeout(function() {
var lines = {};
fs.readFileSync(knownHostsPath, 'utf8')
.split('\n')
.filter(function(line) {
return !! (line.trim());
})
.forEach(function(line) {
if (!lines[line]) {
lines[line] = 0;
}
lines[line]++;
});

var duplicatedLines = Object
.keys(lines)
.map(function(line) {
return lines[line];
})
.filter(function(count) {
return count > 1;
})
.length;

expect( duplicatedLines ).toBe( 0 );
done();
}, 1000);
});

it('should should give helpful error if database cert does not match stored certificate', function(done) {
// Assuming we only run this test on NodeJS with TOFU support
Expand Down