Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow configure HTTPVirtualHosts and HTTPCors settings #1236

Merged
merged 1 commit into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) {
MaxPeers: config.MaxPeers,
MaxPendingPeers: config.MaxPendingPeers,
},
HTTPCors: nil,
HTTPModules: config.FormatAPIModules(),
HTTPVirtualHosts: []string{"localhost"},
HTTPModules: config.FormatAPIModules(),
}

if config.IPCEnabled {
Expand All @@ -144,6 +142,8 @@ func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) {
if config.HTTPEnabled {
nc.HTTPHost = config.HTTPHost
nc.HTTPPort = config.HTTPPort
nc.HTTPVirtualHosts = config.HTTPVirtualHosts
nc.HTTPCors = config.HTTPCors
}

if config.ClusterConfig.Enabled {
Expand Down
14 changes: 14 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/status-im/status-go/params"
. "github.com/status-im/status-go/t/utils"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb"
Expand Down Expand Up @@ -68,3 +69,16 @@ func TestParseNodesToNodeID(t *testing.T) {
require.Len(t, nodeIDs, 1)
require.Equal(t, discover.NodeID{1}, nodeIDs[0])
}

func TestNewGethNodeConfig(t *testing.T) {
config, err := params.NewNodeConfig("", params.RopstenNetworkID)
require.NoError(t, err)
config.HTTPEnabled = true
config.HTTPVirtualHosts = []string{"my.domain.com"}
config.HTTPCors = []string{"http://my.domain.com"}

nc, err := newGethNodeConfig(config)
require.NoError(t, err)
require.Equal(t, []string{"my.domain.com"}, nc.HTTPVirtualHosts)
require.Equal(t, []string{"http://my.domain.com"}, nc.HTTPCors)
}
15 changes: 15 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ type NodeConfig struct {
// HTTPPort is the TCP port number on which to start the Geth's HTTP RPC server.
HTTPPort int

// HTTPVirtualHosts is the list of virtual hostnames which are allowed on incoming requests.
// This is by default {'localhost'}. Using this prevents attacks like
// DNS rebinding, which bypasses SOP by simply masquerading as being within the same
// origin. These attacks do not utilize CORS, since they are not cross-domain.
// By explicitly checking the Host-header, the server will not allow requests
// made against the server with a malicious host domain.
// Requests using an IP address directly are not affected.
HTTPVirtualHosts []string

// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
// clients. Please be aware that CORS is a browser enforced security, it's fully
// useless for custom HTTP clients.
HTTPCors []string

// IPCEnabled specifies whether IPC-RPC Server is enabled or not
IPCEnabled bool

Expand Down Expand Up @@ -399,6 +413,7 @@ func NewNodeConfig(dataDir string, networkID uint64) (*NodeConfig, error) {
Version: Version,
HTTPHost: "localhost",
HTTPPort: 8545,
HTTPVirtualHosts: []string{"localhost"},
ListenAddr: ":0",
APIModules: "eth,net,web3,peer",
MaxPeers: 25,
Expand Down
28 changes: 28 additions & 0 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,34 @@ func TestNodeConfigValidate(t *testing.T) {
}`,
Error: "PFSEnabled is true, but InstallationID is empty",
},
{
Name: "Default HTTP virtual hosts is localhost and CORS is empty",
Config: `{
"NetworkId": 1,
"DataDir": "/some/dir",
"KeyStoreDir": "/some/dir",
"BackupDisabledDataDir": "/some/dir"
}`,
CheckFunc: func(t *testing.T, config *params.NodeConfig) {
require.Equal(t, []string{"localhost"}, config.HTTPVirtualHosts)
require.Nil(t, config.HTTPCors)
},
},
{
Name: "Set HTTP virtual hosts and CORS",
Config: `{
"NetworkId": 1,
"DataDir": "/some/dir",
"KeyStoreDir": "/some/dir",
"BackupDisabledDataDir": "/some/dir",
"HTTPVirtualHosts": ["my.domain.com"],
"HTTPCors": ["http://my.domain.com:8080"]
}`,
CheckFunc: func(t *testing.T, config *params.NodeConfig) {
require.Equal(t, []string{"my.domain.com"}, config.HTTPVirtualHosts)
require.Equal(t, []string{"http://my.domain.com:8080"}, config.HTTPCors)
},
},
}

for _, tc := range testCases {
Expand Down