diff --git a/scripts/run-live.js b/scripts/run-live.js index f7f5ebd..dde3fb7 100644 --- a/scripts/run-live.js +++ b/scripts/run-live.js @@ -228,6 +228,18 @@ const checks = { contains: [server1, server2, server3].filter(Boolean), excludes: ['SSH failed'], }, + // A rejected hop key must not run that host's command or skip its sibling. + hop_host_key: { + contains: ['pin:primary', 'pin:sibling'], + excludes: ['pin:hop-must-not-run'], + logContains: [ + 'pin-order:primary,sibling', + 'pin-error-count:1', + 'pin-rejections:1', + 'Host denied (verification failed)', + ], + allowErrors: ['Hop'], + }, notifications: { contains: ['Note: This line is added to sessionText only', '\n/tmp\n'], logContains: ['Note: Starting deployment'], diff --git a/test/README.md b/test/README.md index 072b6d9..ad8bdd7 100644 --- a/test/README.md +++ b/test/README.md @@ -171,6 +171,28 @@ node scripts/run-live.js host_traversal host_faults --timeout 180 The runner check for `host_faults` encodes the post-change contract and allows the expected `ECONNREFUSED` marker and `Connection` error type. +### Host-key pinning on hops + +`hop_host_key.js` proves that `hostHash` / `hostVerifier` are enforced on a hop, +not only on the primary. The nested host rejects every key with +`hostVerifier: () => false`, and a valid sibling hop follows it: + +```text +primary -> rejected hop (verifier always false) -> valid sibling +``` + +The test needs no stored fingerprint, so it works with the throwaway host keys +generated in CI. It logs `pin-order`, `pin-error-count`, and `pin-rejections`, +and exits nonzero if the rejected hop's command runs, the sibling is skipped, +or fewer than one verification failure is recorded. The runner check also +allows the expected `Hop` error type. + +Run it directly with: + +```bash +node scripts/run-live.js hop_host_key +``` + Each test runs in its own process with a 180-second default timeout. The runner does not trust the exit code: a failed connection still reaches the connect callback with empty session text and exits zero. It checks the callback, the diff --git a/test/coverage.md b/test/coverage.md index 76891cd..e9298af 100644 --- a/test/coverage.md +++ b/test/coverage.md @@ -6,6 +6,11 @@ diagnostic here, not a release gate. ## Current coverage (Node v26.13.0, measured 2026-11-23) +> **Snapshot note (2026-11-24):** these figures are the published `v4.1.0` +> baseline. The later test-only `hop_host_key.js` addition brings the live suite +> to 31 tests; coverage was not remeasured for that change, so the figures and +> miss-overlap table below are intentionally unchanged. + Measured with Node 26's built-in test coverage reporter against the current build. Unit tests run against `lib/`; live tests run against `src/`. The live run uses the default environment (`DEBUG` and `VERBOSE` unset), so diff --git a/test/live/hop_host_key.js b/test/live/hop_host_key.js new file mode 100644 index 0000000..d34966d --- /dev/null +++ b/test/live/hop_host_key.js @@ -0,0 +1,105 @@ +const dotenv = require('dotenv').config(); +const { logFile, log } = require('../utils/logging.js'); +logFile('hop_host_key.js'); + +const SSH2Shell = require('../utils/module.js'); +const { readPrivateKey } = require('../utils/private_key.js'); + +// Hops are opened with `forwardOut` from the parent connection and authenticate +// with the runner's local key. This test rejects the second host's key with a +// verifier, so it needs no stored fingerprint and works with CI's throwaway +// host keys. A valid sibling hop after the rejected one proves the failure is +// contained. +const privateKey = readPrivateKey(); + +const primary = { + server: { + host: process.env.HOST, + port: process.env.PORT, + userName: process.env.USER_NAME, + privateKey, + passPhrase: process.env.KEY_PASSPHRASE, + }, + commands: ['echo pin:primary'], +}; + +const rejectedHop = { + server: { + host: process.env.SERVER2_HOST, + port: process.env.SERVER2_PORT || process.env.PORT, + userName: process.env.SERVER2_USER_NAME, + privateKey, + passPhrase: process.env.KEY_PASSPHRASE, + hostHash: 'sha256', + hostVerifier: () => false, + }, + commands: ['echo pin:hop-must-not-run'], +}; + +const siblingHop = { + server: { + host: process.env.SERVER3_HOST, + port: process.env.SERVER3_PORT || process.env.PORT, + userName: process.env.SERVER3_USER_NAME, + privateKey, + passPhrase: process.env.KEY_PASSPHRASE, + }, + commands: ['echo pin:sibling'], +}; + +primary.hosts = [rejectedHop, siblingHop]; + +const expectedOrder = ['primary', 'sibling']; +const rejectionMessage = 'Host denied (verification failed)'; + +const SSH = new SSH2Shell(primary); + +const errors = []; +SSH.on('msg', log); +SSH.debug = process.env.DEBUG === 'true'; +SSH.verbose = process.env.VERBOSE === 'true'; +SSH.on('error', (err, type) => { + const message = err && err.message ? err.message : String(err); + errors.push(`${type || ''}:${message}`); + log(`pin-error:${type || ''}:${message}`); +}); + +SSH.connect((sessionText) => { + // Command echoes repeat each marker, so keep first-seen order only. + const order = []; + for (const match of sessionText.matchAll(/pin:(primary|hop-must-not-run|sibling)/g)) { + if (order[order.length - 1] !== match[1]) { + order.push(match[1]); + } + } + const rejections = errors.filter((error) => error.includes(rejectionMessage)).length; + + const text = `-----Callback session text:\n${sessionText}\n-----Callback end`; + log(text); + log(`pin-order:${order.join(',')}`); + log(`pin-error-count:${errors.length}`); + log(`pin-rejections:${rejections}`); + console.log(text); + console.log(`pin-order:${order.join(',')}`); + console.log(`pin-error-count:${errors.length}`); + console.log(`pin-rejections:${rejections}`); + + const problems = []; + if (order.join(',') !== expectedOrder.join(',')) { + problems.push(`expected hop order ${expectedOrder.join(',')} (got ${order.join(',')})`); + } + if (errors.length !== 1) { + problems.push(`expected 1 error event (got ${errors.length})`); + } + if (rejections !== 1) { + problems.push(`expected 1 host-key rejection (got ${rejections})`); + } + + if (problems.length > 0) { + for (const problem of problems) { + console.error(problem); + log(`pin-expectation:${problem}`); + } + process.exitCode = 1; + } +});