Skip to content

rpc: enforce rpc api module availability #20468

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

Closed
Closed
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
38 changes: 38 additions & 0 deletions rpc/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,49 @@
package rpc

import (
"fmt"
"net"

"github.com/ethereum/go-ethereum/log"
)

// checkModuleAvailable check that requested api modules (eg. via --rpcapi) are actually
// available API services. If an invalid module is given (ie API "foo" wanted which does not exist),
// then an error is returned including the invalid module and a list of available
// API service names.
func checkModuleAvailable(module string, apis []API) (err error) {
for _, api := range apis {
if module == api.Namespace {
return nil
}
}
// Module did not find a matching api namespace: this is an invalid module.
// Collect list of available modules for user debugging.
available := []string{}
outer:
for _, api := range apis {

// Only include unique api names
for _, av := range available {
if av == api.Namespace {
continue outer
}
}
available = append(available, api.Namespace)
}
return fmt.Errorf("invalid api module: module=%s available=%v", module, available)
}

// StartHTTPEndpoint starts the HTTP RPC endpoint, configured with cors/vhosts/modules
func StartHTTPEndpoint(endpoint string, apis []API, modules []string, cors []string, vhosts []string, timeouts HTTPTimeouts) (net.Listener, *Server, error) {
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {

// Ensure the requested module is actually available.
if err := checkModuleAvailable(module, apis); err != nil {
return nil, nil, err
}
whitelist[module] = true
}
// Register all the APIs exposed by the services
Expand Down Expand Up @@ -57,6 +90,11 @@ func StartWSEndpoint(endpoint string, apis []API, modules []string, wsOrigins []
// Generate the whitelist based on the allowed modules
whitelist := make(map[string]bool)
for _, module := range modules {

// Ensure the requested module is actually available.
if err := checkModuleAvailable(module, apis); err != nil {
return nil, nil, err
}
whitelist[module] = true
}
// Register all the APIs exposed by the services
Expand Down