@@ -189,6 +189,37 @@ func TestSubjectQualifiesForPublicCert(t *testing.T) {
189189 }
190190}
191191
192+ func TestHostOnly (t * testing.T ) {
193+ for i , test := range []struct {
194+ input string
195+ expect string
196+ }{
197+ // hostname without port
198+ {"example.com" , "example.com" },
199+ // hostname with port
200+ {"example.com:443" , "example.com" },
201+ // IPv4 without port
202+ {"1.2.3.4" , "1.2.3.4" },
203+ // IPv4 with port
204+ {"1.2.3.4:80" , "1.2.3.4" },
205+ // IPv6 without port and without brackets
206+ {"::1" , "::1" },
207+ // IPv6 with port (brackets required by RFC 7230)
208+ {"[::1]:80" , "::1" },
209+ // IPv6 without port but with brackets (Go's HTTP server format for host-only)
210+ {"[::1]" , "::1" },
211+ // full IPv6 without port but with brackets
212+ {"[2001:db8::1]" , "2001:db8::1" },
213+ // full IPv6 with port
214+ {"[2001:db8::1]:8080" , "2001:db8::1" },
215+ } {
216+ actual := hostOnly (test .input )
217+ if actual != test .expect {
218+ t .Errorf ("Test %d: hostOnly(%q) = %q, want %q" , i , test .input , actual , test .expect )
219+ }
220+ }
221+ }
222+
192223func TestMatchWildcard (t * testing.T ) {
193224 for i , test := range []struct {
194225 subject , wildcard string
@@ -217,6 +248,10 @@ func TestMatchWildcard(t *testing.T) {
217248 {"1.2.3.4.5.6" , "*.*.*.*.*.*" , true },
218249 {"0.1.2.3.4.5.6" , "*.*.*.*.*.*" , false },
219250 {"1.2.3.4" , "1.2.3.*" , false }, // https://tools.ietf.org/html/rfc2818#section-3.1
251+ // Bracketed IPv6 subjects (from HTTP Host headers) must match bare IPv6 wildcards.
252+ {"[::1]" , "::1" , true },
253+ {"[2001:db8::1]" , "2001:db8::1" , true },
254+ {"[::1]" , "::2" , false },
220255 } {
221256 actual := MatchWildcard (test .subject , test .wildcard )
222257 if actual != test .expect {
0 commit comments