Skip to content

Commit

Permalink
Allow introspection by default in production mode
Browse files Browse the repository at this point in the history
Adds env.php parameter for disabling introspection:
```php
...
    'graphql' => [
        'disable_introspection' => true,
    ],
...
```

Fixes magento#232
  • Loading branch information
pmclain committed Dec 31, 2018
1 parent cd1bcb6 commit 410b012
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
class IntrospectionQueryTest extends GraphQlAbstract
{
/**
* Tests that Introspection is disabled when not in developer mode
* Tests that Introspection is allowed by default
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testIntrospectionQueryWithFieldArgs()
public function testIntrospectionQuery()
{
$query
= <<<QUERY
Expand Down Expand Up @@ -54,11 +54,6 @@ public function testIntrospectionQueryWithFieldArgs()
}
QUERY;

$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'GraphQL response contains errors: GraphQL introspection is not allowed, but ' .
'the query contained __schema or __type'
);
$this->graphQlQuery($query);
$this->assertArrayHasKey('__schema', $this->graphQlQuery($query));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Query;

use Magento\Framework\App\DeploymentConfig;

/**
* Class for fetching the availability of introspection queries
*/
class IntrospectionConfiguration
{
const CONFIG_PATH_DISABLE_INTROSPECTION = 'graphql/disable_introspection';

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @param DeploymentConfig $deploymentConfig
*/
public function __construct(
DeploymentConfig $deploymentConfig
) {
$this->deploymentConfig = $deploymentConfig;
}

/**
* Check the the environment config to determine if introspection should be disabled.
*
* @return int
*/
public function disableIntrospection(): int
{
return (int) $this->deploymentConfig->get(self::CONFIG_PATH_DISABLE_INTROSPECTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryDepth;
use GraphQL\Validator\Rules\QueryComplexity;
use Magento\Framework\App\ObjectManager;

/**
* QueryComplexityLimiter
Expand All @@ -33,16 +34,25 @@ class QueryComplexityLimiter
*/
private $queryComplexity;

/**
* @var IntrospectionConfiguration
*/
private $introspectionConfig;

/**
* @param int $queryDepth
* @param int $queryComplexity
* @param IntrospectionConfiguration $introspectionConfig
*/
public function __construct(
int $queryDepth,
int $queryComplexity
int $queryComplexity,
IntrospectionConfiguration $introspectionConfig = null
) {
$this->queryDepth = $queryDepth;
$this->queryComplexity = $queryComplexity;
$this->introspectionConfig = $introspectionConfig ?? ObjectManager::getInstance()
->get(IntrospectionConfiguration::class);
}

/**
Expand All @@ -53,7 +63,7 @@ public function __construct(
public function execute(): void
{
DocumentValidator::addRule(new QueryComplexity($this->queryComplexity));
DocumentValidator::addRule(new DisableIntrospection());
DocumentValidator::addRule(new DisableIntrospection($this->introspectionConfig->disableIntrospection()));
DocumentValidator::addRule(new QueryDepth($this->queryDepth));
}
}

0 comments on commit 410b012

Please sign in to comment.