Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host keyword in ssh config should be case-insensitive #76

Closed
jsundmannorberg opened this issue Sep 13, 2021 · 8 comments
Closed

Host keyword in ssh config should be case-insensitive #76

jsundmannorberg opened this issue Sep 13, 2021 · 8 comments

Comments

@jsundmannorberg
Copy link

Hi,

According to man ssh_config keywords should be case-insensitive and arguments should be case-sensitive.

However, it appears that the "Host" keyword is case-sensitive in the current implementation (on line 127 in OpenSSHConfig.java we have a case-sensitive equality check if(key_value[0].equals("Host")){ )

We have seen cases where this leads to confusing bugs when users assume that the config keywords are case-insensitive, so it would probably be good to change the code to match the specification.

Regards,
Johan Sundman Norberg

@norrisjeremy
Copy link
Contributor

Hi @jsundmannorberg,

Are you mostly concerned with treatment of the "Host" keyword itself? Or is handling other keywords important as well?

While fixing the instance for the "Host" keyword you pointed out is a simple one-line change, there are other ssh_config type keywords that are are used in other classes (like JSch.java, Session.java, etc.).
This would likely require more signficant changes, such as converting the internal config Map from using Hashtable<String, String> to TreeMap<String, String> with a String.CASE_INSENSITIVE_ORDER type Comparator<String>, which would in turn require careful analysis of internal locking, since TreeMap isn't thread-safe like Hashtable.

Thanks,
Jeremy

@jsundmannorberg
Copy link
Author

Hi @norrisjeremy,

The Host keyword is the one we have encountered. In this case it is made confusing by the fact that other keywords in the same class are case-insensitive (the find method converts all keys to uppercase before searching).

I haven't really looked at how other keywords are treated, but at least they have not caused any issues for us so far.

Regards,
Johan Sundman Norberg

@norrisjeremy
Copy link
Contributor

norrisjeremy commented Oct 5, 2021

Hi @jsundmannorberg,

The new 0.1.68 release includes a fix to make the "Host" keyword case-insensitive.

Thanks,
Jeremy

@jsundmannorberg
Copy link
Author

Hi @norrisjeremy,

Thanks! Unfortunately it appears that there is a bug in the new version. Now it checks that key_value[0].toUpperCase().equals("Host") which will never match.

Regards,
Johan

@norrisjeremy
Copy link
Contributor

Hi @jsundmannorberg,

Well that is an embarrassing blunder. Would something like this look correct to you?

diff --git a/src/main/java/com/jcraft/jsch/OpenSSHConfig.java b/src/main/java/com/jcraft/jsch/OpenSSHConfig.java
index a24eb56..8068784 100644
--- a/src/main/java/com/jcraft/jsch/OpenSSHConfig.java
+++ b/src/main/java/com/jcraft/jsch/OpenSSHConfig.java
@@ -124,7 +124,7 @@ public class OpenSSHConfig implements ConfigRepository {
       if(key_value.length <= 1)
         continue;
 
-      if(key_value[0].toUpperCase().equals("Host")){
+      if(key_value[0].equalsIgnoreCase("Host")){
         config.put(host, kv);
         hosts.addElement(host);
         host = key_value[1];

Thanks,
Jeremy

@jsundmannorberg
Copy link
Author

Hi @norrisjeremy,

That looks like it should work. This kind of bug is surprisingly hard to catch, no worries 😄

Thanks,
Johan

@norrisjeremy
Copy link
Contributor

Hi @jsundmannorberg,

I believe this issued should now hopefully be fully resolved now in the new 0.1.69 release.

Thanks,
Jeremy

@jsundmannorberg
Copy link
Author

The new version works perfectly. Thanks!

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

No branches or pull requests

2 participants