Skip to content

Commit a572b10

Browse files
authored
fix: "@authority" canonicalization corrected (#196)
* functions to parse and normalize host and port according to RFC9421 grammar * @authority canonicalization fix
1 parent b6e9ce3 commit a572b10

4 files changed

Lines changed: 332 additions & 27 deletions

File tree

‎canonicalizer.go‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package httpsig
22

33
import (
44
"fmt"
5-
"net"
65
"net/http"
76
"net/url"
87
"slices"
@@ -150,25 +149,35 @@ func newAuthorityCanonicalizer(params *httpsfv.Params) (canonicalizer, error) {
150149
return nil, err
151150
}
152151

153-
host, port, err := net.SplitHostPort(msg.Authority)
154-
if err != nil {
155-
// no port, just use the whole thing
156-
return []string{strings.ToLower(msg.Authority)}, nil //nolint:nilerr
152+
host, port, valid := splitHostPort(msg.Authority)
153+
if !valid {
154+
return nil, fmt.Errorf("%w: invalid authority %q", ErrCanonicalization, msg.Authority)
157155
}
158156

159-
// default ports shall be omitted
160-
switch strings.ToLower(msg.URL.Scheme) {
161-
case "http":
162-
if port == "80" {
163-
return []string{strings.ToLower(host)}, nil
164-
}
165-
case "https":
166-
if port == "443" {
167-
return []string{strings.ToLower(host)}, nil
157+
host = normalizeHost(host)
158+
if port == "" {
159+
return []string{host}, nil
160+
}
161+
162+
port = strings.TrimLeft(port, "0")
163+
if port == "" {
164+
port = "0"
165+
}
166+
167+
if msg.URL != nil {
168+
switch strings.ToLower(msg.URL.Scheme) {
169+
case "http":
170+
if port == "80" {
171+
return []string{host}, nil
172+
}
173+
case "https":
174+
if port == "443" {
175+
return []string{host}, nil
176+
}
168177
}
169178
}
170179

171-
return []string{strings.ToLower(msg.Authority)}, nil
180+
return []string{host + ":" + port}, nil
172181
}), nil
173182
}
174183

‎canonicalizer_test.go‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,9 @@ func TestCanonicalizeAuthority(t *testing.T) {
510510
want []string
511511
errContains string
512512
}{
513-
"@authority for request - valid http without port": {
513+
"@authority for request - without port": {
514514
identifier: `"@authority"`,
515-
message: &Message{Authority: "example.com", URL: &url.URL{Scheme: "http"}, IsRequest: true},
516-
want: []string{"example.com"},
517-
},
518-
"@authority for request - valid https without port": {
519-
identifier: `"@authority"`,
520-
message: &Message{Authority: "example.com", URL: &url.URL{Scheme: "https"}, IsRequest: true},
515+
message: &Message{Authority: "ExAmPlE.COM", URL: &url.URL{Scheme: "http"}, IsRequest: true},
521516
want: []string{"example.com"},
522517
},
523518
"@authority for request - valid http with default port": {
@@ -530,16 +525,56 @@ func TestCanonicalizeAuthority(t *testing.T) {
530525
message: &Message{Authority: "example.com:443", URL: &url.URL{Scheme: "https"}, IsRequest: true},
531526
want: []string{"example.com"},
532527
},
533-
"@authority for request - valid http with custom port": {
534-
identifier: `"@authority"`,
535-
message: &Message{Authority: "example.com:8080", URL: &url.URL{Scheme: "http"}, IsRequest: true},
536-
want: []string{"example.com:8080"},
537-
},
538528
"@authority for request - valid https with custom port": {
539529
identifier: `"@authority"`,
540530
message: &Message{Authority: "example.com:8443", URL: &url.URL{Scheme: "https"}, IsRequest: true},
541531
want: []string{"example.com:8443"},
542532
},
533+
"@authority for request - ipv6 default port": {
534+
identifier: `"@authority"`,
535+
message: &Message{Authority: "[2001:DB8::1]:443", URL: &url.URL{Scheme: "https"}, IsRequest: true},
536+
want: []string{"[2001:db8::1]"},
537+
},
538+
"@authority for request - percent-encoded letter": {
539+
identifier: `"@authority"`,
540+
message: &Message{Authority: "EXA%6Dple.COM", URL: &url.URL{Scheme: "https"}, IsRequest: true},
541+
want: []string{"example.com"},
542+
},
543+
"@authority for request - padded default port": {
544+
identifier: `"@authority"`,
545+
message: &Message{Authority: "EXAMPLE.com:0443", URL: &url.URL{Scheme: "HTTPS"}, IsRequest: true},
546+
want: []string{"example.com"},
547+
},
548+
"@authority for request - padded non-default port": {
549+
identifier: `"@authority"`,
550+
message: &Message{Authority: "EXAMPLE.com:008443", URL: &url.URL{Scheme: "https"}, IsRequest: true},
551+
want: []string{"example.com:8443"},
552+
},
553+
"@authority for request - empty port": {
554+
identifier: `"@authority"`,
555+
message: &Message{Authority: "EXAMPLE.com:", URL: &url.URL{Scheme: "https"}, IsRequest: true},
556+
want: []string{"example.com"},
557+
},
558+
"@authority for request - zero port": {
559+
identifier: `"@authority"`,
560+
message: &Message{Authority: "example.com:0000", URL: &url.URL{Scheme: "https"}, IsRequest: true},
561+
want: []string{"example.com:0"},
562+
},
563+
"@authority for request - no default-port removal for other scheme": {
564+
identifier: `"@authority"`,
565+
message: &Message{Authority: "EXAMPLE.com:443", URL: &url.URL{Scheme: "http"}, IsRequest: true},
566+
want: []string{"example.com:443"},
567+
},
568+
"@authority for request - no default-port removal without scheme": {
569+
identifier: `"@authority"`,
570+
message: &Message{Authority: "EXAMPLE.com:80", URL: &url.URL{}, IsRequest: true},
571+
want: []string{"example.com:80"},
572+
},
573+
"@authority for request - invalid non-numeric port": {
574+
identifier: `"@authority"`,
575+
message: &Message{Authority: "example.com:abc", URL: &url.URL{Scheme: "https"}, IsRequest: true},
576+
errContains: "invalid authority",
577+
},
543578
"@authority for request - invalid": {
544579
identifier: `"@authority";req`,
545580
message: &Message{Authority: "example.com", URL: &url.URL{Scheme: "http"}, IsRequest: true},

‎net.go‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package httpsig
2+
3+
import (
4+
"net/netip"
5+
"strings"
6+
)
7+
8+
// splitHostPort splits an HTTP authority into a validated host (including
9+
// IP-literal brackets) and an optional port. Userinfo, paths, queries, and
10+
// fragments are not permitted.
11+
//
12+
//nolint:cyclop
13+
func splitHostPort(authority string) (string, string, bool) {
14+
if authority == "" {
15+
return "", "", false
16+
}
17+
18+
var (
19+
host string
20+
port string
21+
)
22+
23+
if authority[0] == '[' { //nolint:nestif
24+
end := strings.IndexByte(authority, ']')
25+
if end == -1 {
26+
return "", "", false
27+
}
28+
29+
if !validIPLiteral(authority[1:end]) {
30+
return "", "", false
31+
}
32+
33+
host = authority[:end+1]
34+
if end+1 < len(authority) {
35+
if authority[end+1] != ':' {
36+
return "", "", false
37+
}
38+
39+
port = authority[end+2:]
40+
}
41+
} else {
42+
if before, after, ok := strings.Cut(authority, ":"); ok {
43+
host, port = before, after
44+
} else {
45+
host = authority
46+
}
47+
48+
if !validAuthorityRegName(host) {
49+
return "", "", false
50+
}
51+
}
52+
53+
for i := range len(port) {
54+
if port[i] < '0' || port[i] > '9' {
55+
return "", "", false
56+
}
57+
}
58+
59+
return host, port, true
60+
}
61+
62+
// normalizeHost applies HTTP URI case and percent-encoding
63+
// normalization to a host already validated by splitHostPort.
64+
//
65+
//nolint:cyclop
66+
func normalizeHost(host string) string {
67+
if strings.HasPrefix(host, "[") || !strings.Contains(host, "%") {
68+
return strings.ToLower(host)
69+
}
70+
71+
const upperHex = "0123456789ABCDEF"
72+
73+
var normalized strings.Builder
74+
normalized.Grow(len(host))
75+
76+
for i := 0; i < len(host); i++ {
77+
ch := host[i]
78+
if ch == '%' {
79+
decoded := hexValue(host[i+1])<<4 | hexValue(host[i+2]) //nolint:mnd
80+
if (decoded >= 'a' && decoded <= 'z') ||
81+
(decoded >= 'A' && decoded <= 'Z') ||
82+
(decoded >= '0' && decoded <= '9') ||
83+
decoded == '-' || decoded == '.' || decoded == '_' || decoded == '~' {
84+
ch = decoded
85+
} else {
86+
normalized.WriteByte('%')
87+
normalized.WriteByte(upperHex[decoded>>4])
88+
normalized.WriteByte(upperHex[decoded&0x0f])
89+
90+
i += 2
91+
92+
continue
93+
}
94+
95+
i += 2
96+
}
97+
98+
if ch >= 'A' && ch <= 'Z' {
99+
ch += 'a' - 'A'
100+
}
101+
102+
normalized.WriteByte(ch)
103+
}
104+
105+
return normalized.String()
106+
}
107+
108+
// hexValue converts a validated ASCII hex digit to its numeric value.
109+
func hexValue(ch byte) byte {
110+
switch {
111+
case ch >= '0' && ch <= '9':
112+
return ch - '0'
113+
case ch >= 'A' && ch <= 'F':
114+
return ch - 'A' + 10 //nolint:mnd
115+
default:
116+
return ch - 'a' + 10 //nolint:mnd
117+
}
118+
}
119+
120+
func validIPLiteral(literal string) bool {
121+
ip, err := netip.ParseAddr(literal)
122+
if err == nil {
123+
return ip.Is6() && ip.Zone() == ""
124+
}
125+
126+
return validIPvFuture(literal)
127+
}
128+
129+
func validAuthorityRegName(name string) bool {
130+
if name == "" {
131+
return false
132+
}
133+
134+
for i := 0; i < len(name); i++ {
135+
ch := name[i]
136+
if isRegNameChar(ch) {
137+
continue
138+
}
139+
140+
if ch != '%' || i+2 >= len(name) || !isHexDigit(name[i+1]) || !isHexDigit(name[i+2]) {
141+
return false
142+
}
143+
144+
i += 2
145+
}
146+
147+
return true
148+
}
149+
150+
//nolint:cyclop
151+
func validIPvFuture(literal string) bool {
152+
if len(literal) < 4 || (literal[0] != 'v' && literal[0] != 'V') {
153+
return false
154+
}
155+
156+
i := 1
157+
for i < len(literal) && isHexDigit(literal[i]) {
158+
i++
159+
}
160+
161+
if i == 1 || i >= len(literal)-1 || literal[i] != '.' {
162+
return false
163+
}
164+
165+
for i++; i < len(literal); i++ {
166+
if !isRegNameChar(literal[i]) && literal[i] != ':' {
167+
return false
168+
}
169+
}
170+
171+
return true
172+
}
173+
174+
func isRegNameChar(ch byte) bool {
175+
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') {
176+
return true
177+
}
178+
179+
switch ch {
180+
case '-', '.', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=':
181+
return true
182+
}
183+
184+
return false
185+
}
186+
187+
func isHexDigit(ch byte) bool {
188+
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')
189+
}

‎net_test.go‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package httpsig
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSplitHostPort(t *testing.T) {
10+
t.Parallel()
11+
12+
for name, tc := range map[string]struct {
13+
input string
14+
host string
15+
port string
16+
valid bool
17+
}{
18+
"domain without port": {input: "EXAMPLE.com", host: "EXAMPLE.com", valid: true},
19+
"domain with port": {input: "EXAMPLE.com:0443", host: "EXAMPLE.com", port: "0443", valid: true},
20+
"domain with empty port": {input: "EXAMPLE.com:", host: "EXAMPLE.com", valid: true},
21+
"ipv6 without port": {input: "[2001:DB8::1]", host: "[2001:DB8::1]", valid: true},
22+
"ipv6 with port": {input: "[2001:DB8::1]:443", host: "[2001:DB8::1]", port: "443", valid: true},
23+
"ipv6 with empty port": {input: "[2001:DB8::1]:", host: "[2001:DB8::1]", valid: true},
24+
"invalid ipv6 encoded zone": {input: "[fe80::1%25eth0]"},
25+
"invalid ipv6 encoded zone case": {input: "[fe80::1%25ETH0]"},
26+
"ipvfuture": {input: "[v1.a:2]", host: "[v1.a:2]", valid: true},
27+
"percent-encoded reg-name": {input: "exa%6Dple.com", host: "exa%6Dple.com", valid: true},
28+
"invalid empty authority": {input: ""},
29+
"invalid empty hostname": {input: ":80"},
30+
"invalid alpha port": {input: "example.com:abc"},
31+
"invalid signed port": {input: "example.com:+80"},
32+
"invalid unbracketed ipv6": {input: "2001:db8::1"},
33+
"invalid ipv6": {input: "[not-ipv6]"},
34+
"invalid missing bracket": {input: "[2001:db8::1"},
35+
"invalid ipvfuture": {input: "[v.no]"},
36+
"invalid zone encoding": {input: "[fe80::1%eth0]"},
37+
"invalid userinfo": {input: "user@example.com"},
38+
"invalid path": {input: "example.com/path"},
39+
"invalid percent escape": {input: "exa%GGmple.com"},
40+
"invalid non-ASCII": {input: "exämple.com"},
41+
} {
42+
t.Run(name, func(t *testing.T) {
43+
host, port, valid := splitHostPort(tc.input)
44+
assert.Equal(t, tc.valid, valid)
45+
assert.Equal(t, tc.host, host)
46+
assert.Equal(t, tc.port, port)
47+
})
48+
}
49+
}
50+
51+
func TestNormalizeHost(t *testing.T) {
52+
t.Parallel()
53+
54+
for name, tc := range map[string]struct {
55+
input string
56+
want string
57+
}{
58+
"plain lowercase": {input: "example.com", want: "example.com"},
59+
"mixed case": {input: "EXAMPLE.COM", want: "example.com"},
60+
"percent-encoded letter": {input: "EXA%6Dple.COM", want: "example.com"},
61+
"percent-encoded capital": {input: "EX%41MPLE.COM", want: "example.com"},
62+
"percent-encoded tilde": {input: "EXA%7Emple.COM", want: "exa~mple.com"},
63+
"percent-encoded dot": {input: "EXAMPLE%2eCOM", want: "example.com"},
64+
"percent-encoded reserved": {input: "EXAMPLE%2f.COM", want: "example%2F.com"},
65+
"percent-encoded non-ASCII": {input: "EXAMPLE%C3%a4.COM", want: "example%C3%A4.com"},
66+
"do not double-decode percent": {input: "EXAMPLE%256d.COM", want: "example%256d.com"},
67+
} {
68+
t.Run(name, func(t *testing.T) {
69+
assert.Equal(t, tc.want, normalizeHost(tc.input))
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)