Skip to content

Commit

Permalink
Disallow introspection fields on subscription root (#5187)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Jun 28, 2022
1 parent 66b9dce commit 9affedb
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/HotChocolate/Core/src/Validation/ErrorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,17 @@ public static IError SubscriptionSingleRootField(
.Build();
}

public static IError SubscriptionNoTopLevelIntrospectionField(
this IDocumentValidatorContext context,
OperationDefinitionNode operation)
{
return ErrorBuilder.New()
.SetMessage(Resources.ErrorHelper_SubscriptionNoTopLevelIntrospectionField)
.AddLocation(operation)
.SpecifiedBy("sec-Single-root-field")
.Build();
}

public static IError MaxOperationComplexity(
this IDocumentValidatorContext context,
OperationDefinitionNode operation,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@
<data name="ErrorHelper_SubscriptionSingleRootField" xml:space="preserve">
<value>Subscription operations must have exactly one root field.</value>
</data>
<data name="ErrorHelper_SubscriptionNoTopLevelIntrospectionField" xml:space="preserve">
<value>Subscription must not select an introspection top level field.</value>
</data>
<data name="ErrorHelper_MaxOperationComplexity" xml:space="preserve">
<value>The GraphQL document has an operation complexity of {0} which exceeds the max allowed operation complexity of {1}.</value>
</data>
Expand Down
10 changes: 9 additions & 1 deletion src/HotChocolate/Core/src/Validation/Rules/OperationVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
using HotChocolate.Types.Introspection;

namespace HotChocolate.Validation.Rules;

Expand Down Expand Up @@ -54,7 +56,8 @@ protected override ISyntaxVisitorAction Enter(
else if (!context.Names.Add(operation.Name.Value))
{
context.ReportError(context.OperationNameNotUnique(
operation, operation.Name.Value));
operation,
operation.Name.Value));
}
}
}
Expand Down Expand Up @@ -89,6 +92,11 @@ protected override ISyntaxVisitorAction Leave(
{
context.ReportError(context.SubscriptionSingleRootField(node));
}
else if (IntrospectionFields.TypeName.Equals(context.Names.Single()))
{
context.ReportError(context.SubscriptionNoTopLevelIntrospectionField(node));
}

return Continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,17 @@ subscription sub {
$"Subscription operations must " +
"have exactly one root field.", t.Message));
}

[Fact]
public void DisallowedOnlyIntrospectionField()
{
ExpectErrors(@"
subscription sub {
__typename
}
",
t => Assert.Equal(
"Subscription must not select an introspection top level field.", t.Message));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[
{
"Message": "Subscription must not select an introspection top level field.",
"Code": null,
"Path": null,
"Locations": [
{
"Line": 2,
"Column": 17
}
],
"Extensions": {
"specifiedBy": "http://spec.graphql.org/October2021/#sec-Single-root-field"
},
"Exception": null,
"SyntaxNode": {
"Kind": "OperationDefinition",
"Location": {
"Start": 17,
"End": 97,
"Line": 2,
"Column": 17
},
"Name": {
"Kind": "Name",
"Location": {
"Start": 30,
"End": 35,
"Line": 2,
"Column": 30
},
"Value": "sub"
},
"Operation": "Subscription",
"VariableDefinitions": [],
"Directives": [],
"SelectionSet": {
"Kind": "SelectionSet",
"Location": {
"Start": 34,
"End": 97,
"Line": 2,
"Column": 34
},
"Selections": [
{
"Kind": "Field",
"Alias": null,
"Arguments": [],
"Required": null,
"SelectionSet": null,
"Location": {
"Start": 56,
"End": 84,
"Line": 3,
"Column": 21
},
"Name": {
"Kind": "Name",
"Location": {
"Start": 56,
"End": 84,
"Line": 3,
"Column": 21
},
"Value": "__typename"
},
"Directives": []
}
]
}
}
}
]

0 comments on commit 9affedb

Please sign in to comment.