Skip to content

Commit 410b012

Browse files
committed
Allow introspection by default in production mode
Adds env.php parameter for disabling introspection: ```php ... 'graphql' => [ 'disable_introspection' => true, ], ... ``` Fixes #232
1 parent cd1bcb6 commit 410b012

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/IntrospectionQueryTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
class IntrospectionQueryTest extends GraphQlAbstract
1313
{
1414
/**
15-
* Tests that Introspection is disabled when not in developer mode
15+
* Tests that Introspection is allowed by default
1616
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1717
*/
18-
public function testIntrospectionQueryWithFieldArgs()
18+
public function testIntrospectionQuery()
1919
{
2020
$query
2121
= <<<QUERY
@@ -54,11 +54,6 @@ public function testIntrospectionQueryWithFieldArgs()
5454
}
5555
QUERY;
5656

57-
$this->expectException(\Exception::class);
58-
$this->expectExceptionMessage(
59-
'GraphQL response contains errors: GraphQL introspection is not allowed, but ' .
60-
'the query contained __schema or __type'
61-
);
62-
$this->graphQlQuery($query);
57+
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
6358
}
6459
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Query;
9+
10+
use Magento\Framework\App\DeploymentConfig;
11+
12+
/**
13+
* Class for fetching the availability of introspection queries
14+
*/
15+
class IntrospectionConfiguration
16+
{
17+
const CONFIG_PATH_DISABLE_INTROSPECTION = 'graphql/disable_introspection';
18+
19+
/**
20+
* @var DeploymentConfig
21+
*/
22+
private $deploymentConfig;
23+
24+
/**
25+
* @param DeploymentConfig $deploymentConfig
26+
*/
27+
public function __construct(
28+
DeploymentConfig $deploymentConfig
29+
) {
30+
$this->deploymentConfig = $deploymentConfig;
31+
}
32+
33+
/**
34+
* Check the the environment config to determine if introspection should be disabled.
35+
*
36+
* @return int
37+
*/
38+
public function disableIntrospection(): int
39+
{
40+
return (int) $this->deploymentConfig->get(self::CONFIG_PATH_DISABLE_INTROSPECTION);
41+
}
42+
}

lib/internal/Magento/Framework/GraphQl/Query/QueryComplexityLimiter.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Validator\Rules\DisableIntrospection;
1212
use GraphQL\Validator\Rules\QueryDepth;
1313
use GraphQL\Validator\Rules\QueryComplexity;
14+
use Magento\Framework\App\ObjectManager;
1415

1516
/**
1617
* QueryComplexityLimiter
@@ -33,16 +34,25 @@ class QueryComplexityLimiter
3334
*/
3435
private $queryComplexity;
3536

37+
/**
38+
* @var IntrospectionConfiguration
39+
*/
40+
private $introspectionConfig;
41+
3642
/**
3743
* @param int $queryDepth
3844
* @param int $queryComplexity
45+
* @param IntrospectionConfiguration $introspectionConfig
3946
*/
4047
public function __construct(
4148
int $queryDepth,
42-
int $queryComplexity
49+
int $queryComplexity,
50+
IntrospectionConfiguration $introspectionConfig = null
4351
) {
4452
$this->queryDepth = $queryDepth;
4553
$this->queryComplexity = $queryComplexity;
54+
$this->introspectionConfig = $introspectionConfig ?? ObjectManager::getInstance()
55+
->get(IntrospectionConfiguration::class);
4656
}
4757

4858
/**
@@ -53,7 +63,7 @@ public function __construct(
5363
public function execute(): void
5464
{
5565
DocumentValidator::addRule(new QueryComplexity($this->queryComplexity));
56-
DocumentValidator::addRule(new DisableIntrospection());
66+
DocumentValidator::addRule(new DisableIntrospection($this->introspectionConfig->disableIntrospection()));
5767
DocumentValidator::addRule(new QueryDepth($this->queryDepth));
5868
}
5969
}

0 commit comments

Comments
 (0)