Skip to content
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

basic host key verification idea #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Options are an object with following properties:
* `bastionHost` (optional): You can specify a bastion host if you want
* `passphrase` (optional): You can specify the passphrase when you have an encrypted private key. If you don't specify the passphrase and you use an encrypted private key, you get prompted in the command line.
* `noReadline` (optional): Don't prompt for private key passphrases using readline (eg if this is not run in an interactive session)
* `verifyKnownHosts` (optional): Verify remote SSH hostkey using your known_hosts. The implementation only works on systems with an OpenSSH (OpenBSD) version of ssh-keygen.

#### `connection.executeCommand(command: string): Promise<void>`

Expand Down
32 changes: 30 additions & 2 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ interface Options {
endHost: string
agentSocket?: string,
skipAutoPrivateKey?: boolean
noReadline?: boolean
noReadline?: boolean,
verifyKnownHosts?: boolean
}

interface ForwardingOptions {
Expand Down Expand Up @@ -148,13 +149,40 @@ class SSHConnection {
const connection = new Client()
return new Promise<Client>(async (resolve, reject) => {

const options = {
const options : {[k: string]: any} = {
host,
port: this.options.endPort,
username: this.options.username,
password: this.options.password,
privateKey: this.options.privateKey
}
if (this.options.verifyKnownHosts) {
options.hostVerifier = function(hashedKey, verifyCb) {
let b64key = hashedKey.toString('base64');
if (!b64key) return false;

let escapedHost = host.replace(/[^a-z\d\.\-]/g,'$1'); // Make arg safer for shell-exec by enforcing a strict whitelist of literal host chars
let connPort = Number(options.port);
if (connPort && connPort !== 22) {
escapedHost = '['+escapedHost+']:'+connPort;
}
let { exec } = require("child_process");
exec('ssh-keygen -F "'+escapedHost+'"', (error, stdout) => {
if (error) {
verifyCb(false);
return;
}
let matching_key = stdout.split("\n").find(function(keyline) {
return keyline.split(" ")[2] === b64key;
});
if (matching_key) {
verifyCb(true);
} else {
verifyCb(false);
}
});
};
}
if (this.options.agentForward) {
options['agentForward'] = true

Expand Down