Skip to content

fix(hostkey): close known_hosts file when appending a key fails - #500

Merged
kke merged 1 commit into
mainfrom
fix/known-hosts-close-on-write-error
Oct 29, 2026
Merged

kke merged 1 commit into
mainfrom
fix/known-hosts-close-on-write-error

Conversation

@kke

@kke kke commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

When the accept-new host key callback failed to append a key to known_hosts, it returned the WriteString error without closing the file, leaking the descriptor.

The file is now closed before the write error is returned. The success path is unchanged.

No test: a failing write on a regular file can only be forced portably with something like /dev/full (Linux only), and even then the test could only check that an error comes back, not that the descriptor was closed. That's a lot of setup to check a one-line Close.

The accept-new callback returned without closing the known_hosts file
when WriteString failed, leaking the descriptor. Close it before
returning the write error.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke kke added the bug Something isn't working label Oct 25, 2026
@kke
kke requested a lite review from Copilot October 25, 2026 18:11
@kke
kke marked this pull request as ready for review October 25, 2026 18:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The targeted leak is fixed, the success path is unchanged, and no unresolved review issues remain.

No open issues 🎉

What changed in this PR

This pull request fixes a file-descriptor leak in the SSH accept-new host-key callback by closing known_hosts before returning a write error.

Changes:

  • Closes the known_hosts file when appending a key fails.
  • Preserves the existing successful write-and-close path.
File Description
protocol/​ssh/​hostkey/​callbacks.go Closes the file on WriteString failure to prevent descriptor leaks.

Review effort: Lite

@kke
kke merged commit 2adf123 into main Oct 29, 2026
18 checks passed
@kke
kke deleted the fix/known-hosts-close-on-write-error branch October 29, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants