Skip to content

Commit a71a7d7

Browse files
authored
Fix escaping in WildcardHostMatcher (#382)
* Escape '[' and ']' in WildcardHostMatcher * Anchoring regex to match entire string (Fixes #381)
1 parent d2e0f50 commit a71a7d7

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/main/java/com/hierynomus/sshj/transport/verification/KnownHostMatchers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static class WildcardHostMatcher implements HostMatcher {
135135
private final Pattern pattern;
136136

137137
public WildcardHostMatcher(String hostEntry) {
138-
this.pattern = Pattern.compile(hostEntry.replace(".", "\\.").replace("*", ".*").replace("?", "."));
138+
this.pattern = Pattern.compile("^" + hostEntry.replace("[", "\\[").replace("]", "\\]").replace(".", "\\.").replace("*", ".*").replace("?", ".") + "$");
139139
}
140140

141141
@Override

src/main/java/net/schmizz/sshj/transport/verification/OpenSSHKnownHosts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ private String getKeyString() {
360360
}
361361

362362
protected String getHostPart() {
363-
return hostPart;
363+
return hostPart;
364364
}
365365
}
366366

src/test/groovy/com/hierynomus/sshj/transport/verification/KnownHostMatchersTest.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class KnownHostMatchersSpec extends Specification {
4949
"aaa.b??.com" | "aaa.bccd.com" | false
5050
"|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.1.61" | true
5151
"|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.2.61" | false
52+
"[aaa.bbb.com]:2222" | "aaa.bbb.com" | false
53+
"[aaa.bbb.com]:2222" | "[aaa.bbb.com]:2222" | true
54+
"[aaa.?bb.com]:2222" | "[aaa.dbb.com]:2222" | true
55+
"[aaa.?xb.com]:2222" | "[aaa.dbb.com]:2222" | false
56+
"[*.bbb.com]:2222" | "[aaa.bbb.com]:2222" | true
5257
yesno = match ? "" : "no"
5358
}
5459
}

0 commit comments

Comments
 (0)