Skip to content

Commit 1f923bc

Browse files
authored
fix: max-depth ignore only Field Node named __schema (#823)
* fix: max-depth ignore only Field Node named __schema * Create hip-cheetahs-eat.md Signed-off-by: M0ngi <[email protected]> --------- Signed-off-by: M0ngi <[email protected]>
1 parent 9f6a655 commit 1f923bc

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

.changeset/hip-cheetahs-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@escape.tech/graphql-armor-max-depth": patch
3+
---
4+
5+
fix: max-depth ignore only Field Node named __schema

packages/plugins/max-depth/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ class MaxDepthVisitor {
7676
node: FieldNode | FragmentDefinitionNode | InlineFragmentNode | OperationDefinitionNode | FragmentSpreadNode,
7777
parentDepth = 0,
7878
): number {
79-
if (this.config.ignoreIntrospection && 'name' in node && node.name?.value === '__schema') {
79+
if (
80+
this.config.ignoreIntrospection &&
81+
'name' in node &&
82+
node.name?.value === '__schema' &&
83+
node.kind === Kind.FIELD
84+
) {
8085
return 0;
8186
}
8287
let depth = parentDepth;

packages/plugins/max-depth/test/index.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,34 @@ describe('maxDepthPlugin', () => {
239239
`Syntax Error: Query depth limit of ${maxDepth} exceeded, found ${maxDepth + 1}.`,
240240
]);
241241
});
242+
243+
it('rejects for fragment named `__schema` exceeding max depth', async () => {
244+
const bypass_query = `
245+
query {
246+
books {
247+
author {
248+
books {
249+
author {
250+
...__schema
251+
}
252+
}
253+
}
254+
}
255+
}
256+
fragment __schema on Author {
257+
books {
258+
title
259+
}
260+
}
261+
`;
262+
const maxDepth = 6;
263+
const testkit = createTestkit([maxDepthPlugin({ n: maxDepth, exposeLimits: true })], schema);
264+
const result = await testkit.execute(bypass_query);
265+
266+
assertSingleExecutionValue(result);
267+
expect(result.errors).toBeDefined();
268+
expect(result.errors?.map((error) => error.message)).toEqual([
269+
`Syntax Error: Query depth limit of ${maxDepth} exceeded, found ${maxDepth + 2}.`,
270+
]);
271+
});
242272
});

0 commit comments

Comments
 (0)