forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add allowed http hosts configuration (ava-labs#1566)
- Loading branch information
1 parent
8fb8afe
commit bfaa7f7
Showing
8 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (C) 2019-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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAllowedHostsHandler_ServeHTTP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
allowed []string | ||
host string | ||
serve bool | ||
}{ | ||
{ | ||
name: "no host header", | ||
allowed: []string{"www.foobar.com"}, | ||
host: "", | ||
serve: true, | ||
}, | ||
{ | ||
name: "ip", | ||
allowed: []string{"www.foobar.com"}, | ||
host: "192.168.1.1", | ||
serve: true, | ||
}, | ||
{ | ||
name: "hostname not allowed", | ||
allowed: []string{"www.foobar.com"}, | ||
host: "www.evil.com", | ||
}, | ||
{ | ||
name: "hostname allowed", | ||
allowed: []string{"www.foobar.com"}, | ||
host: "www.foobar.com", | ||
serve: true, | ||
}, | ||
{ | ||
name: "wildcard", | ||
allowed: []string{"*"}, | ||
host: "www.foobar.com", | ||
serve: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
baseHandler := &testHandler{} | ||
|
||
httpAllowedHostsHandler := filterInvalidHosts( | ||
baseHandler, | ||
test.allowed, | ||
) | ||
|
||
w := &httptest.ResponseRecorder{} | ||
r := httptest.NewRequest("", "/", nil) | ||
r.Host = test.host | ||
|
||
httpAllowedHostsHandler.ServeHTTP(w, r) | ||
|
||
if test.serve { | ||
require.True(baseHandler.called) | ||
return | ||
} | ||
|
||
require.Equal(http.StatusForbidden, w.Code) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters