Fix escaping in WildcardHostMatcher (#382)

* Escape '[' and ']' in WildcardHostMatcher

* Anchoring regex to match entire string (Fixes #381)
This commit is contained in:
Jeroen van Erp
2017-11-13 15:49:48 +01:00
committed by GitHub
parent d2e0f50d0c
commit a71a7d7d33
3 changed files with 7 additions and 2 deletions

View File

@@ -135,7 +135,7 @@ public class KnownHostMatchers {
private final Pattern pattern; private final Pattern pattern;
public WildcardHostMatcher(String hostEntry) { public WildcardHostMatcher(String hostEntry) {
this.pattern = Pattern.compile(hostEntry.replace(".", "\\.").replace("*", ".*").replace("?", ".")); this.pattern = Pattern.compile("^" + hostEntry.replace("[", "\\[").replace("]", "\\]").replace(".", "\\.").replace("*", ".*").replace("?", ".") + "$");
} }
@Override @Override

View File

@@ -360,7 +360,7 @@ public class OpenSSHKnownHosts
} }
protected String getHostPart() { protected String getHostPart() {
return hostPart; return hostPart;
} }
} }

View File

@@ -49,6 +49,11 @@ class KnownHostMatchersSpec extends Specification {
"aaa.b??.com" | "aaa.bccd.com" | false "aaa.b??.com" | "aaa.bccd.com" | false
"|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.1.61" | true "|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.1.61" | true
"|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.2.61" | false "|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.2.61" | false
"[aaa.bbb.com]:2222" | "aaa.bbb.com" | false
"[aaa.bbb.com]:2222" | "[aaa.bbb.com]:2222" | true
"[aaa.?bb.com]:2222" | "[aaa.dbb.com]:2222" | true
"[aaa.?xb.com]:2222" | "[aaa.dbb.com]:2222" | false
"[*.bbb.com]:2222" | "[aaa.bbb.com]:2222" | true
yesno = match ? "" : "no" yesno = match ? "" : "no"
} }
} }