Skip to content

Add config option to disable introspection query #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Overblog\GraphQLBundle\DependencyInjection;

use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Overblog\GraphQLBundle\Error\ErrorHandler;
Expand Down Expand Up @@ -166,6 +167,7 @@ private function securitySection()
->children()
->append($this->securityQuerySection('query_max_depth', QueryDepth::DISABLED))
->append($this->securityQuerySection('query_max_complexity', QueryComplexity::DISABLED))
->append($this->securityQuerySection('disable_introspection', DisableIntrospection::DISABLED))
->booleanNode('handle_cors')->defaultFalse()->end()
->end()
->end();
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/OverblogGraphQLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ private function treatConfigs(array $configs, ContainerBuilder $container, $forc

private function setSecurity(array $config, ContainerBuilder $container)
{
if (!empty($config['security']['disable_introspection'])) {
$executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
$executorDefinition->addMethodCall('disableIntrospectionQuery');
}

foreach ($config['security'] as $key => $value) {
$container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
}
Expand Down
6 changes: 6 additions & 0 deletions Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Schema;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Overblog\GraphQLBundle\Event\Events;
Expand Down Expand Up @@ -113,6 +114,11 @@ public function setMaxQueryComplexity($maxQueryComplexity)
$queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
}

public function disableIntrospectionQuery()
{
DocumentValidator::addRule(new DisableIntrospection());
}

/**
* @param null|string $schemaName
* @param array $request
Expand Down
19 changes: 19 additions & 0 deletions Resources/doc/security/disable_introspection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Disable introspection
=====================

This bundle supports [webonyx/graphql-php validation rule to disable introspection queries](webonyx/graphql-php).
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix the url


Introspection is a mechanism for fetching schema structure. It is used by tools like GraphiQL for auto-completion, query validation, etc.

It means that anybody can get a full description of your schema by sending a special query containing meta fields __type and __schema.

If you are not planning to expose your API to the general public, it makes sense to disable this feature in production. By disabling, tools like GraphiQL won't work anymore.

```yaml
#app/config/config.yml
overblog_graphql:
security:
disable_introspection: '%kernel.debug%'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace by disable_introspection: false since this example will disable introspection only in debug mode (dev environment by example)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just renaming the config entry enable_introspection will ease the usage of %kernel.debug% parameter. What do you think of that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think that is probably the best solution. I will change this today or tomorrow and I will add the necessary change to have the introspection enabled by default to avoid any BC issues.

```

Introspection is enabled by default.
1 change: 1 addition & 0 deletions Resources/doc/security/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Security
* [Fields public control](fields-public-control.md)
* [Limiting query depth](limiting-query-depth.md)
* [Query complexity analysis](query-complexity-analysis.md)
* [Disable introspection](disable_introspection.md)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add this entry to main readme file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do


Next step [handling errors](../error-handling/index.md)
17 changes: 17 additions & 0 deletions Tests/Functional/App/config/disableIntrospection/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
imports:
- { resource: ../config.yml }
- { resource: ../connection/services.yml }

overblog_graphql:
security:
disable_introspection: true
definitions:
class_namespace: "Overblog\\GraphQLBundle\\QueryComplexity\\__DEFINITIONS__"
schema:
query: Query
mutation: ~
mappings:
types:
-
type: yaml
dir: "%kernel.root_dir%/config/queryComplexity/mapping"
39 changes: 39 additions & 0 deletions Tests/Functional/Security/DisableIntrospectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Overblog\GraphQLBundle\Tests\Functional\Security;

use Overblog\GraphQLBundle\Tests\Functional\TestCase;

class DisableIntrospectionTest extends TestCase
{
private $introspectionQuery = <<<'EOF'
query {
__schema {
types {
name
description
}
}
}
EOF;

public function testIntrospectionDisabled()
{
$expected = [
'errors' => [
[
'message' => 'GraphQL introspection is not allowed, but the query contained __schema or __type',
'category' => 'graphql',
'locations' => [
[
'line' => 2,
'column' => 3,
],
],
],
],
];

$this->assertResponse($this->introspectionQuery, $expected, self::ANONYMOUS_USER, 'disableIntrospection');
}
}