forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallowed_hosts.go
76 lines (62 loc) · 1.77 KB
/
allowed_hosts.go
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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package server
import (
"net"
"net/http"
"strings"
"github.com/ava-labs/avalanchego/utils/set"
)
const wildcard = "*"
var _ http.Handler = (*allowedHostsHandler)(nil)
func filterInvalidHosts(
handler http.Handler,
allowed []string,
) http.Handler {
s := set.Set[string]{}
for _, host := range allowed {
if host == wildcard {
// wildcards match all hostnames, so just return the base handler
return handler
}
s.Add(strings.ToLower(host))
}
return &allowedHostsHandler{
handler: handler,
hosts: s,
}
}
// allowedHostsHandler is an implementation of http.Handler that validates the
// http host header of incoming requests. This can prevent DNS rebinding attacks
// which do not utilize CORS-headers. Http request host headers are validated
// against a whitelist to determine whether the request should be dropped or
// not.
type allowedHostsHandler struct {
handler http.Handler
hosts set.Set[string]
}
func (a *allowedHostsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// if the host header is missing we can serve this request because dns
// rebinding attacks rely on this header
if r.Host == "" {
a.handler.ServeHTTP(w, r)
return
}
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
// either invalid (too many colons) or no port specified
host = r.Host
}
if ipAddr := net.ParseIP(host); ipAddr != nil {
// accept requests from ips
a.handler.ServeHTTP(w, r)
return
}
// a specific hostname - we need to check the whitelist to see if we should
// accept this r
if a.hosts.Contains(strings.ToLower(host)) {
a.handler.ServeHTTP(w, r)
return
}
http.Error(w, "invalid host specified", http.StatusForbidden)
}