Skip to content

fix(ssh): enable host key verification with known_hosts loading#48

Open
DeryFerd wants to merge 1 commit into
anvie:mainfrom
DeryFerd:fix/ssh-enable-host-key-verification
Open

fix(ssh): enable host key verification with known_hosts loading#48
DeryFerd wants to merge 1 commit into
anvie:mainfrom
DeryFerd:fix/ssh-enable-host-key-verification

Conversation

@DeryFerd
Copy link
Copy Markdown
Contributor

The SSH backend in backend/tools/lib/backends/ssh_backend.py was using paramiko.AutoAddPolicy(), which silently accepts any host key presented by the remote server. No verification, no warning, no record. This makes every SSH workplace connection vulnerable to man-in-the-middle attacks — an attacker on the same network could intercept the connection, present their own host key, and capture all transmitted data including commands, file contents, and credentials.

Two changes:

  1. AutoAddPolicy()WarningPolicy(). Unknown hosts still connect (backward compatible — we don't want to break existing setups on first run), but paramiko now logs a warning instead of silently accepting. This at least makes the risk visible in logs.

  2. New _load_host_keys() method. Before connecting, the backend now tries to load known host keys from standard locations: ~/.ssh/known_hosts first, then /etc/ssh/ssh_known_hosts and /etc/ssh/known_hosts as fallbacks. If a host key is found in one of these files and the remote server presents a different key, paramiko will reject the connection rather than accepting it. If no known_hosts file exists anywhere, it falls back to WarningPolicy behavior and logs a hint suggesting the user run ssh-keyscan -H <host> >> ~/.ssh/known_hosts.

This mirrors how the ssh CLI works by default — it trusts keys it has seen before and warns about new ones. The difference is that before this change, the SSH backend was effectively running with StrictHostKeyChecking no on every connection.

For users who already have SSH workplaces configured: Their first connection after this upgrade will still succeed (via WarningPolicy), but they'll see a warning in the logs. If they want full verification, they can pre-populate known_hosts by connecting once via the ssh CLI or running ssh-keyscan.

For new connections: Same behavior as before, just with a visible warning instead of silent acceptance.

Three tests verify that WarningPolicy is used, AutoAddPolicy is gone, and the _load_host_keys method exists on SSHBackend.

- Replace AutoAddPolicy (silently accepts any host) with WarningPolicy
- Add _load_host_keys() to load ~/.ssh/known_hosts and system known_hosts
- Log warning when no known_hosts file found with ssh-keyscan hint
- Unknown hosts still connect (backward compatible) but with warning
- Add 3 tests verifying WarningPolicy and _load_host_keys method
ureh-terbalik pushed a commit to ureh-terbalik/evonic that referenced this pull request May 20, 2026
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.

1 participant