Skip to content

Commit

Permalink
Fix escaping in WildcardHostMatcher (#382)
Browse files Browse the repository at this point in the history
* Escape '[' and ']' in WildcardHostMatcher

* Anchoring regex to match entire string (Fixes #381)
  • Loading branch information
hierynomus committed Nov 13, 2017
1 parent d2e0f50 commit a71a7d7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
Expand Up @@ -135,7 +135,7 @@ private static class WildcardHostMatcher implements HostMatcher {
private final Pattern pattern;

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

@Override
Expand Down
Expand Up @@ -360,7 +360,7 @@ private String getKeyString() {
}

protected String getHostPart() {
return hostPart;
return hostPart;
}
}

Expand Down
Expand Up @@ -49,6 +49,11 @@ class KnownHostMatchersSpec extends Specification {
"aaa.b??.com" | "aaa.bccd.com" | false
"|1|F1E1KeoE/eEWhi10WpGv4OdiO6Y=|3988QV0VE8wmZL7suNrYQLITLCg=" | "192.168.1.61" | true
"|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"
}
}

0 comments on commit a71a7d7

Please sign in to comment.