Skip to content

Commit

Permalink
Add max allowed query size (#2813)
Browse files Browse the repository at this point in the history
* add max allowed query size
* address comment
  • Loading branch information
nevermore3 authored Sep 10, 2021
1 parent c1be163 commit c334437
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions conf/nebula-graphd.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
# Whether to treat partial success as an error.
# This flag is only used for Read-only access, and Modify access always treats partial success as an error.
--accept_partial_success=false
# Maximum sentence length, unit byte
--max_allowed_query_size=4194304

########## networking ##########
# Comma separated Meta Server Addresses
Expand Down
2 changes: 2 additions & 0 deletions conf/nebula-graphd.conf.production
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
# Whether to treat partial success as an error.
# This flag is only used for Read-only access, and Modify access always treats partial success as an error.
--accept_partial_success=false
# Maximum sentence length, unit byte
--max_allowed_query_size=4194304

########## networking ##########
# Comma separated Meta Server Addresses
Expand Down
1 change: 1 addition & 0 deletions src/graph/service/GraphFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>::max(),
Expand Down
21 changes: 21 additions & 0 deletions src/graph/validator/test/QueryValidatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()), "SyntaxError: Query is too large (282 > 256).");
FLAGS_max_allowed_query_size = 4194304;
}

TEST_F(QueryValidatorTest, TestMatch) {
{
std::string query =
Expand Down
9 changes: 7 additions & 2 deletions src/parser/GQLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "parser/GraphParser.hpp"
#include "parser/GraphScanner.h"

DECLARE_uint32(max_allowed_query_size);
namespace nebula {

class GQLParser {
Expand Down Expand Up @@ -39,8 +40,12 @@ class GQLParser {
}

StatusOr<std::unique_ptr<Sentence>> parse(std::string query) {
// Since GraphScanner needs a writable buffer, we have to copy the query
// string
// Since GraphScanner needs a writable buffer, we have to copy the query string
size_t querySize = query.size();
size_t maxAllowedQuerySize = static_cast<size_t>(FLAGS_max_allowed_query_size);
if (querySize > maxAllowedQuerySize) {
return Status::SyntaxError("Query is too large (%ld > %ld).", querySize, maxAllowedQuerySize);
}
buffer_ = std::move(query);
pos_ = &buffer_[0];
end_ = pos_ + buffer_.size();
Expand Down

0 comments on commit c334437

Please sign in to comment.