From 0d772b9f094285227c5d89fb61cb57e206e94199 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 8 Aug 2023 13:35:58 +0200 Subject: [PATCH] graphql: avoid greedy allocation (#27873) Fixes a graphql-dos --------- Co-authored-by: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Co-authored-by: Sina Mahmoodi --- graphql/graphql.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/graphql/graphql.go b/graphql/graphql.go index 3a3e7db865d5..b407ab425f18 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -1250,7 +1250,7 @@ func (r *Resolver) Blocks(ctx context.Context, args struct { if to < from { return []*Block{}, nil } - ret := make([]*Block, 0, to-from+1) + var ret []*Block for i := from; i <= to; i++ { numberOrHash := rpc.BlockNumberOrHashWithNumber(i) block := &Block{ @@ -1268,6 +1268,9 @@ func (r *Resolver) Blocks(ctx context.Context, args struct { break } ret = append(ret, block) + if err := ctx.Err(); err != nil { + return nil, err + } } return ret, nil }