Skip to content

Commit a903912

Browse files
fjlmeowsbits
andauthored
rpc: check module availability at startup (#20597)
Fixes #20467 Co-authored-by: meowsbits <[email protected]>
1 parent 44c365c commit a903912

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

cmd/geth/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func defaultNodeConfig() node.Config {
9999
cfg := node.DefaultConfig
100100
cfg.Name = clientIdentifier
101101
cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
102-
cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
103-
cfg.WSModules = append(cfg.WSModules, "eth", "shh")
102+
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
103+
cfg.WSModules = append(cfg.WSModules, "eth")
104104
cfg.IPCPath = "geth.ipc"
105105
return cfg
106106
}

rpc/endpoints.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,39 @@
1717
package rpc
1818

1919
import (
20+
"fmt"
2021
"net"
2122

2223
"github.com/ethereum/go-ethereum/log"
2324
)
2425

25-
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
26+
// checkModuleAvailability check that all names given in modules are actually
27+
// available API services.
28+
func checkModuleAvailability(modules []string, apis []API) error {
29+
available := make(map[string]struct{})
30+
var availableNames string
31+
for i, api := range apis {
32+
if _, ok := available[api.Namespace]; !ok {
33+
available[api.Namespace] = struct{}{}
34+
if i > 0 {
35+
availableNames += ", "
36+
}
37+
availableNames += api.Namespace
38+
}
39+
}
40+
for _, name := range modules {
41+
if _, ok := available[name]; !ok {
42+
return fmt.Errorf("invalid API %q in whitelist (available: %s)", name, availableNames)
43+
}
44+
}
45+
return nil
46+
}
47+
48+
// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules.
2649
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
50+
if err := checkModuleAvailability(modules, apis); err != nil {
51+
return nil, nil, err
52+
}
2753
// Generate the whitelist based on the allowed modules
2854
whitelist := make(map[string]bool)
2955
for _, module := range modules {
@@ -51,9 +77,11 @@ func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []str
5177
return listener, handler, err
5278
}
5379

54-
// StartWSEndpoint starts a websocket endpoint
80+
// StartWSEndpoint starts a websocket endpoint.
5581
func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []string, exposeAll bool) (net.Listener, *Server, error) {
56-
82+
if err := checkModuleAvailability(modules, apis); err != nil {
83+
return nil, nil, err
84+
}
5785
// Generate the whitelist based on the allowed modules
5886
whitelist := make(map[string]bool)
5987
for _, module := range modules {

0 commit comments

Comments
 (0)