Skip to content

known_host host identity fingerprint duplication  #108

Closed
@legraphista

Description

@legraphista

Referring to issue #107
I have looked through the code and found that the only way that this error can happen

/app/node_modules/neo4j-driver/lib/v1/internal/ch-node.js:214
      for (var i = 0; i < pending.length; i++) {
                                 ^

TypeError: Cannot read property 'length' of null
    at /app/node_modules/neo4j-driver/lib/v1/internal/ch-node.js:214:34
    ...

is if the trust certificate callback is called twice:

    this._handleConnectionError = this._handleConnectionError.bind(this);

-> this._conn = connect(opts, function () {
      if (!self._open) {
        return;
      }

     <...>
      var pending = self._pending;
      self._pending = null;
      for (var i = 0; i < pending.length; i++) {
        self.write(pending[i]);
      }
    }, this._handleConnectionError);

because it it setting self._pending to be null.

The trust certificate function can be called twice if the loadFingerprint callback is called twice.
After looking at the known_hosts file

localhost:7687 fabacd62dbd94...
localhost:7687 fabacd62dbd94...

We see that there are duplicate entries, causing this to fire more than once:

require('readline').createInterface({
    input: fs.createReadStream(knownHostsPath)
  }).on('line', (line)  => {
    if( line.startsWith( serverId )) {
-->   found = true;
-->   cb( line.split(" ")[1] );
    }
  }).on('close', () => {
    if(!found) {
      cb(null);
    }
  });

But why are there duplicates?
Under the circumstances that the known_hosts file exists but is empty (manually created by someone) and if the app is calling for two (or more) driver sessions in the same process tick

const session = driver.session();
const session2 = driver.session();

The driver doesn't have time to update the known_hosts file and thus creates two (or more) entries of the same thing.
After that, the next session that is created:

setTimeout(() => {
    const session3 = driver.session();
}, 1000);

will throw the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions