-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathhostauthorization.go
More file actions
246 lines (210 loc) · 5.36 KB
/
Copy pathhostauthorization.go
File metadata and controls
246 lines (210 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package hostauthorization
import (
"fmt"
"net"
"strings"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/utils/v2"
utilsstrings "github.com/gofiber/utils/v2/strings"
"golang.org/x/net/idna"
)
// RFC 1035 length limits.
const (
maxDomainLength = 253
maxLabelLength = 63
)
type parsedHosts struct {
exact map[string]struct{}
wildcardSuffixes []string
}
// parseAllowedHosts splits AllowedHosts into exact and wildcard groups,
// normalizing entries (port strip, lowercase, IDN→Punycode) and enforcing
// RFC 1035 length limits. Panics on misconfiguration so it surfaces at startup.
func parseAllowedHosts(hosts []string) parsedHosts {
parsed := parsedHosts{
exact: make(map[string]struct{}, len(hosts)),
}
for _, h := range hosts {
h = utils.TrimSpace(h)
if h == "" {
continue
}
// Reject the leading-dot form some other tools use; we want "*.example.com".
if len(h) > 1 && h[0] == '.' {
panic("hostauthorization: invalid host " + h + " — subdomain wildcards use the \"*.example.com\" form")
}
isWildcard := strings.HasPrefix(h, "*.")
if isWildcard {
h = h[2:]
}
h = normalizeHost(h)
if h == "" {
continue
}
validateHostLength(h)
if isWildcard {
// Stored with leading dot so the hot-path HasSuffix check stays alloc-free.
parsed.wildcardSuffixes = append(parsed.wildcardSuffixes, "."+h)
} else {
parsed.exact[h] = struct{}{}
}
}
return parsed
}
func validateHostLength(host string) {
if len(host) > maxDomainLength {
panic(fmt.Sprintf("hostauthorization: host %q exceeds RFC 1035 maximum of %d characters (%d chars)",
host, maxDomainLength, len(host)))
}
// IPv6 hosts contain colons and aren't dotted labels.
if strings.IndexByte(host, ':') >= 0 {
return
}
for label := range strings.SplitSeq(host, ".") {
if len(label) > maxLabelLength {
panic(fmt.Sprintf("hostauthorization: host %q has label %q exceeding RFC 1035 limit of %d characters (%d chars)",
host, label, maxLabelLength, len(label)))
}
}
}
// normalizeHost strips port, trailing dot, and IPv6 brackets, lowercases,
// and converts IDN labels to Punycode (matching what browsers send).
func normalizeHost(host string) string {
// Fast path for plain hostnames — avoids net.SplitHostPort's error allocation.
if host != "" && host[0] != '[' && strings.IndexByte(host, ':') < 0 {
host = utils.TrimRight(host, '.')
host = utilsstrings.ToLower(host)
return toPunycode(host)
}
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
} else {
host = utils.TrimLeft(host, '[')
host = utils.TrimRight(host, ']')
}
host = utils.TrimRight(host, '.')
host = utilsstrings.ToLower(host)
return toPunycode(host)
}
func toPunycode(host string) string {
if host == "" || strings.IndexByte(host, ':') >= 0 || isASCII(host) {
return host
}
if ascii, err := idna.Lookup.ToASCII(host); err == nil {
return ascii
}
// Non-convertible input falls through; it won't match any Punycode entry,
// which is the correct security default.
return host
}
func parseNormalizedAuthority(authority string) (string, bool) {
authority = utils.TrimSpace(authority)
if authority == "" {
return "", false
}
host := authority
if authority[0] == '[' {
idx := -1
for i := 1; i < len(authority); i++ {
switch authority[i] {
case '@', '[':
return "", false
case ']':
idx = i
i = len(authority)
}
}
if idx <= 1 {
return "", false
}
host = authority[1:idx]
rest := authority[idx+1:]
if rest != "" {
if rest[0] != ':' {
return "", false
}
if !isValidPort(rest[1:]) {
return "", false
}
}
} else {
colonIdx := -1
for i := 0; i < len(authority); i++ {
switch authority[i] {
case '@', '[', ']':
return "", false
case ':':
if colonIdx != -1 {
return "", false
}
colonIdx = i
}
}
if colonIdx != -1 {
host = authority[:colonIdx]
if !isValidPort(authority[colonIdx+1:]) {
return "", false
}
}
}
host = normalizeHost(host)
if host == "" {
return "", false
}
return host, true
}
func isValidPort(raw string) bool {
if raw == "" || len(raw) > 5 {
return false
}
var port int
for i := 0; i < len(raw); i++ {
if raw[i] < '0' || raw[i] > '9' {
return false
}
port = port*10 + int(raw[i]-'0')
}
return port <= 65535
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= 0x80 {
return false
}
}
return true
}
// matchHost evaluates exact → wildcard → AllowedHostsFunc.
// The func is a fallback only — never called when a static rule matched.
func matchHost(host string, parsed parsedHosts, allowedHostsFunc func(string) bool) bool {
if _, ok := parsed.exact[host]; ok {
return true
}
for _, suffix := range parsed.wildcardSuffixes {
if strings.HasSuffix(host, suffix) {
return true
}
}
if allowedHostsFunc != nil && allowedHostsFunc(host) {
return true
}
return false
}
// New creates a new host authorization middleware handler.
func New(config ...Config) fiber.Handler {
cfg := configDefault(config...)
parsed := parseAllowedHosts(cfg.AllowedHosts)
return func(c fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
host, ok := parseNormalizedAuthority(c.Host())
if !ok {
return cfg.ErrorHandler(c, ErrForbiddenHost)
}
if matchHost(host, parsed, cfg.AllowedHostsFunc) {
return c.Next()
}
return cfg.ErrorHandler(c, ErrForbiddenHost)
}
}