Skip to content

Commit

Permalink
Avoid logging debug info for invalid JSON-RPC methods
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Oct 8, 2024
1 parent 1f3f543 commit 0324b75
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
61 changes: 61 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,67 @@ import (

const maxFeeHistoryBlockCount = 1024

// A map containing all the valid method names that are found
// in the Ethereum JSON-RPC API specification.
// Update accordingly if any new methods are added/removed.
var validMethods = map[string]struct{}{
// eth namespace
"eth_blockNumber": {},
"eth_syncing": {},
"eth_sendRawTransaction": {},
"eth_getBalance": {},
"eth_getTransactionByHash": {},
"eth_getTransactionByBlockHashAndIndex": {},
"eth_getTransactionByBlockNumberAndIndex": {},
"eth_getTransactionReceipt": {},
"eth_getBlockByHash": {},
"eth_getBlockByNumber": {},
"eth_getBlockReceipts": {},
"eth_getBlockTransactionCountByHash": {},
"eth_getBlockTransactionCountByNumber": {},
"eth_call": {},
"eth_getLogs": {},
"eth_getTransactionCount": {},
"eth_estimateGas": {},
"eth_getCode": {},
"eth_feeHistory": {},
"eth_getStorageAt": {},
"eth_chainId": {},
"eth_coinbase": {},
"eth_gasPrice": {},
"eth_getUncleCountByBlockHash": {},
"eth_getUncleCountByBlockNumber": {},
"eth_getUncleByBlockHashAndIndex": {},
"eth_getUncleByBlockNumberAndIndex": {},
"eth_maxPriorityFeePerGas": {},
"eth_mining": {},
"eth_hashrate": {},
"eth_getProof": {},
"eth_createAccessList": {},

// web3 namespace
"web3_clientVersion": {},
"web3_sha3": {},

// net namespace
"net_listening": {},
"net_peerCount": {},
"net_version": {},

// txpool namespace
"txpool_content": {},
"txpool_contentFrom": {},
"txpool_status": {},
"txpool_inspect": {},
}

// Returns whether the given method name is a valid method from
// the Ethereum JSON-RPC API specification.
func IsValidMethod(methodName string) bool {
_, ok := validMethods[methodName]
return ok
}

var latestBlockNumberOrHash = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)

func SupportedAPIs(
Expand Down
26 changes: 18 additions & 8 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,16 +246,22 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

requestBody := make(map[string]any)
// Check if WebSocket request and serve if JSON-RPC over WebSocket is enabled
if b, err := io.ReadAll(r.Body); err == nil {
_ = json.Unmarshal(b, &requestBody)

h.logger.Debug().
Str("IP", r.RemoteAddr).
Str("url", r.URL.String()).
Fields(requestBody).
Bool("is-ws", isWebSocket(r)).
Msg("API request")
// Do not log any debug info for methods that are not valid
// JSON-RPC methods.
if methodValue, ok := requestBody["method"]; ok {
if methodStr, ok := methodValue.(string); ok && IsValidMethod(methodStr) {
h.logger.Debug().
Str("IP", r.RemoteAddr).
Str("url", r.URL.String()).
Fields(requestBody).
Bool("is-ws", isWebSocket(r)).
Msg("API request")

}
}

r.Body = io.NopCloser(bytes.NewBuffer(b))
r.Body.Close()
Expand All @@ -270,6 +276,7 @@ func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

ws := h.wsHandler
// Check if WebSocket request and serve if JSON-RPC over WebSocket is enabled
if ws != nil && isWebSocket(r) {
ws.ServeHTTP(w, r)
return
Expand Down Expand Up @@ -480,7 +487,10 @@ func (w *responseHandler) Write(data []byte) (int, error) {
if message.Error == nil {
r, _ := message.Result.MarshalJSON()
log.RawJSON("result", r).Msg("API response")
} else { // still debug output all errors even known ones
}

// still debug output all errors even known ones
if message.Error != nil && message.Error.Code != errMethodNotFound {
l.Debug().
Str("error", message.Error.Message).
Int("code", message.Error.Code).
Expand Down

0 comments on commit 0324b75

Please sign in to comment.