Skip to content

Commit 90522d8

Browse files
committed
Merge remote-tracking branch 'origin/ability_to_disable_introspection' into good-faith-introspection
2 parents d304767 + c032d57 commit 90522d8

7 files changed

Lines changed: 224 additions & 2 deletions

File tree

‎src/main/java/graphql/ErrorType.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public enum ErrorType implements ErrorClassification {
1111
DataFetchingException,
1212
NullValueInNonNullableField,
1313
OperationNotSupported,
14+
IntrospectionDisabled,
1415
ExecutionAborted
1516
}

‎src/main/java/graphql/execution/AsyncExecutionStrategy.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import graphql.execution.instrumentation.ExecutionStrategyInstrumentationContext;
77
import graphql.execution.instrumentation.Instrumentation;
88
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters;
9+
import graphql.introspection.Introspection;
910

1011
import java.util.List;
12+
import java.util.Optional;
1113
import java.util.concurrent.CompletableFuture;
1214
import java.util.function.BiConsumer;
1315

@@ -46,6 +48,11 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
4648
MergedSelectionSet fields = parameters.getFields();
4749
List<String> fieldNames = fields.getKeys();
4850

51+
Optional<ExecutionResult> isNotSensible = Introspection.isIntrospectionSensible(executionContext.getGraphQLContext(),fields);
52+
if (isNotSensible.isPresent()) {
53+
return CompletableFuture.completedFuture(isNotSensible.get());
54+
}
55+
4956
DeferredExecutionSupport deferredExecutionSupport = createDeferredExecutionSupport(executionContext, parameters);
5057
Async.CombinedBuilder<FieldValueInfo> futures = getAsyncFieldValueInfo(executionContext, parameters, deferredExecutionSupport);
5158

‎src/main/java/graphql/execution/AsyncSerialExecutionStrategy.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import graphql.execution.instrumentation.Instrumentation;
77
import graphql.execution.instrumentation.InstrumentationContext;
88
import graphql.execution.instrumentation.parameters.InstrumentationExecutionStrategyParameters;
9+
import graphql.introspection.Introspection;
910

1011
import java.util.List;
12+
import java.util.Optional;
1113
import java.util.concurrent.CompletableFuture;
1214

1315
import static graphql.execution.instrumentation.SimpleInstrumentationContext.nonNullCtx;
@@ -40,6 +42,13 @@ public CompletableFuture<ExecutionResult> execute(ExecutionContext executionCont
4042
MergedSelectionSet fields = parameters.getFields();
4143
ImmutableList<String> fieldNames = ImmutableList.copyOf(fields.keySet());
4244

45+
// this is highly unlikely since Mutations cant do introspection BUT in theory someone could make the query strategy this code
46+
// so belts and braces
47+
Optional<ExecutionResult> isNotSensible = Introspection.isIntrospectionSensible(executionContext.getGraphQLContext(), fields);
48+
if (isNotSensible.isPresent()) {
49+
return CompletableFuture.completedFuture(isNotSensible.get());
50+
}
51+
4352
CompletableFuture<List<Object>> resultsFuture = Async.eachSequentially(fieldNames, (fieldName, prevResults) -> {
4453
MergedField currentField = fields.getSubField(fieldName);
4554
ResultPath fieldPath = parameters.getPath().segment(mkNameForPath(currentField));

‎src/main/java/graphql/introspection/Introspection.java‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
import com.google.common.collect.ImmutableSet;
55
import graphql.Assert;
6+
import graphql.ExecutionResult;
67
import graphql.GraphQLContext;
78
import graphql.Internal;
89
import graphql.PublicApi;
10+
import graphql.execution.MergedField;
11+
import graphql.execution.MergedSelectionSet;
912
import graphql.execution.ValuesResolver;
1013
import graphql.language.AstPrinter;
1114
import graphql.schema.FieldCoordinates;
@@ -32,14 +35,17 @@
3235
import graphql.schema.GraphQLSchema;
3336
import graphql.schema.GraphQLUnionType;
3437
import graphql.schema.InputValueWithState;
38+
import org.jetbrains.annotations.NotNull;
3539

3640
import java.util.ArrayList;
3741
import java.util.HashSet;
3842
import java.util.LinkedHashMap;
3943
import java.util.List;
4044
import java.util.Locale;
4145
import java.util.Map;
46+
import java.util.Optional;
4247
import java.util.Set;
48+
import java.util.concurrent.atomic.AtomicBoolean;
4349
import java.util.function.Function;
4450
import java.util.stream.Collectors;
4551

@@ -58,8 +64,83 @@
5864
import static graphql.schema.GraphQLTypeUtil.unwrapAllAs;
5965
import static graphql.schema.GraphQLTypeUtil.unwrapOne;
6066

67+
/**
68+
* GraphQl has a unique capability called <a href="https://spec.graphql.org/October2021/#sec-Introspection">Introspection</a> that allow
69+
* consumers to inspect the system and discover the fields and types available and makes the system self documented.
70+
* <p>
71+
* Some security recommendations such as <a href="https://owasp.org/www-chapter-vancouver/assets/presentations/2020-06_GraphQL_Security.pdf">OWASP</a>
72+
* recommend that introspection be disabled in production. The {@link Introspection#enabledJvmWide(boolean)} method can be used to disable
73+
* introspection for the whole JVM or you can place {@link Introspection#INTROSPECTION_DISABLED} into the {@link GraphQLContext} of a request
74+
* to disable introspection for that request.
75+
*/
6176
@PublicApi
6277
public class Introspection {
78+
79+
80+
/**
81+
* Placing a boolean value under this key in the per request {@link GraphQLContext} will enable
82+
* or disable Introspection on that request.
83+
*/
84+
public static final String INTROSPECTION_DISABLED = "INTROSPECTION_DISABLED";
85+
private static final AtomicBoolean INTROSPECTION_ENABLED_STATE = new AtomicBoolean(true);
86+
87+
/**
88+
* This static method will enable / disable Introspection at a JVM wide level.
89+
*
90+
* @param enabled the flag indicating the desired enabled state
91+
*
92+
* @return the previous state of enablement
93+
*/
94+
public static boolean enabledJvmWide(boolean enabled) {
95+
return INTROSPECTION_ENABLED_STATE.getAndSet(enabled);
96+
}
97+
98+
/**
99+
* @return true if Introspection is enabled at a JVM wide level or false otherwise
100+
*/
101+
public static boolean isEnabledJvmWide() {
102+
return INTROSPECTION_ENABLED_STATE.get();
103+
}
104+
105+
/**
106+
* This will look in to the field selection set and see if there are introspection fields,
107+
* and if there is,it checks if introspection should run, and if not it will return an errored {@link ExecutionResult}
108+
* that can be returned to the user.
109+
*
110+
* @param mergedSelectionSet the fields to be executed
111+
*
112+
* @return an optional error result
113+
*/
114+
public static Optional<ExecutionResult> isIntrospectionSensible(GraphQLContext graphQLContext, MergedSelectionSet mergedSelectionSet) {
115+
MergedField schemaField = mergedSelectionSet.getSubField(SchemaMetaFieldDef.getName());
116+
if (schemaField != null) {
117+
if (!isIntrospectionEnabled(graphQLContext)) {
118+
return mkDisabledError(schemaField);
119+
}
120+
}
121+
MergedField typeField = mergedSelectionSet.getSubField(TypeMetaFieldDef.getName());
122+
if (typeField != null) {
123+
if (!isIntrospectionEnabled(graphQLContext)) {
124+
return mkDisabledError(typeField);
125+
}
126+
}
127+
// later we can put a good faith check code here to check the fields make sense
128+
return Optional.empty();
129+
}
130+
131+
@NotNull
132+
private static Optional<ExecutionResult> mkDisabledError(MergedField schemaField) {
133+
IntrospectionDisabledError error = new IntrospectionDisabledError(schemaField.getSingleField().getSourceLocation());
134+
return Optional.of(ExecutionResult.newExecutionResult().addError(error).build());
135+
}
136+
137+
private static boolean isIntrospectionEnabled(GraphQLContext graphQlContext) {
138+
if (!isEnabledJvmWide()) {
139+
return false;
140+
}
141+
return !graphQlContext.getOrDefault(INTROSPECTION_DISABLED, false);
142+
}
143+
63144
private static final Map<FieldCoordinates, IntrospectionDataFetcher<?>> introspectionDataFetchers = new LinkedHashMap<>();
64145

65146
private static void register(GraphQLFieldsContainer parentType, String fieldName, IntrospectionDataFetcher<?> introspectionDataFetcher) {
@@ -636,6 +717,7 @@ public enum DirectiveLocation {
636717
return environment.getGraphQLSchema().getType(name);
637718
};
638719

720+
// __typename is always available
639721
public static final IntrospectionDataFetcher<?> TypeNameMetaFieldDefDataFetcher = environment -> simplePrint(environment.getParentType());
640722

641723
@Internal
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package graphql.introspection;
2+
3+
import graphql.ErrorClassification;
4+
import graphql.ErrorType;
5+
import graphql.GraphQLError;
6+
import graphql.Internal;
7+
import graphql.language.SourceLocation;
8+
9+
import java.util.Collections;
10+
import java.util.List;
11+
12+
@Internal
13+
public class IntrospectionDisabledError implements GraphQLError {
14+
15+
private final List<SourceLocation> locations;
16+
17+
public IntrospectionDisabledError(SourceLocation sourceLocation) {
18+
locations = sourceLocation == null ? Collections.emptyList() : Collections.singletonList(sourceLocation);
19+
}
20+
21+
@Override
22+
public String getMessage() {
23+
return "Introspection has been disabled for this request";
24+
}
25+
26+
@Override
27+
public List<SourceLocation> getLocations() {
28+
return locations;
29+
}
30+
31+
@Override
32+
public ErrorClassification getErrorType() {
33+
return ErrorType.IntrospectionDisabled;
34+
}
35+
}

‎src/main/java/graphql/schema/visibility/NoIntrospectionGraphqlFieldVisibility.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@
1212
* This field visibility will prevent Introspection queries from being performed. Technically this puts your
1313
* system in contravention of <a href="https://spec.graphql.org/October2021/#sec-Introspection">the specification</a>
1414
* but some production systems want this lock down in place.
15+
*
16+
* @deprecated This is no longer the best way to prevent Introspection - {@link graphql.introspection.Introspection#enabledJvmWide(boolean)}
17+
* can be used instead
1518
*/
1619
@PublicApi
20+
@Deprecated(since = "2024-03-16")
1721
public class NoIntrospectionGraphqlFieldVisibility implements GraphqlFieldVisibility {
1822

23+
@Deprecated(since = "2024-03-16")
1924
public static NoIntrospectionGraphqlFieldVisibility NO_INTROSPECTION_FIELD_VISIBILITY = new NoIntrospectionGraphqlFieldVisibility();
2025

2126

‎src/test/groovy/graphql/introspection/IntrospectionTest.groovy‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package graphql.introspection
22

3-
3+
import graphql.ErrorType
4+
import graphql.ExecutionInput
45
import graphql.TestUtil
6+
import graphql.execution.AsyncSerialExecutionStrategy
57
import graphql.schema.DataFetcher
68
import graphql.schema.FieldCoordinates
79
import graphql.schema.GraphQLCodeRegistry
@@ -547,7 +549,7 @@ class IntrospectionTest extends Specification {
547549

548550
then:
549551
def oldQuery = oldIntrospectionQuery.replaceAll("\\s+", "")
550-
def newQuery = newIntrospectionQuery.replaceAll("\\s+","")
552+
def newQuery = newIntrospectionQuery.replaceAll("\\s+", "")
551553
oldQuery == newQuery
552554
}
553555

@@ -688,4 +690,85 @@ class IntrospectionTest extends Specification {
688690
queryType["isOneOf"] == null
689691
}
690692

693+
def "jvm wide enablement"() {
694+
def graphQL = TestUtil.graphQL("type Query { f : String } ").build()
695+
696+
when:
697+
def er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
698+
699+
then:
700+
er.errors.isEmpty()
701+
702+
when:
703+
Introspection.enabledJvmWide(false)
704+
er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
705+
706+
then:
707+
er.errors[0] instanceof IntrospectionDisabledError
708+
er.errors[0].getErrorType() == ErrorType.IntrospectionDisabled
709+
710+
when:
711+
Introspection.enabledJvmWide(true)
712+
er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
713+
714+
then:
715+
er.errors.isEmpty()
716+
}
717+
718+
def "per request enablement"() {
719+
def graphQL = TestUtil.graphQL("type Query { f : String } ").build()
720+
721+
when:
722+
// null context
723+
def ei = ExecutionInput.newExecutionInput(IntrospectionQuery.INTROSPECTION_QUERY)
724+
.build()
725+
def er = graphQL.execute(ei)
726+
727+
then:
728+
er.errors.isEmpty()
729+
730+
when:
731+
ei = ExecutionInput.newExecutionInput(IntrospectionQuery.INTROSPECTION_QUERY)
732+
.graphQLContext(Map.of(Introspection.INTROSPECTION_DISABLED, false)).build()
733+
er = graphQL.execute(ei)
734+
735+
then:
736+
er.errors.isEmpty()
737+
738+
when:
739+
ei = ExecutionInput.newExecutionInput(IntrospectionQuery.INTROSPECTION_QUERY)
740+
.graphQLContext(Map.of(Introspection.INTROSPECTION_DISABLED, true)).build()
741+
er = graphQL.execute(ei)
742+
743+
then:
744+
er.errors[0] instanceof IntrospectionDisabledError
745+
er.errors[0].getErrorType() == ErrorType.IntrospectionDisabled
746+
}
747+
748+
def "AsyncSerialExecutionStrategy with jvm wide enablement"() {
749+
def graphQL = TestUtil.graphQL("type Query { f : String } ")
750+
.queryExecutionStrategy(new AsyncSerialExecutionStrategy()).build()
751+
752+
when:
753+
def er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
754+
755+
then:
756+
er.errors.isEmpty()
757+
758+
when:
759+
Introspection.enabledJvmWide(false)
760+
er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
761+
762+
then:
763+
er.errors[0] instanceof IntrospectionDisabledError
764+
er.errors[0].getErrorType() == ErrorType.IntrospectionDisabled
765+
766+
when:
767+
Introspection.enabledJvmWide(true)
768+
er = graphQL.execute(IntrospectionQuery.INTROSPECTION_QUERY)
769+
770+
then:
771+
er.errors.isEmpty()
772+
}
773+
691774
}

0 commit comments

Comments
 (0)