I have seen people who suggest using a code chunk like this:
begin
Net::SSH::start
rescue Net::SSH::HostKeyError => e
e.remember_host!
retry
end
As the exception type suggests, it's for cases where there's a hostkey mismatch, and it adds the host to known hosts, as far as I understand.
So my question is, isn't it risky to just unconditionally add the host whenever the host isn't recognized? I imagine the host needs to be recognized in order to avoid man-in-the-middle attacks or similar, and just immediately adding any unrecognized hosts seem like a security breach.
Please keep in mind that I am very new to Ruby, know very little about SSH or security in general so I would appreciate simple explanations. And this is my first stack overflow question, so I am sorry if I am violating any guidelines. I wanted to ask this as a comment on a question that was actually using that code chunk, but I didn't have enough reputation.
I'm looking forward to any explanations and thanks in advance.