Skip to content

Commit

Permalink
Merge pull request ethereum#17 from sei-protocol/yzang/SEI-6785
Browse files Browse the repository at this point in the history
Add method deny list in geth http server
  • Loading branch information
yzang2019 authored Mar 12, 2024
2 parents a4f97f7 + c27c86e commit db3a094
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Server struct {

mutex sync.Mutex
codecs map[ServerCodec]struct{}
denyList map[string]struct{}
run atomic.Bool
batchItemLimit int
batchResponseLimit int
Expand All @@ -56,8 +57,9 @@ type Server struct {
// NewServer creates a new server instance with no registered handlers.
func NewServer() *Server {
server := &Server{
idgen: randomIDGenerator(),
codecs: make(map[ServerCodec]struct{}),
idgen: randomIDGenerator(),
codecs: make(map[ServerCodec]struct{}),
denyList: make(map[string]struct{}),
}
server.run.Store(true)
// Register the default service providing meta information about the RPC service such
Expand Down Expand Up @@ -86,6 +88,12 @@ func (s *Server) RegisterName(name string, receiver interface{}) error {
return s.services.registerName(name, receiver)
}

// RegisterDenyList add given method name to the deny list so that RPC requests that matches
// any of the methods in deny list will got rejected directly.
func (s *Server) RegisterDenyList(methodName string) {
s.denyList[methodName] = struct{}{}
}

// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes
// the response back using the given codec. It will block until the codec is closed or the
// server is stopped. In either case the codec is closed.
Expand Down Expand Up @@ -141,13 +149,23 @@ func (s *Server) serveSingleRequest(ctx context.Context, codec ServerCodec) {
defer h.close(io.EOF, nil)

reqs, batch, err := codec.readBatch()

if err != nil {
if err != io.EOF {
resp := errorMessage(&invalidMessageError{"parse error"})
codec.writeJSON(ctx, resp, true)
}
return
}
// check deny list
for _, req := range reqs {
method := req.Method
if _, found := s.denyList[method]; found {
resp := errorMessage(&methodNotFoundError{method: method})
codec.writeJSON(ctx, resp, true)
return
}
}
if batch {
h.handleBatch(reqs)
} else {
Expand Down

0 comments on commit db3a094

Please sign in to comment.