Skip to content

Handle known_hosts markers using native net-ssh entries - #589

Open
richardkemp-ocn wants to merge 1 commit into
capistrano:masterfrom
richardkemp-ocn:fix-known-hosts-markers
Open

richardkemp-ocn wants to merge 1 commit into
capistrano:masterfrom
richardkemp-ocn:fix-known-hosts-markers

Conversation

@richardkemp-ocn

Copy link
Copy Markdown

Problem

The cached known-hosts reader treats the first field as the host list. For @cert-authority and @revoked lines, that field is the marker; the actual host pattern then fails the key-type check, silently dropping the entry.

This rejects host certificates trusted only through a CA, and prevents net-ssh's revocation checks from seeing revoked host or CA keys. SSHKit installs this reader by default.

Fixes #588. Related: basecamp/kamal#1814 concerns the same cached reader, but its separate multiple-hashed-key lookup problem is not changed here.

Change

  • Keep the existing indexed cache for files without markers.
  • Delegate files containing markers to Net::SSH::KnownHosts, retaining its entry objects, host-pattern/hash matching, and check_host_ip behavior. Cache these lookups by host and relevant matching option; add invalidates them as before.
  • Pass native revoked entries to Net::SSH::HostKeys when supported, so net-ssh's own verifiers enforce revocation and certificate constraints. No custom verifier or dependency-floor change.

Full revocation/constraint behavior is tested with net-ssh 7.3.10 and 8.2.0. Compatibility is capability-based (HostKeys#revoked? / KnownHosts#revoked_keys_for), not a version-string comparison. Older net-ssh keeps its native limitations: 7.3.0 supports CA entries but does not enforce revocation or the newer certificate constraints; this change does not backport those checks.

Minimal reproduction

With a net-ssh version supporting revocation, this uses only a freshly generated, throwaway key and makes no network connections:

require "sshkit"
require "tmpdir"

key = OpenSSL::PKey::RSA.new(2048).public_key
entry = "example.com ssh-rsa #{[key.to_blob].pack('m0')}\n"

Dir.mktmpdir do |dir|
  path = File.join(dir, "known_hosts")
  File.write(path, "@revoked #{entry}#{entry}")
  options = { user_known_hosts_file: [path], global_known_hosts_file: [] }

  [Net::SSH::KnownHosts, SSHKit::Backend::Netssh::KnownHosts.new].each do |reader|
    keys = reader.search_for("example.com", options)
    puts "#{reader}: revoked?=#{keys.revoked?(key)}"
  end
end

Before this change, native net-ssh reports true but SSHKit reports false, leaving the same key trusted by the ordinary entry. After this change, both report true; net-ssh's verifier refuses the key. The certificate regression tests additionally exercise successful CA verification and rejection of expired, wrong-principal, user, and unsupported-critical-option certificates.

Tests

  • bundle exec rake test:units: 230 tests, 3460 assertions, 0 failures/errors/skips, separately with net-ssh 8.2.0 and 7.3.10 (Ruby 3.4.11).
  • Backend tests with net-ssh 7.3.0: 18 tests, 63 assertions, 0 failures/errors/skips; newer-only cases are conditionally defined.
  • New regressions fail against unmodified master. Fixtures contain only generated public keys and certificates using example.com identities.
  • The full green runs use a disposable checkout named sshkit: the pre-existing deprecation-logger test hard-codes that directory name in a regexp and fails in a checkout named sshkit-upstream, on both clean master and this branch. No tests were excluded or modified to obtain the green runs.
  • rake lint is blocked by the existing RuboCop/parser dependency's missing racc/parser under Ruby 3.4; the identical failure occurs on clean master.

Written by Codex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Netssh::KnownHosts ignores @revoked entries, so a revoked host key is not refused

1 participant