Skip to content

Commit 2ef4bfc

Browse files
committed
Block reserved names in non-Introspection schemas
1 parent 7c91e3a commit 2ef4bfc

File tree

2 files changed

+104
-25
lines changed

2 files changed

+104
-25
lines changed

include/SchemaLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ class SchemaLoader
281281
void visitObjectTypeExtension(const peg::ast_node& objectTypeExtension);
282282
void visitDirectiveDefinition(const peg::ast_node& directiveDefinition);
283283

284+
static void blockReservedName(
285+
std::string_view name, std::optional<tao::graphqlpeg::position> position = std::nullopt);
284286
static OutputFieldList getOutputFields(const peg::ast_node::children_t& fields);
285287
static InputFieldList getInputFields(const peg::ast_node::children_t& fields);
286288

src/SchemaLoader.cpp

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,12 @@ void SchemaLoader::visitObjectTypeDefinition(const peg::ast_node& objectTypeDefi
548548
std::string_view description;
549549

550550
peg::on_first_child<peg::object_name>(objectTypeDefinition,
551-
[&name](const peg::ast_node& child) {
551+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
552552
name = child.string_view();
553+
if (!isIntrospection)
554+
{
555+
blockReservedName(name, child.begin());
556+
}
553557
});
554558

555559
peg::on_first_child<peg::description>(objectTypeDefinition,
@@ -575,9 +579,14 @@ void SchemaLoader::visitObjectTypeExtension(const peg::ast_node& objectTypeExten
575579
{
576580
std::string_view name;
577581

578-
peg::on_first_child<peg::object_name>(objectTypeExtension, [&name](const peg::ast_node& child) {
579-
name = child.string_view();
580-
});
582+
peg::on_first_child<peg::object_name>(objectTypeExtension,
583+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
584+
name = child.string_view();
585+
if (!isIntrospection)
586+
{
587+
blockReservedName(name, child.begin());
588+
}
589+
});
581590

582591
const auto itrType = _objectNames.find(name);
583592

@@ -609,8 +618,12 @@ void SchemaLoader::visitInterfaceTypeDefinition(const peg::ast_node& interfaceTy
609618
std::string_view description;
610619

611620
peg::on_first_child<peg::interface_name>(interfaceTypeDefinition,
612-
[&name](const peg::ast_node& child) {
621+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
613622
name = child.string_view();
623+
if (!isIntrospection)
624+
{
625+
blockReservedName(name, child.begin());
626+
}
614627
});
615628

616629
peg::on_first_child<peg::description>(interfaceTypeDefinition,
@@ -637,8 +650,12 @@ void SchemaLoader::visitInterfaceTypeExtension(const peg::ast_node& interfaceTyp
637650
std::string_view name;
638651

639652
peg::on_first_child<peg::interface_name>(interfaceTypeExtension,
640-
[&name](const peg::ast_node& child) {
653+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
641654
name = child.string_view();
655+
if (!isIntrospection)
656+
{
657+
blockReservedName(name, child.begin());
658+
}
642659
});
643660

644661
const auto itrType = _interfaceNames.find(name);
@@ -666,8 +683,12 @@ void SchemaLoader::visitInputObjectTypeDefinition(const peg::ast_node& inputObje
666683
std::string_view description;
667684

668685
peg::on_first_child<peg::object_name>(inputObjectTypeDefinition,
669-
[&name](const peg::ast_node& child) {
686+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
670687
name = child.string_view();
688+
if (!isIntrospection)
689+
{
690+
blockReservedName(name, child.begin());
691+
}
671692
});
672693

673694
peg::on_first_child<peg::description>(inputObjectTypeDefinition,
@@ -694,8 +715,12 @@ void SchemaLoader::visitInputObjectTypeExtension(const peg::ast_node& inputObjec
694715
std::string_view name;
695716

696717
peg::on_first_child<peg::object_name>(inputObjectTypeExtension,
697-
[&name](const peg::ast_node& child) {
718+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
698719
name = child.string_view();
720+
if (!isIntrospection)
721+
{
722+
blockReservedName(name, child.begin());
723+
}
699724
});
700725

701726
const auto itrType = _inputNames.find(name);
@@ -722,9 +747,14 @@ void SchemaLoader::visitEnumTypeDefinition(const peg::ast_node& enumTypeDefiniti
722747
std::string_view name;
723748
std::string_view description;
724749

725-
peg::on_first_child<peg::enum_name>(enumTypeDefinition, [&name](const peg::ast_node& child) {
726-
name = child.string_view();
727-
});
750+
peg::on_first_child<peg::enum_name>(enumTypeDefinition,
751+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
752+
name = child.string_view();
753+
if (!isIntrospection)
754+
{
755+
blockReservedName(name, child.begin());
756+
}
757+
});
728758

729759
peg::on_first_child<peg::description>(enumTypeDefinition,
730760
[&description](const peg::ast_node& child) {
@@ -749,9 +779,14 @@ void SchemaLoader::visitEnumTypeExtension(const peg::ast_node& enumTypeExtension
749779
{
750780
std::string_view name;
751781

752-
peg::on_first_child<peg::enum_name>(enumTypeExtension, [&name](const peg::ast_node& child) {
753-
name = child.string_view();
754-
});
782+
peg::on_first_child<peg::enum_name>(enumTypeExtension,
783+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
784+
name = child.string_view();
785+
if (!isIntrospection)
786+
{
787+
blockReservedName(name, child.begin());
788+
}
789+
});
755790

756791
const auto itrType = _enumNames.find(name);
757792

@@ -827,8 +862,12 @@ void SchemaLoader::visitScalarTypeDefinition(const peg::ast_node& scalarTypeDefi
827862
std::string_view description;
828863

829864
peg::on_first_child<peg::scalar_name>(scalarTypeDefinition,
830-
[&name](const peg::ast_node& child) {
865+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
831866
name = child.string_view();
867+
if (!isIntrospection)
868+
{
869+
blockReservedName(name, child.begin());
870+
}
832871
});
833872

834873
peg::on_first_child<peg::description>(scalarTypeDefinition,
@@ -851,9 +890,14 @@ void SchemaLoader::visitScalarTypeExtension(const peg::ast_node& scalarTypeExten
851890
{
852891
std::string_view name;
853892

854-
peg::on_first_child<peg::scalar_name>(scalarTypeExtension, [&name](const peg::ast_node& child) {
855-
name = child.string_view();
856-
});
893+
peg::on_first_child<peg::scalar_name>(scalarTypeExtension,
894+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
895+
name = child.string_view();
896+
if (!isIntrospection)
897+
{
898+
blockReservedName(name, child.begin());
899+
}
900+
});
857901

858902
const auto itrType = _scalarNames.find(name);
859903

@@ -909,9 +953,14 @@ void SchemaLoader::visitUnionTypeDefinition(const peg::ast_node& unionTypeDefini
909953
std::string_view name;
910954
std::string_view description;
911955

912-
peg::on_first_child<peg::union_name>(unionTypeDefinition, [&name](const peg::ast_node& child) {
913-
name = child.string_view();
914-
});
956+
peg::on_first_child<peg::union_name>(unionTypeDefinition,
957+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
958+
name = child.string_view();
959+
if (!isIntrospection)
960+
{
961+
blockReservedName(name, child.begin());
962+
}
963+
});
915964

916965
peg::on_first_child<peg::description>(unionTypeDefinition,
917966
[&description](const peg::ast_node& child) {
@@ -936,9 +985,14 @@ void SchemaLoader::visitUnionTypeExtension(const peg::ast_node& unionTypeExtensi
936985
{
937986
std::string_view name;
938987

939-
peg::on_first_child<peg::union_name>(unionTypeExtension, [&name](const peg::ast_node& child) {
940-
name = child.string_view();
941-
});
988+
peg::on_first_child<peg::union_name>(unionTypeExtension,
989+
[isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
990+
name = child.string_view();
991+
if (!isIntrospection)
992+
{
993+
blockReservedName(name, child.begin());
994+
}
995+
});
942996

943997
const auto itrType = _unionNames.find(name);
944998

@@ -958,8 +1012,12 @@ void SchemaLoader::visitDirectiveDefinition(const peg::ast_node& directiveDefini
9581012
Directive directive;
9591013

9601014
peg::on_first_child<peg::directive_name>(directiveDefinition,
961-
[&directive](const peg::ast_node& child) {
1015+
[isIntrospection = _isIntrospection, &directive](const peg::ast_node& child) {
9621016
directive.name = child.string_view();
1017+
if (!isIntrospection)
1018+
{
1019+
blockReservedName(directive.name, child.begin());
1020+
}
9631021
});
9641022

9651023
peg::on_first_child<peg::description>(directiveDefinition,
@@ -1030,6 +1088,25 @@ std::string_view SchemaLoader::getSafeCppName(std::string_view type) noexcept
10301088
return (safeNames.cend() == itr) ? type : itr->second->second;
10311089
}
10321090

1091+
void SchemaLoader::blockReservedName(
1092+
std::string_view name, std::optional<tao::graphqlpeg::position> position)
1093+
{
1094+
// https://spec.graphql.org/October2021/#sec-Names.Reserved-Names
1095+
if (name.size() > 1 && name.substr(0, 2) == R"gql(__)gql"sv)
1096+
{
1097+
std::ostringstream error;
1098+
1099+
error << "Names starting with __ are reserved: " << name;
1100+
1101+
if (position)
1102+
{
1103+
error << " line: " << position->line << " column: " << position->column;
1104+
}
1105+
1106+
throw std::runtime_error(error.str());
1107+
}
1108+
}
1109+
10331110
OutputFieldList SchemaLoader::getOutputFields(const peg::ast_node::children_t& fields)
10341111
{
10351112
OutputFieldList outputFields;

0 commit comments

Comments
 (0)