Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: limit the size of query results (#6367)

rebeliced4b96b781d90
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 3d8fc468c7..c6da294f55 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -14,6 +14,7 @@ import (
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
+ "google.golang.org/protobuf/proto"
"github.com/google/cel-go/cel"
"github.com/labstack/echo/v4"
@@ -34,6 +35,12 @@ import (
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
+const (
+ // The maximum number of bytes for sql results in response body.
+ // 10 MB.
+ maximumSQLResultSize = 10 * 1024 * 1024
+)
+
// SQLService is the service for SQL.
type SQLService struct {
v1pb.UnimplementedSQLServiceServer
@@ -102,10 +109,21 @@ func (s *SQLService) Query(ctx context.Context, request *v1pb.QueryRequest) (*v1
return nil, queryErr
}
- return &v1pb.QueryResponse{
+ response := &v1pb.QueryResponse{
Results: results,
- Advices: adviceList,
- }, nil
+ }
+
+ if proto.Size(response) > maximumSQLResultSize {
+ response.Results = []*v1pb.QueryResult{
+ {
+ Error: fmt.Sprintf("Output of query exceeds max allowed output size of %dMB", maximumSQLResultSize/1024/1024),
+ },
+ }
+ }
+
+ response.Advices = adviceList
+
+ return response, nil
}
// postQuery does the following: