|
| 1 | +package graphql.introspection; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import com.google.common.collect.ImmutableListMultimap; |
| 5 | +import graphql.ErrorClassification; |
| 6 | +import graphql.ExecutionResult; |
| 7 | +import graphql.ExecutionResultImpl; |
| 8 | +import graphql.GraphQLContext; |
| 9 | +import graphql.GraphQLError; |
| 10 | +import graphql.PublicApi; |
| 11 | +import graphql.execution.ExecutionContext; |
| 12 | +import graphql.language.SourceLocation; |
| 13 | +import graphql.normalized.ExecutableNormalizedField; |
| 14 | +import graphql.normalized.ExecutableNormalizedOperation; |
| 15 | +import graphql.schema.FieldCoordinates; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Optional; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
| 23 | +import static graphql.schema.FieldCoordinates.coordinates; |
| 24 | + |
| 25 | +/** |
| 26 | + * This {@link graphql.execution.instrumentation.Instrumentation} ensure that a submitted introspection query is done in |
| 27 | + * good faith. |
| 28 | + * <p> |
| 29 | + * There are attack vectors where a crafted introspection query can cause the engine to spend too much time |
| 30 | + * producing introspection data. This is especially true on large schemas with lots of types and fields. |
| 31 | + * <p> |
| 32 | + * Schemas form a cyclic graph and hence it's possible to send in introspection queries that can reference those cycles |
| 33 | + * and in large schemas this can be expensive and perhaps a "denial of service". |
| 34 | + * <p> |
| 35 | + * This instrumentation only allows one __schema field or one __type field to be present, and it does not allow the `__Type` fields |
| 36 | + * to form a cycle, i.e., that can only be present once. This allows the standard and common introspection queries to work |
| 37 | + * so tooling such as graphiql can work. |
| 38 | + */ |
| 39 | +@PublicApi |
| 40 | +public class GoodFaithIntrospection { |
| 41 | + |
| 42 | + /** |
| 43 | + * Placing a boolean value under this key in the per request {@link GraphQLContext} will enable |
| 44 | + * or disable Good Faith Introspection on that request. |
| 45 | + */ |
| 46 | + public static final String GOOD_FAITH_INTROSPECTION_DISABLED = "GOOD_FAITH_INTROSPECTION_DISABLED"; |
| 47 | + |
| 48 | + private static final AtomicBoolean ENABLED_STATE = new AtomicBoolean(true); |
| 49 | + |
| 50 | + /** |
| 51 | + * @return true if good faith introspection is enabled |
| 52 | + */ |
| 53 | + public static boolean isEnabledJvmWide() { |
| 54 | + return ENABLED_STATE.get(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * This allows you to disable good faith introspection, which is on by default. |
| 59 | + * |
| 60 | + * @param flag the desired state |
| 61 | + * |
| 62 | + * @return the previous state |
| 63 | + */ |
| 64 | + public static boolean enabledJvmWide(boolean flag) { |
| 65 | + return ENABLED_STATE.getAndSet(flag); |
| 66 | + } |
| 67 | + |
| 68 | + private static final Map<FieldCoordinates, Integer> ALLOWED_FIELD_INSTANCES = new HashMap<>(); |
| 69 | + |
| 70 | + static { |
| 71 | + ALLOWED_FIELD_INSTANCES.put(coordinates("Query", "__schema"), 1); |
| 72 | + ALLOWED_FIELD_INSTANCES.put(coordinates("Query", "__type"), 1); |
| 73 | + ALLOWED_FIELD_INSTANCES.put(coordinates("__Type", "fields"), 1); |
| 74 | + ALLOWED_FIELD_INSTANCES.put(coordinates("__Type", "inputFields"), 1); |
| 75 | + ALLOWED_FIELD_INSTANCES.put(coordinates("__Type", "interfaces"), 1); |
| 76 | + ALLOWED_FIELD_INSTANCES.put(coordinates("__Type", "possibleTypes"), 1); |
| 77 | + } |
| 78 | + |
| 79 | + public static Optional<ExecutionResult> checkIntrospection(ExecutionContext executionContext) { |
| 80 | + if (isIntrospectionEnabled(executionContext.getGraphQLContext())) { |
| 81 | + ExecutableNormalizedOperation operation = executionContext.getNormalizedQueryTree().get(); |
| 82 | + ImmutableListMultimap<FieldCoordinates, ExecutableNormalizedField> coordinatesToENFs = operation.getCoordinatesToNormalizedFields(); |
| 83 | + for (Map.Entry<FieldCoordinates, Integer> entry : ALLOWED_FIELD_INSTANCES.entrySet()) { |
| 84 | + FieldCoordinates coordinates = entry.getKey(); |
| 85 | + Integer allowSize = entry.getValue(); |
| 86 | + ImmutableList<ExecutableNormalizedField> normalizedFields = coordinatesToENFs.get(coordinates); |
| 87 | + if (normalizedFields.size() > allowSize) { |
| 88 | + BadFaithIntrospectionError error = new BadFaithIntrospectionError(coordinates.toString()); |
| 89 | + return Optional.of(ExecutionResultImpl.newExecutionResult().addError(error).build()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return Optional.empty(); |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean isIntrospectionEnabled(GraphQLContext graphQlContext) { |
| 97 | + if (!isEnabledJvmWide()) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + return !graphQlContext.getOrDefault(GOOD_FAITH_INTROSPECTION_DISABLED, false); |
| 101 | + } |
| 102 | + |
| 103 | + public static class BadFaithIntrospectionError implements GraphQLError { |
| 104 | + private final String message; |
| 105 | + |
| 106 | + public BadFaithIntrospectionError(String qualifiedField) { |
| 107 | + this.message = String.format("This request is not asking for introspection in good faith - %s is present too often!", qualifiedField); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getMessage() { |
| 112 | + return message; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ErrorClassification getErrorType() { |
| 117 | + return ErrorClassification.errorClassification("BadFaithIntrospection"); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public List<SourceLocation> getLocations() { |
| 122 | + return null; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments