CVE-2023-26144: Uncontrolled Resource Consumption (CWE-400)
value: Versions of the package graphql from 16.3.0 and before 16.8.1 are vulnerable to Denial of Service (DoS) due to insufficient checks in the OverlappingFieldsCanBeMergedRule.ts file when parsing large queries. This vulnerability allows an attacker to degrade system performance.**Note:** It was not proven that this vulnerability can crash the process.
CWE description: The product does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the exhaustion of available resources.
CVSS v3 base score: 5.3
Published: 2023-09-20T05:15Z
Repository: https://github.com/graphql/graphql-js
Language: TypeScript
Files changed: benchmark/repeated-fields-benchmark.js, src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts, src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Commit date: 2023-09-05 21:29:40 +0300
Commit message: OverlappingFieldsCanBeMergedRule: Fix performance degradation (#3958)

Co-authored-by: AaronMoat <AaronMoat@users.noreply.github.com>
Co-authored-by: Ivan Goncharov <ivan.goncharov.ua@gmail.com>
Resolves https://github.com/graphql/graphql-js/issues/3955 (at least

Diff: diff --git a/benchmark/repeated-fields-benchmark.js b/benchmark/repeated-fields-benchmark.js
new file mode 100644
index 00000000..b4169862
--- /dev/null
+++ b/benchmark/repeated-fields-benchmark.js
@@ -0,0 +1,13 @@
+import { graphqlSync } from 'graphql/graphql.js';
+import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
+
+const schema = buildSchema('type Query { hello: String! }');
+const source = `{ ${'hello '.repeat(250)}}`;
+
+export const benchmark = {
+  name: 'Many repeated fields',
+  count: 5,
+  measure() {
+    graphqlSync({ schema, source });
+  },
+};
diff --git a/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts b/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
index 52c2deb1..ecb56a15 100644
--- a/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
+++ b/src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
@@ -177,10 +177,28 @@ describe('Validate: Overlapping fields can be merged', () => {
         ],
       },
     ]);
   });
 
+  it('different stream directive extra argument', () => {
+    expectErrors(`
+      fragment conflictingArgs on Dog {
+        name @stream(label: "streamLabel", initialCount: 1)
+        name @stream(label: "streamLabel", initialCount: 1, extraArg: true)
+      }
+    `).toDeepEqual([
+      {
+        message:
+          'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
+        locations: [
+          { line: 3, column: 9 },
+          { line: 4, column: 9 },
+        ],
+      },
+    ]);
+  });
+
   it('mix of stream and no stream', () => {
     expectErrors(`
       fragment conflictingArgs on Dog {
         name @stream
         name
diff --git a/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts b/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
index 185648fe..851f7ea6 100644
--- a/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
+++ b/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
@@ -5,12 +5,12 @@ import { GraphQLError } from '../../error/GraphQLError.js';
 
 import type {
   DirectiveNode,
   FieldNode,
   FragmentDefinitionNode,
-  ObjectValueNode,
   SelectionSetNode,
+  ValueNode,
 } from '../../language/ast.js';
 import { Kind } from '../../language/kinds.js';
 import { print } from '../../language/printer.js';
 import type { ASTVisitor } from '../../language/visitor.js';
 
@@ -590,11 +590,11 @@ function findConflict(
         [node2],
       ];
     }
 
     // Two field calls must have the same arguments.
-    if (stringifyArguments(node1) !== stringifyArguments(node2)) {
+    if (!sameArguments(node1, node2)) {
       return [
         [responseName, 'they have differing arguments'],
         [node1],
         [node2],
       ];
@@ -647,23 +647,42 @@ function findConflict(
     );
     return subfieldConflicts(conflicts, responseName, node1, node2);
   }
 }
 
-function stringifyArguments(fieldNode: FieldNode | DirectiveNode): string {
-  // FIXME https://github.com/graphql/graphql-js/issues/2203
-  const args = /* c8 ignore next */ fieldNode.arguments ?? [];
-
-  const inputObjectWithArgs: ObjectValueNode = {
-    kind: Kind.OBJECT,
-    fields: args.map((argNode) => ({
-      kind: Kind.OBJECT_FIELD,
-      name: argNode.name,
-      value: argNode.value,
-    })),
-  };
-  return print(sortValueNode(inputObjectWithArgs));
+function sameArguments(
+  node1: FieldNode | DirectiveNode,
+  node2: FieldNode | DirectiveNode,
+): boolean {
+  const args1 = node1.arguments;
+  const args2 = node2.arguments;
+
+  if (args1 === undefined || args1.length === 0) {
+    return args2 === undefined || args2.length === 0;
+  }
+  if (args2 === undefined || args2.length === 0) {
+    return false;
+  }
+
+  if (args1.length !== args2.length) {
+    return false;
+  }
+
+  const values2 = new Map(args2.map(({ name, value }) => [name.value, value]));
+  return args1.every((arg1) => {
+    const value1 = arg1.value;
+    const value2 = values2.get(arg1.name.value);
+    if (value2 === undefined) {
+      return false;
+    }
+
+    return stringifyValue(value1) === stringifyValue(value2);
+  });
+}
+
+function stringifyValue(value: ValueNode): string | null {
+  return print(sortValueNode(value));
 }
 
 function getStreamDirective(
   directives: ReadonlyArray<DirectiveNode>,
 ): DirectiveNode | undefined {
@@ -679,11 +698,11 @@ function sameStreams(
   if (!stream1 && !stream2) {
     // both fields do not have streams
     return true;
   } else if (stream1 && stream2) {
     // check if both fields have equivalent streams
-    return stringifyArguments(stream1) === stringifyArguments(stream2);
+    return sameArguments(stream1, stream2);
   }
   // fields have a mix of stream and no stream
   return false;
 }
 

Vulnerable code:   ObjectValueNode,
    if (stringifyArguments(node1) !== stringifyArguments(node2)) {
function stringifyArguments(fieldNode: FieldNode | DirectiveNode): string {
  // FIXME https://github.com/graphql/graphql-js/issues/2203
  const args = /* c8 ignore next */ fieldNode.arguments ?? [];

  const inputObjectWithArgs: ObjectValueNode = {
    kind: Kind.OBJECT,
    fields: args.map((argNode) => ({
      kind: Kind.OBJECT_FIELD,
      name: argNode.name,
      value: argNode.value,
    })),
  };
  return print(sortValueNode(inputObjectWithArgs));
    return stringifyArguments(stream1) === stringifyArguments(stream2);
Fixed code: import { graphqlSync } from 'graphql/graphql.js';
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';

const schema = buildSchema('type Query { hello: String! }');
const source = `{ ${'hello '.repeat(250)}}`;

export const benchmark = {
  name: 'Many repeated fields',
  count: 5,
  measure() {
    graphqlSync({ schema, source });
  },
};
  it('different stream directive extra argument', () => {
    expectErrors(`
      fragment conflictingArgs on Dog {
        name @stream(label: "streamLabel", initialCount: 1)
        name @stream(label: "streamLabel", initialCount: 1, extraArg: true)
      }
    `).toDeepEqual([
      {
        message:
          'Fields "name" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
        locations: [
          { line: 3, column: 9 },
          { line: 4, column: 9 },
        ],
      },
    ]);
  });

  ValueNode,
    if (!sameArguments(node1, node2)) {
function sameArguments(
  node1: FieldNode | DirectiveNode,
  node2: FieldNode | DirectiveNode,
): boolean {
  const args1 = node1.arguments;
  const args2 = node2.arguments;

  if (args1 === undefined || args1.length === 0) {
    return args2 === undefined || args2.length === 0;
  }
  if (args2 === undefined || args2.length === 0) {
    return false;
  }

  if (args1.length !== args2.length) {
    return false;
  }

  const values2 = new Map(args2.map(({ name, value }) => [name.value, value]));
  return args1.every((arg1) => {
    const value1 = arg1.value;
    const value2 = values2.get(arg1.name.value);
    if (value2 === undefined) {
      return false;
    }

    return stringifyValue(value1) === stringifyValue(value2);
  });
}

function stringifyValue(value: ValueNode): string | null {
  return print(sortValueNode(value));
    return sameArguments(stream1, stream2);