Skip to content

Commit

Permalink
Fixes DNS rebinding vulnerability in router by explicitly passing Hos…
Browse files Browse the repository at this point in the history
…t parameter based off config listenaddress (thrasher-corp#209)
  • Loading branch information
xtda authored and thrasher- committed Apr 4, 2019
1 parent ca55f2f commit 6c8f8ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
5 changes: 4 additions & 1 deletion restful_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gorilla/mux"
"github.com/thrasher-/gocryptotrader/common"
log "github.com/thrasher-/gocryptotrader/logger"

_ "net/http/pprof"
Expand Down Expand Up @@ -45,6 +46,7 @@ var routes = Routes{}
// router
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
listenAddr := bot.config.Webserver.ListenAddress

routes = Routes{
Route{
Expand Down Expand Up @@ -114,7 +116,8 @@ func NewRouter() *mux.Router {
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(RESTLogger(route.HandlerFunc, route.Name))
Handler(RESTLogger(route.HandlerFunc, route.Name)).
Host(common.ExtractHost(listenAddr))
}

if bot.config.Profiler.Enabled {
Expand Down
31 changes: 31 additions & 0 deletions restful_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
)

Expand Down Expand Up @@ -51,3 +52,33 @@ func TestConfigAllJsonResponse(t *testing.T) {
t.Error("Test failed. Json not equal to config")
}
}

func TestInvalidHostRequest(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/config/all", nil)
if err != nil {
t.Fatal(err)
}
req.Host = "invalidsite.com"

resp := httptest.NewRecorder()
NewRouter().ServeHTTP(resp, req)

if status := resp.Code; status != http.StatusNotFound {
t.Errorf("Test failed. Response returned wrong status code expected %v got %v", http.StatusNotFound, status)
}
}

func TestValidHostRequest(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/config/all", nil)
if err != nil {
t.Fatal(err)
}
req.Host = common.ExtractHost(bot.config.Webserver.ListenAddress)

resp := httptest.NewRecorder()
NewRouter().ServeHTTP(resp, req)

if status := resp.Code; status != http.StatusOK {
t.Errorf("Test failed. Response returned wrong status code expected %v got %v", http.StatusOK, status)
}
}

0 comments on commit 6c8f8ff

Please sign in to comment.