diff --git a/src/graph/service/GraphFlags.cpp b/src/graph/service/GraphFlags.cpp index 08e3164ae75..20e60daba8c 100644 --- a/src/graph/service/GraphFlags.cpp +++ b/src/graph/service/GraphFlags.cpp @@ -45,6 +45,7 @@ DEFINE_string(auth_type, DEFINE_string(cloud_http_url, "", "cloud http url including ip, port, url path"); DEFINE_uint32(max_allowed_statements, 512, "Max allowed sequential statements"); +DEFINE_uint32(max_allowed_query_size, 4194304, "Max allowed sequential query size"); DEFINE_int64(max_allowed_connections, std::numeric_limits::max(), diff --git a/src/graph/validator/SequentialValidator.cpp b/src/graph/validator/SequentialValidator.cpp index 61c12573df7..a31ad8b4de0 100644 --- a/src/graph/validator/SequentialValidator.cpp +++ b/src/graph/validator/SequentialValidator.cpp @@ -13,6 +13,7 @@ #include "graph/service/PermissionCheck.h" DECLARE_uint32(max_allowed_statements); +DECLARE_uint32(max_allowed_query_size); namespace nebula { namespace graph { @@ -24,6 +25,11 @@ Status SequentialValidator::validateImpl() { "given.", static_cast(sentence_->kind())); } + size_t querySize = sentence_->toString().size(); + size_t maxAllowedQuerySize = static_cast(FLAGS_max_allowed_query_size); + if (querySize > maxAllowedQuerySize) { + return Status::SemanticError("Query is too large (%ld > %ld).", querySize, maxAllowedQuerySize); + } auto seqSentence = static_cast(sentence_); auto sentences = seqSentence->sentences(); diff --git a/src/graph/validator/test/QueryValidatorTest.cpp b/src/graph/validator/test/QueryValidatorTest.cpp index 8593b17f55e..05463568e2b 100644 --- a/src/graph/validator/test/QueryValidatorTest.cpp +++ b/src/graph/validator/test/QueryValidatorTest.cpp @@ -8,6 +8,7 @@ #include "graph/validator/test/ValidatorTestBase.h" DECLARE_uint32(max_allowed_statements); +DECLARE_uint32(max_allowed_query_size); namespace nebula { namespace graph { @@ -1118,6 +1119,26 @@ TEST_F(QueryValidatorTest, TestMaxAllowedStatements) { "exceeded"); } +TEST_F(QueryValidatorTest, TestMaxAllowedQuerySize) { + FLAGS_max_allowed_query_size = 256; + std::string query = "INSERT VERTEX person(name, age) VALUES "; + std::string value = "\"person_1\":(\"person_1\", 1),"; + int count = (FLAGS_max_allowed_query_size - query.size()) / value.size(); + std::string values; + values.reserve(FLAGS_max_allowed_query_size); + for (int i = 0; i < count; ++i) { + values.append(value); + } + values.erase(values.size() - 1); + query += values; + EXPECT_TRUE(checkResult(query)); + query.append(",\"person_2\":(\"person_2\", 2);"); + auto result = checkResult(query); + EXPECT_FALSE(result); + EXPECT_EQ(std::string(result.message()), "SemanticError: Query is too large (271 > 256)."); + FLAGS_max_allowed_query_size = 4194304; +} + TEST_F(QueryValidatorTest, TestMatch) { { std::string query =