skip to content

A Paramiko SFTP upload script calls set_missing_host_key_policy(AutoAddPolicy()); what does that do, and how should Paramiko, JSch and ssh2-sftp-client check host keys instead?

level: juniorimportance: should knowfreq 30%

answer

  1. the policy for keys you do not have
  2. RejectPolicy is the default
  3. nothing is loaded until you ask
  4. JSch's ask and the copied no
  5. a verifier callback in Node

basics

~20 s

AutoAddPolicy makes Paramiko trust and store any host key it has not seen, so an impostor is accepted silently; load a pinned file and keep the default RejectPolicy, use JSch's setKnownHosts with StrictHostKeyChecking yes, and give ssh2-sftp-client a hostVerifier.

solid answer

~40 s

In Paramiko, the **missing-host-key policy** runs only when the server's key is not among the keys the client loaded. `SSHClient` defaults to `RejectPolicy`, but it loads no keys by itself, so a script that connects straight away fails with "not found in known_hosts" — and tutorials paper over that with `AutoAddPolicy`, which adds whatever key answers and connects. The fix is to load a pinned file (`load_system_host_keys(path)`) and keep `RejectPolicy`. In JSch, `StrictHostKeyChecking` defaults to `ask`, and copied snippets set it to `no`; instead call `setKnownHosts(path)` and set it to `yes`. ssh2-sftp-client reads no known_hosts file, so you pass a `hostVerifier` that compares the presented key with the pinned value.

go deeper

for a junior

Recall that AutoAddPolicy and JSch's StrictHostKeyChecking no both mean trust whatever answers, and that the fix is a loaded, pinned host-key file.

for a middle

Explain that Paramiko's policy covers only unknown keys, that SSHClient loads nothing by default, and how JSch and ssh2-sftp-client express the same check.

for a senior

Show how you would review integration code for silent host-key trust, and how library upgrades such as Paramiko 5.0 surface legacy partner servers.

for a principal

Set the rule for every integration team: one pinned-key mechanism per language, reviewed key files, and a path for partners whose servers fall behind library defaults.

## The problem every SFTP library has to solve An SFTP library opens an SSH connection before it can move a single file, and the SSH server proves its identity with a **host key**. The library must decide whether that key belongs to the server you meant to reach. OpenSSH's command-line client keeps the answer in `known_hosts` files; libraries make you choose, and the choice that makes a script "just work" is usually the insecure one. ## Paramiko (Python) Paramiko's `SSHClient` keeps two sets of host keys: **system** keys loaded with `load_system_host_keys()` (read-only, never saved back) and keys loaded with `load_host_keys()` (saved back by `save_host_keys`). Calling `load_system_host_keys()` with no argument reads the user's `~/.ssh/known_hosts`, and silently does nothing if the file is missing. A new `SSHClient` loads **nothing** until you call one of these. When the server's key is not in either set, the client consults its **missing-host-key policy**: | Policy | What it does with an unknown key | |---|---| | `RejectPolicy` (default) | raises `SSHException`: server not found in known_hosts | | `AutoAddPolicy` | adds the key, saves it if a host-keys file was loaded, connects | | `WarningPolicy` | logs a Python warning and connects | A key that **differs** from a stored one is a separate path: `SSHClient` raises `BadHostKeyException`, and the policy is not consulted at all. The classic failure follows from those two facts. A script creates `SSHClient()`, loads no keys, and connects; the default `RejectPolicy` refuses, the developer searches the error, and pastes `set_missing_host_key_policy(AutoAddPolicy())`. From then on, any server that answers — including one placed in the path by an attacker — is accepted and receives the files and the credentials the script sends. The fix keeps the safe default and gives it something to compare against: ```python client = paramiko.SSHClient() client.load_system_host_keys("ci_known_hosts") client.connect("sftp.bank.example.com", username="deploy", key_filename="/run/secrets/deploy_key") ``` **Paramiko 5.0** (May 2026) also removed SHA-1 key exchanges (`diffie-hellman-group1-sha1`, `diffie-hellman-group14-sha1`, `diffie-hellman-group-exchange-sha1`), SHA-1 `ssh-rsa` signatures and GSSAPI support; 4.0 had already removed DSA keys. An upgrade can therefore break a connection to an old server that offers nothing newer — a reason to ask the partner to upgrade, not to pin an old library. ## JSch (Java) JSch is maintained today as the `com.github.mwiede:jsch` fork. Its session setting `StrictHostKeyChecking` defaults to `ask`, which expects a `UserInfo` callback to confirm a new key — a headless job has nobody to answer. Copied snippets "fix" that with `session.setConfig("StrictHostKeyChecking", "no")`, the same silent trust as `AutoAddPolicy`. The pinned version: 1. call `jsch.setKnownHosts("ci_known_hosts")` with a file in OpenSSH's format; 2. set `StrictHostKeyChecking` to `yes` on the session; 3. let a mismatch or unknown key fail the job. The fork also disabled SHA-1 `ssh-rsa` signatures by default from its 0.2.0 release, following OpenSSH; RSA keys keep working through `rsa-sha2-256` and `rsa-sha2-512`. ## ssh2-sftp-client (Node.js) ssh2-sftp-client is a promise-based wrapper around the `ssh2` module and does not read a `known_hosts` file. Host-key checking happens only through connection options: `hostHash` chooses the digest applied to the server's key, and `hostVerifier` is a callback that receives the key (or its hash) and returns whether to accept it. A job pins the expected value and returns true only on an exact match. ## One pinned file for several clients Paramiko's `load_system_host_keys(path)` reads a file in the `known_hosts` format OpenSSH uses, and JSch's `setKnownHosts(path)` takes a known_hosts file too, so one reviewed file can serve a shell step that runs OpenSSH's `sftp` and a library job that talks to the same partner. For a server on a non-standard port, Paramiko looks the host up as `[host]:port`, the bracketed form OpenSSH writes. For ssh2-sftp-client the pinned value is whatever your `hostVerifier` compares against, so record that fingerprint in the same reviewed place rather than in the code that uses it. Keeping the pin in one place has a practical payoff: when the partner rotates its host key, one reviewed change updates every job, and nobody is tempted to switch checking off in the one job that was missed. ## What the three have in common - The safe answer is always **compare with a pinned value obtained out of band**, never "accept what answers first". - A refusal on an unknown or changed key is the library doing its job; the right response is to verify the key with the server's operator. - The pinned file is public data, but its integrity decides which server you trust, so it belongs under review with the code. - The same rule holds for the OpenSSH command-line client, where `StrictHostKeyChecking=yes` with a pinned `UserKnownHostsFile` plays the role of `RejectPolicy` with a loaded file.

  • If Paramiko already has a stored key for the host but the server presents a different one, does AutoAddPolicy accept it?
    No. The missing-host-key policy only runs when no key is stored for that host. When a stored key exists and differs, `SSHClient` raises `BadHostKeyException` whatever the policy is. AutoAddPolicy's danger is the first contact, which on a fresh CI runner is every run.
  • After upgrading to Paramiko 5.0, connections to one old partner server fail during negotiation. What changed?
    Paramiko 5.0 removed SHA-1 key exchange methods, including `diffie-hellman-group14-sha1` and `diffie-hellman-group-exchange-sha1`, SHA-1 `ssh-rsa` signatures and GSSAPI. A server offering only those has nothing in common with the client. The durable fix is a server that offers SHA-2 or curve-based key exchange and `rsa-sha2-256`/`512` or Ed25519 host keys.

  • AutoAddPolicy is safe because the connection is still encrypted
  • Paramiko's SSHClient reads ~/.ssh/known_hosts automatically on connect
  • Setting JSch's StrictHostKeyChecking to no is the standard fix for CI
  • Paramiko 5.0 stopped accepting RSA keys altogether
  • ssh2-sftp-client checks the server against known_hosts by default