Skip to content

Commit

Permalink
Validate the Max Query Complexity Value (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisaruGuruge authored Jul 31, 2024
1 parent ebd1099 commit 98591fc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ballerina/engine.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ isolated class Engine {
if maxQueryDepth is int && maxQueryDepth < 1 {
return error Error("Max query depth value must be a positive integer");
}
if queryComplexityConfig is QueryComplexityConfig && queryComplexityConfig.maxComplexity < 0 {
return error Error("Max complexity value must be greater than zero");
}
self.maxQueryDepth = maxQueryDepth;
self.schema = check createSchema(schemaString);
self.interceptors = interceptors;
Expand Down
24 changes: 19 additions & 5 deletions ballerina/tests/06_configurations_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ function dataProviderGraphiQLPath() returns (string[][]) {
@test:Config {
groups: ["listener", "configs"]
}
function testInvalidMaxQueryDepth() returns error? {
Engine|Error engine = new ("", 0, testService, [], true, true);
test:assertTrue(engine is Error);
Error err = <Error>engine;
test:assertEquals(err.message(), "Max query depth value must be a positive integer");
isolated function testInvalidMaxQueryDepth() returns error? {
lock {
Engine|Error engine = new ("", 0, testService, [], true, true);
test:assertTrue(engine is Error);
Error err = <Error>engine;
test:assertEquals(err.message(), "Max query depth value must be a positive integer");
}
}

@test:Config {
Expand Down Expand Up @@ -126,3 +128,15 @@ function dataProviderGraphiqlPathLog() returns map<[(int|http:Listener), Listene
"4": [9093, {secureSocket}, "https://localhost:9093", "wss://localhost:9093"]
};
}

@test:Config {
groups: ["configs", "validation", "query_complexity"]
}
isolated function testInvalidMaxQueryComplexity() returns error? {
lock {
Engine|Error engine = new ("", 10, testService, [], true, true, queryComplexityConfig = {maxComplexity: -1});
test:assertTrue(engine is Error);
Error err = <Error>engine;
test:assertEquals(err.message(), "Max complexity value must be greater than zero");
}
}
2 changes: 1 addition & 1 deletion ballerina/tests/service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// specific language governing permissions and limitations
// under the License.

Service testService = service object {
final isolated Service testService = isolated service object {
isolated resource function get greet() returns string {
return "Hello";
}
Expand Down
2 changes: 1 addition & 1 deletion docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -4163,7 +4163,7 @@ Due to [a limitation](https://github.com/ballerina-platform/ballerina-lang/issue

The introspection queries will have the `defaultFieldComplexity` per each field. This cannot be overridden.

> **Note:** When the maximum query complexity value is set to a lower value, tools such as GraphiQL may fail to generate the schema from the service due the introspection query complexity exceeding the maximum query complexity value. The complexity value of the introspection query from the GrapihQL client is 23 (assuming the default field complexity value is 1). In such cases, the testings can be done by either increasing the threshold value or using the `warnOnly` mode.
> **Note:** When the maximum query complexity value is set to a lower value, tools such as GraphiQL may fail to generate the schema from the service due the introspection query complexity exceeding the maximum query complexity value. The complexity value of the introspection query from the GraphiQL client is 23 (assuming the default field complexity value is 1). In such cases, the testings can be done by either increasing the threshold value or using the `warnOnly` mode.
##### 10.9.1.3 Response for Invalid Document with Exceeding Max Query Complexity

Expand Down

0 comments on commit 98591fc

Please sign in to comment.