diff --git a/docs/execution/index.rst b/docs/execution/index.rst index f26259d3..dbfbfa72 100644 --- a/docs/execution/index.rst +++ b/docs/execution/index.rst @@ -10,4 +10,3 @@ Execution dataloader fileuploading subscriptions - queryvalidation diff --git a/docs/execution/queryvalidation.rst b/docs/execution/queryvalidation.rst deleted file mode 100644 index 02e29a35..00000000 --- a/docs/execution/queryvalidation.rst +++ /dev/null @@ -1,123 +0,0 @@ -Query Validation -================ -GraphQL uses query validators to check if Query AST is valid and can be executed. Every GraphQL server implements -standard query validators. For example, there is an validator that tests if queried field exists on queried type, that -makes query fail with "Cannot query field on type" error if it doesn't. - -To help with common use cases, graphene provides a few validation rules out of the box. - - -Depth limit Validator ---------------------- -The depth limit validator helps to prevent execution of malicious -queries. It takes in the following arguments. - -- ``max_depth`` is the maximum allowed depth for any operation in a GraphQL document. -- ``ignore`` Stops recursive depth checking based on a field name. Either a string or regexp to match the name, or a function that returns a boolean -- ``callback`` Called each time validation runs. Receives an Object which is a map of the depths for each operation. - -Usage ------ - -Here is how you would implement depth-limiting on your schema. - -.. code:: python - - from graphql import validate, parse - from graphene import ObjectType, Schema, String - from graphene.validation import depth_limit_validator - - - class MyQuery(ObjectType): - name = String(required=True) - - - schema = Schema(query=MyQuery) - - # queries which have a depth more than 20 - # will not be executed. - - validation_errors = validate( - schema=schema.graphql_schema, - document_ast=parse('THE QUERY'), - rules=( - depth_limit_validator( - max_depth=20 - ), - ) - ) - - -Disable Introspection ---------------------- -the disable introspection validation rule ensures that your schema cannot be introspected. -This is a useful security measure in production environments. - -Usage ------ - -Here is how you would disable introspection for your schema. - -.. code:: python - - from graphql import validate, parse - from graphene import ObjectType, Schema, String - from graphene.validation import DisableIntrospection - - - class MyQuery(ObjectType): - name = String(required=True) - - - schema = Schema(query=MyQuery) - - # introspection queries will not be executed. - - validation_errors = validate( - schema=schema.graphql_schema, - document_ast=parse('THE QUERY'), - rules=( - DisableIntrospection, - ) - ) - - -Implementing custom validators ------------------------------- -All custom query validators should extend the `ValidationRule `_ -base class importable from the graphql.validation.rules module. Query validators are visitor classes. They are -instantiated at the time of query validation with one required argument (context: ASTValidationContext). In order to -perform validation, your validator class should define one or more of enter_* and leave_* methods. For possible -enter/leave items as well as details on function documentation, please see contents of the visitor module. To make -validation fail, you should call validator's report_error method with the instance of GraphQLError describing failure -reason. Here is an example query validator that visits field definitions in GraphQL query and fails query validation -if any of those fields are blacklisted: - -.. code:: python - - from graphql import GraphQLError - from graphql.language import FieldNode - from graphql.validation import ValidationRule - - - my_blacklist = ( - "disallowed_field", - ) - - - def is_blacklisted_field(field_name: str): - return field_name.lower() in my_blacklist - - - class BlackListRule(ValidationRule): - def enter_field(self, node: FieldNode, *_args): - field_name = node.name.value - if not is_blacklisted_field(field_name): - return - - self.report_error( - GraphQLError( - f"Cannot query '{field_name}': field is blacklisted.", node, - ) - ) - diff --git a/docs/index.rst b/docs/index.rst index 05b7fd87..d0297d85 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -10,6 +10,7 @@ Contents: types/index execution/index relay/index + security/index testing/index api/index diff --git a/docs/security/customvalidation.rst b/docs/security/customvalidation.rst new file mode 100644 index 00000000..375f62f9 --- /dev/null +++ b/docs/security/customvalidation.rst @@ -0,0 +1,52 @@ +Implementing custom validators +============================== + +GraphQL uses query validators to check if Query AST is valid and can be executed. Every GraphQL server implements standard query validators. + +For example, a validator tests if a queried field exists on queried type, making the query fail with a "Cannot query field on type" error if it does not. + +If you need more complex validation than presented before, you can implement your own query validators. + +Implementing your custom validators +----------------------------------- + +All custom query validators should extend the `ValidationRule`_ base class importable from the ``graphql.validation.rules`` module. +Query validators are `Visitor`_ classes. + +Your custom validator should implement some methods of + +In order to perform validation, your validator class should define some one or more of ``enter_*`` or ``leave_*`` methods. + +Foreach methods, you will receive a node object to test: + +- You can now choose to raise an error by calling ``report_error`` method with an instance of GraphQLError describing the failure reason. +- Or you can continue the parsing by returning ``None``. + +Example +------- + +Here is an example query validator that only allows queries fields with a name of even length. + +.. code:: python + + from graphql import GraphQLError + from graphql.language import FieldNode + from graphql.validation import ValidationRule + + + class MyCustomValidationRule(ValidationRule): + def enter_field(self, node: FieldNode, *_args): + if len(node.name.value) % 2 == 0: + # Here the query length is even, so we allow it. + return + else: + # Here the query length is odd, so we don't want to allow it. + # Calling self.report_error will make the query fail with the error message. + self.report_error( + GraphQLError( + f"Cannot query '{field_name}': length is odd.", node, + ) + ) + +.. _ValidationRule: https://github.com/graphql-python/graphql-core/blob/v3.0.5/src/graphql/validation/rules/__init__.py#L37 +.. _Visitor: https://github.com/graphql-python/graphql-core/blob/d90bf9902ca1639365639d5632861d1e18d672a9/src/graphql/language/visitor.py#L111 diff --git a/docs/security/dast.rst b/docs/security/dast.rst new file mode 100644 index 00000000..f045da1a --- /dev/null +++ b/docs/security/dast.rst @@ -0,0 +1,32 @@ +Dynamic Application Security Testing +==================================== + +Continuous security testing +--------------------------- + +One of the best way to stop wondering about security for your API is to be able to scan it each time you deploy it into +staging or production environments. As you run your unit tests in your CI/CD pipeline, you can bullet-proof your GraphQL +application before it even reaches a production environment. + +Security testing tools +---------------------- + +graphql.security +________________ + +`graphql.security`_ is a free, quick graphql security testing tool, allowing you to quickly assess the most common +vulnerabilities in your application. + +Escape +______ + +`Escape`_ is a GraphQL security SaaS platform running an automated pentest tool. + +You can effortlessly incorporate this platform into your current CI/CD pipeline such as Github Actions or Gitlab CIs +which makes it convenient to set up. + +The security notifications will be automatically communicated to your CI/CD platform, enabling you to promptly attend to +them. + +.. _graphql.security: https://graphql.security/ +.. _Escape: https://escape.tech/ diff --git a/docs/security/index.rst b/docs/security/index.rst new file mode 100644 index 00000000..c35d62d0 --- /dev/null +++ b/docs/security/index.rst @@ -0,0 +1,21 @@ +====================== +Security consideration +====================== + +This section will discuss the most common security risks and how to mitigate them. + +As GraphQL is a query language, it allows users to use a wider pannel of inputs than traditional REST APIs. + +Due to this feature, GraphQL APIs are inherently prone to various security risks, but they can be reduced by taking appropriate precautions. Neglecting them can expose the API to vulnerabilities like credential leakage or denial of service attacks. + +As Graphene is only an engine to run GraphQL queries, it is not responsible for the HTTP layer, and this security must be handled by the web framework you are using. For example, if you are using Django-GraphQL, you may also want to check out the `Django documentation`_ on securing your API. + +.. toctree:: + :maxdepth: 1 + + maxdepth + introspection + customvalidation + dast + +.. _Django documentation: https://docs.djangoproject.com/en/4.1/topics/security/ diff --git a/docs/security/introspection.rst b/docs/security/introspection.rst new file mode 100644 index 00000000..0cc8a90b --- /dev/null +++ b/docs/security/introspection.rst @@ -0,0 +1,49 @@ +Disable Introspection +===================== + +What is the introspection ? +--------------------------- + +The introspection query is a query that allows you to ask the server what queries and mutations are supported. If you are a REST user, you can view it as an openapi or swagger schema. + +Should I disable my introspection ? +----------------------------------- + +Whether you are building a private or a public API, you might want to disable introspection : + +- If you are building a public API, the introspection allows consumers (developers) to know what they can do with your API. If you disable it, it will be harder for them to use your API. +- However, if you are building a private API, the only consumers of your API will be your developers. In this case, keep the introspection open in staging environments but close it in production to reduce the attack surface. + +Remember that disabling introspection does not prevent hackers from sending queries to your API. It just makes it harder to know what they can do with it. + +Implementation +-------------- + +Graphene provides a validation rule to disable introspection. It ensures that your schema cannot be introspected. + +You just need to import the ``DisableIntrospection`` class from ``graphene.validation``. + +Here is a code example of how you can disable introspection for your schema. + +.. code:: python + + from graphql import validate, parse + from graphene import ObjectType, Schema, String + from graphene.validation import DisableIntrospection + + + class MyQuery(ObjectType): + name = String(required=True) + + + schema = Schema(query=MyQuery) + + # introspection queries will not be executed. + + validation_errors = validate( + schema=schema.graphql_schema, + document_ast=parse('THE QUERY'), + rules=( + DisableIntrospection, + ) + ) diff --git a/docs/security/maxdepth.rst b/docs/security/maxdepth.rst new file mode 100644 index 00000000..5eae1dad --- /dev/null +++ b/docs/security/maxdepth.rst @@ -0,0 +1,120 @@ +Depth limit Validator +===================== + +By default, GraphQL queries can be arbitrarily deep. But a profound query can lead to a denial of service attack where a client can send a deeply nested query that will take a long time to execute. + +If you find a cycle in the graph, you can iterate over it to generate big queries to put an extreme load on the backend. + +Example +------- + +For example a simple app that allows you to find your friends and their friends can lead to this query : + +.. code:: graphql + + query { + me { + friends { + friends { + friends { + friends { + ... + # dumping the whole database + } + } + } + } + } + } + +This is not a common use case; your dev team will not do that in the first place. However, as your endpoint is publicly +available, you cannot be sure that someone will not try to do so. + +Remediation +----------- + +Graphene provides a depth limit validator that can be used to prevent this kind of attack. It can be configured to limit +the depth of all the queries or only some specific ones. + +Arguments +_________ + +The only required argument is ``max_depth`` which is the maximum allowed depth for any operation in a GraphQL document. + +The other optional parameters are the following ones : + +- ``ignore``: A list of patterns that, if matched stops recursive depth checking. It can be one of the following : + - ``Callable : (dict[str, int]) -> bool``: A function that receives the current operation and returns a boolean. + - ``Pattern``: A compiled regex pattern that is matched against the operation name. + - ``str``: An operation name. +- ``callback: (dict[str, int]) -> None`` Called each time validation runs. Receives an Object which is a map of the depths for each operation. + +Usage +----- + +Here is an example of how you would implement depth-limiting on your schema. + +.. code:: python + + from graphql import validate, parse + from graphene import ObjectType, Schema, String + from graphene.validation import depth_limit_validator + + + class Me(ObjectType): + id = String() + + schema = Schema(query=Me) + + VALID_QUERY = """ + query valid { + me { + # depth is 1 + id friends { + # depth is 2 + id friends { + # depth is 3 + id + } + } + } + } + """ + + INVALID_QUERY = """ + query evil { + me { + # depth is 1 + id friends { + # depth is 2 + id friends { + # depth is 3 + id friends { + # depth is 4 + id + } + } + } + } + } + """ + + RULES = (depth_limit_validator(max_depth=3), ) + + VALID_RESPONSE = validate( + schema=schema.graphql_schema, + document_ast=parse(VALID_QUERY), + rules=RULES + ) + + assert len(VALID_RESPONSE) == 0 + + INVALID_RESPONSE = validate( + schema=schema.graphql_schema, + document_ast=parse(INVALID_QUERY), + rules=RULES + ) + + + assert len(INVALID_RESPONSE) == 1 + assert INVALID_RESPONSE[0].message == "'evil' exceeds maximum operation depth of 3."