Skip to content

Commit 6820a6d

Browse files
authored
Merge pull request #176 from edefimov/add_env_support
#172 Support environment variables for max depth and query complexity
2 parents 3da337e + b725b55 commit 6820a6d

File tree

7 files changed

+202
-3
lines changed

7 files changed

+202
-3
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ private function addBuilderSection($name)
198198
private function addSecurityQuerySection($name, $disabledValue)
199199
{
200200
$builder = new TreeBuilder();
201-
$node = $builder->root($name, 'integer');
201+
$node = $builder->root($name, 'scalar');
202202
$node->beforeNormalization()
203203
->ifTrue(function ($v) {
204204
return is_string($v) && is_numeric($v);
205205
})
206206
->then(function ($v) {
207-
return intval($v);
207+
return (int) $v;
208208
})
209209
->end();
210210

@@ -221,7 +221,7 @@ private function addSecurityQuerySection($name, $disabledValue)
221221
->defaultFalse()
222222
->validate()
223223
->ifTrue(function ($v) {
224-
return $v < 0;
224+
return is_int($v) && $v < 0;
225225
})
226226
->thenInvalid('"overblog_graphql.security.'.$name.'" must be greater or equal to 0.')
227227
->end()

Tests/Functional/Security/QueryComplexityTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ public function testComplexityReachLimitation()
5656
$this->assertResponse($this->userFriendsWithoutLimitQuery, $expected, self::ANONYMOUS_USER, 'queryComplexity');
5757
}
5858

59+
public function testComplexityReachLimitationEnv()
60+
{
61+
$expected = [
62+
'errors' => [
63+
[
64+
'message' => 'Max query complexity should be 10 but got 54.',
65+
],
66+
],
67+
];
68+
69+
$this->assertResponse($this->userFriendsWithoutLimitQuery, $expected, self::ANONYMOUS_USER, 'queryComplexityEnv');
70+
}
71+
5972
public function testComplexityUnderLimitation()
6073
{
6174
$expected = [
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the OverblogGraphQLBundle package.
5+
*
6+
* (c) Overblog <http://github.com/overblog/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Overblog\GraphQLBundle\Tests\Functional\Security;
13+
14+
use Overblog\GraphQLBundle\Tests\Functional\TestCase;
15+
16+
class QueryMaxDepthTest extends TestCase
17+
{
18+
private $userFriendsWithoutViolationQuery = <<<'EOF'
19+
query {
20+
user {
21+
friends(first:1) {
22+
edges {
23+
node {
24+
name
25+
}
26+
}
27+
}
28+
}
29+
}
30+
EOF;
31+
32+
private $userFriendsWithViolationQuery = <<<'EOF'
33+
query {
34+
user {
35+
friends(first: 1) {
36+
edges {
37+
node {
38+
name
39+
friends {
40+
edges {
41+
node {
42+
name
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
EOF;
52+
53+
public function testMaxDepthReachLimitation()
54+
{
55+
$expected = [
56+
'errors' => [
57+
[
58+
'message' => 'Max query depth should be 3 but got 6.',
59+
],
60+
],
61+
];
62+
63+
$this->assertResponse($this->userFriendsWithViolationQuery, $expected, self::ANONYMOUS_USER, 'queryMaxDepth');
64+
}
65+
66+
public function testMaxDepthReachLimitationEnv()
67+
{
68+
$expected = [
69+
'errors' => [
70+
[
71+
'message' => 'Max query depth should be 3 but got 6.',
72+
],
73+
],
74+
];
75+
76+
$this->assertResponse($this->userFriendsWithViolationQuery, $expected, self::ANONYMOUS_USER, 'queryMaxDepthEnv');
77+
}
78+
79+
public function testComplexityUnderLimitation()
80+
{
81+
$expected = [
82+
'data' => [
83+
'user' => [
84+
'friends' => [
85+
'edges' => [
86+
['node' => ['name' => 'Nick']],
87+
],
88+
],
89+
],
90+
],
91+
];
92+
93+
$this->assertResponse($this->userFriendsWithoutViolationQuery, $expected, self::ANONYMOUS_USER, 'queryMaxDepth');
94+
}
95+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
imports:
2+
- { resource: ../config.yml }
3+
- { resource: ../connection/services.yml }
4+
5+
parameters:
6+
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\QueryComplexity\\__DEFINITIONS__"
7+
env(GRAPHQL_QUERY_MAX_COMPLEXITY): 10
8+
9+
overblog_graphql:
10+
security:
11+
query_max_complexity: '%env(GRAPHQL_QUERY_MAX_COMPLEXITY)%'
12+
definitions:
13+
schema:
14+
query: Query
15+
mutation: ~
16+
mappings:
17+
types:
18+
-
19+
type: yml
20+
dir: "%kernel.root_dir%/config/queryComplexity/mapping"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
imports:
2+
- { resource: ../config.yml }
3+
- { resource: ../connection/services.yml }
4+
5+
parameters:
6+
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\QueryComplexity\\__DEFINITIONS__"
7+
8+
overblog_graphql:
9+
security:
10+
query_max_depth: '3'
11+
definitions:
12+
schema:
13+
query: Query
14+
mutation: ~
15+
mappings:
16+
types:
17+
-
18+
type: yml
19+
dir: "%kernel.root_dir%/config/queryMaxDepth/mapping"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Query:
2+
type: object
3+
config:
4+
fields:
5+
user:
6+
type: User
7+
resolve: '@=resolver("query")'
8+
9+
User:
10+
type: object
11+
config:
12+
fields:
13+
name:
14+
type: String
15+
friends:
16+
type: friendConnection
17+
argsBuilder: "Relay::Connection"
18+
resolve: '@=resolver("friends", [value, args])'
19+
20+
friendConnection:
21+
type: relay-connection
22+
config:
23+
nodeType: User
24+
resolveNode: '@=resolver("node", [value])'
25+
edgeFields:
26+
friendshipTime:
27+
type: String
28+
resolve: "Yesterday"
29+
connectionFields:
30+
totalCount:
31+
type: Int
32+
resolve: '@=resolver("connection")'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
imports:
2+
- { resource: ../config.yml }
3+
- { resource: ../connection/services.yml }
4+
5+
parameters:
6+
overblog_graphql.type_class_namespace: "Overblog\\GraphQLBundle\\QueryComplexity\\__DEFINITIONS__"
7+
env(GRAPHQL_QUERY_MAX_DEPTH): 3
8+
9+
overblog_graphql:
10+
security:
11+
query_max_depth: '%env(GRAPHQL_QUERY_MAX_DEPTH)%'
12+
definitions:
13+
schema:
14+
query: Query
15+
mutation: ~
16+
mappings:
17+
types:
18+
-
19+
type: yml
20+
dir: "%kernel.root_dir%/config/queryMaxDepth/mapping"

0 commit comments

Comments
 (0)