Skip to content

Skip comment and blank lines when parsing known_hosts#634

Open
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:chore/known-hosts-comments
Open

Skip comment and blank lines when parsing known_hosts#634
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:chore/known-hosts-comments

Conversation

@arpitjain099

@arpitjain099 arpitjain099 commented Jun 27, 2026

Copy link
Copy Markdown

Fixes #530.

hostKey in internal/podman/ssh.go parses known_hosts line by line and logged Failed to parse known_hosts for every non-nil error from ssh.ParseKnownHosts. But that function returns io.EOF for comment lines, blank lines, and whitespace-only lines, all of which are valid in a known_hosts file, so a normal file with a couple of comments would spam an error log for each one. This is the spot @wagoodman pointed at in the issue.

The fix short-circuits on io.EOF so those lines are skipped quietly; genuinely malformed key entries come back as non-EOF errors and still log like before.

Added TestHostKey_commentsDoNotLogErrors with a testdata/known_hosts_comments fixture (a comment, a blank line, then a real host entry); it checks the real entry is found and nothing was logged. It fails before the change (two spurious logs) and passes after. go test ./internal/podman/ and go vet ./internal/podman/ are clean.

Thanks for taking a look.

ssh.ParseKnownHosts returns io.EOF for comment, blank, and
whitespace-only lines, which are all valid in an OpenSSH known_hosts
file. hostKey treated every non-nil error the same and logged a
"Failed to parse known_hosts" error for each of those lines, which is
noisy and misleading. Short-circuit on io.EOF so those lines are
skipped quietly while genuinely malformed key entries (non-EOF errors)
still log as before.

Fixes anchore#530

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
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.

Support for OpenSSH comments when parsing known_hosts

1 participant