@ocap/gql 1.30.25 → 1.30.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/http.d.mts CHANGED
@@ -32,6 +32,8 @@ interface HttpHandlerOptions {
32
32
  introspection?: boolean;
33
33
  showStack?: boolean;
34
34
  maxQueryDepth?: number;
35
+ maxTotalSelections?: number;
36
+ maxAliases?: number;
35
37
  }
36
38
  /**
37
39
  * Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
package/esm/http.mjs CHANGED
@@ -229,6 +229,8 @@ const defaultErrorHandler = (err) => {
229
229
  };
230
230
  const MAX_BATCH_SIZE = 40;
231
231
  const MAX_QUERY_DEPTH = 15;
232
+ const MAX_TOTAL_SELECTIONS = 1e3;
233
+ const MAX_ALIASES = 100;
232
234
  const BODY_SIZE_LIMIT = 100 * 1024;
233
235
  /**
234
236
  * Read the raw express request body as a utf-8 string, enforcing the size limit.
@@ -471,13 +473,110 @@ function getMaxSelectionDepth(documentAST, limit = MAX_QUERY_DEPTH + 1) {
471
473
  }
472
474
  return max;
473
475
  }
476
+ /**
477
+ * Count total expanded field selections and field aliases across all operations
478
+ * in the document, recursively expanding inline and named fragments with memoization.
479
+ * Bounds fragment + alias width amplification (e.g. K^D nested fragment spreads) in O(|AST|) time.
480
+ */
481
+ function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_SELECTIONS + 1, aliasLimit = MAX_ALIASES + 1) {
482
+ const fragments = /* @__PURE__ */ new Map();
483
+ const operations = [];
484
+ for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition" && def.name?.value) fragments.set(def.name.value, def.selectionSet?.selections || []);
485
+ else if (def.kind === "OperationDefinition") operations.push(def);
486
+ const fragMemo = /* @__PURE__ */ new Map();
487
+ const measure = (selections, visited = /* @__PURE__ */ new Set()) => {
488
+ let fields = 0;
489
+ let aliases = 0;
490
+ for (const sel of selections) {
491
+ if (fields >= selectionLimit && aliases >= aliasLimit) return {
492
+ fields: selectionLimit,
493
+ aliases: aliasLimit,
494
+ hitCycle: false
495
+ };
496
+ if (sel.kind === "Field") {
497
+ fields = Math.min(selectionLimit, fields + 1);
498
+ if (sel.alias?.value) aliases = Math.min(aliasLimit, aliases + 1);
499
+ const childSelections = sel.selectionSet?.selections || [];
500
+ if (childSelections.length > 0) {
501
+ const sub = measure(childSelections, visited);
502
+ if (sub.hitCycle) return {
503
+ fields: selectionLimit,
504
+ aliases: aliasLimit,
505
+ hitCycle: true
506
+ };
507
+ fields = Math.min(selectionLimit, fields + sub.fields);
508
+ aliases = Math.min(aliasLimit, aliases + sub.aliases);
509
+ }
510
+ } else if (sel.kind === "InlineFragment") {
511
+ const sub = measure(sel.selectionSet?.selections || [], visited);
512
+ if (sub.hitCycle) return {
513
+ fields: selectionLimit,
514
+ aliases: aliasLimit,
515
+ hitCycle: true
516
+ };
517
+ fields = Math.min(selectionLimit, fields + sub.fields);
518
+ aliases = Math.min(aliasLimit, aliases + sub.aliases);
519
+ } else if (sel.kind === "FragmentSpread" && sel.name?.value) {
520
+ const fragName = sel.name.value;
521
+ if (visited.has(fragName)) return {
522
+ fields: selectionLimit,
523
+ aliases: aliasLimit,
524
+ hitCycle: true
525
+ };
526
+ const cached = fragMemo.get(fragName);
527
+ if (cached !== void 0) {
528
+ fields = Math.min(selectionLimit, fields + cached.fields);
529
+ aliases = Math.min(aliasLimit, aliases + cached.aliases);
530
+ continue;
531
+ }
532
+ const nextVisited = new Set(visited);
533
+ nextVisited.add(fragName);
534
+ const sub = measure(fragments.get(fragName) || [], nextVisited);
535
+ if (sub.hitCycle) return {
536
+ fields: selectionLimit,
537
+ aliases: aliasLimit,
538
+ hitCycle: true
539
+ };
540
+ const clamped = {
541
+ fields: Math.min(selectionLimit, sub.fields),
542
+ aliases: Math.min(aliasLimit, sub.aliases)
543
+ };
544
+ fragMemo.set(fragName, clamped);
545
+ fields = Math.min(selectionLimit, fields + clamped.fields);
546
+ aliases = Math.min(aliasLimit, aliases + clamped.aliases);
547
+ }
548
+ }
549
+ return {
550
+ fields,
551
+ aliases,
552
+ hitCycle: false
553
+ };
554
+ };
555
+ let totalFields = 0;
556
+ let totalAliases = 0;
557
+ for (const op of operations) {
558
+ const sub = measure(op.selectionSet?.selections || []);
559
+ totalFields = Math.min(selectionLimit, totalFields + sub.fields);
560
+ totalAliases = Math.min(aliasLimit, totalAliases + sub.aliases);
561
+ if (totalFields >= selectionLimit && totalAliases >= aliasLimit) break;
562
+ }
563
+ return {
564
+ fields: totalFields,
565
+ aliases: totalAliases
566
+ };
567
+ }
568
+ const parseEnvPositiveInt = (raw) => {
569
+ if (raw === void 0 || raw.trim() === "") return void 0;
570
+ const parsed = Number.parseInt(raw.trim(), 10);
571
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : void 0;
572
+ };
474
573
  const parseEnvBoolean = (raw) => {
475
574
  if (raw === void 0 || raw.trim() === "") return void 0;
476
575
  const normalized = raw.trim().toLowerCase();
477
576
  if (normalized === "1" || normalized === "true") return true;
478
577
  if (normalized === "0" || normalized === "false") return false;
479
578
  };
480
- const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH }) => {
579
+ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
481
580
  const schema = buildSchema(schemaSource);
482
581
  const rootValue = createResolvers(resolver);
483
582
  const formatGraphQLError = (err) => {
@@ -536,6 +635,13 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
536
635
  if (maxQueryDepth > 0) {
537
636
  if (getMaxSelectionDepth(documentAST, maxQueryDepth + 1) > maxQueryDepth) throw new Error(`Query depth exceeded allowed maximum depth of ${maxQueryDepth}`);
538
637
  }
638
+ if (maxTotalSelections > 0 || maxAliases > 0) {
639
+ const selLimit = maxTotalSelections > 0 ? maxTotalSelections + 1 : Number.MAX_SAFE_INTEGER;
640
+ const aliasLimit = maxAliases > 0 ? maxAliases + 1 : Number.MAX_SAFE_INTEGER;
641
+ const { fields, aliases } = countTotalSelectionsAndAliases(documentAST, selLimit, aliasLimit);
642
+ if (maxTotalSelections > 0 && fields > maxTotalSelections) throw new Error(`Query complexity exceeded allowed maximum of ${maxTotalSelections} total field selections`);
643
+ if (maxAliases > 0 && aliases > maxAliases) throw new Error(`Query aliases exceeded allowed maximum of ${maxAliases} aliases`);
644
+ }
539
645
  } catch (batchError) {
540
646
  return sendJSON(500, { errors: [formatAnyError(batchError)] });
541
647
  }
package/lib/http.cjs CHANGED
@@ -232,6 +232,8 @@ const defaultErrorHandler = (err) => {
232
232
  };
233
233
  const MAX_BATCH_SIZE = 40;
234
234
  const MAX_QUERY_DEPTH = 15;
235
+ const MAX_TOTAL_SELECTIONS = 1e3;
236
+ const MAX_ALIASES = 100;
235
237
  const BODY_SIZE_LIMIT = 100 * 1024;
236
238
  /**
237
239
  * Read the raw express request body as a utf-8 string, enforcing the size limit.
@@ -474,13 +476,110 @@ function getMaxSelectionDepth(documentAST, limit = MAX_QUERY_DEPTH + 1) {
474
476
  }
475
477
  return max;
476
478
  }
479
+ /**
480
+ * Count total expanded field selections and field aliases across all operations
481
+ * in the document, recursively expanding inline and named fragments with memoization.
482
+ * Bounds fragment + alias width amplification (e.g. K^D nested fragment spreads) in O(|AST|) time.
483
+ */
484
+ function countTotalSelectionsAndAliases(documentAST, selectionLimit = MAX_TOTAL_SELECTIONS + 1, aliasLimit = MAX_ALIASES + 1) {
485
+ const fragments = /* @__PURE__ */ new Map();
486
+ const operations = [];
487
+ for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition" && def.name?.value) fragments.set(def.name.value, def.selectionSet?.selections || []);
488
+ else if (def.kind === "OperationDefinition") operations.push(def);
489
+ const fragMemo = /* @__PURE__ */ new Map();
490
+ const measure = (selections, visited = /* @__PURE__ */ new Set()) => {
491
+ let fields = 0;
492
+ let aliases = 0;
493
+ for (const sel of selections) {
494
+ if (fields >= selectionLimit && aliases >= aliasLimit) return {
495
+ fields: selectionLimit,
496
+ aliases: aliasLimit,
497
+ hitCycle: false
498
+ };
499
+ if (sel.kind === "Field") {
500
+ fields = Math.min(selectionLimit, fields + 1);
501
+ if (sel.alias?.value) aliases = Math.min(aliasLimit, aliases + 1);
502
+ const childSelections = sel.selectionSet?.selections || [];
503
+ if (childSelections.length > 0) {
504
+ const sub = measure(childSelections, visited);
505
+ if (sub.hitCycle) return {
506
+ fields: selectionLimit,
507
+ aliases: aliasLimit,
508
+ hitCycle: true
509
+ };
510
+ fields = Math.min(selectionLimit, fields + sub.fields);
511
+ aliases = Math.min(aliasLimit, aliases + sub.aliases);
512
+ }
513
+ } else if (sel.kind === "InlineFragment") {
514
+ const sub = measure(sel.selectionSet?.selections || [], visited);
515
+ if (sub.hitCycle) return {
516
+ fields: selectionLimit,
517
+ aliases: aliasLimit,
518
+ hitCycle: true
519
+ };
520
+ fields = Math.min(selectionLimit, fields + sub.fields);
521
+ aliases = Math.min(aliasLimit, aliases + sub.aliases);
522
+ } else if (sel.kind === "FragmentSpread" && sel.name?.value) {
523
+ const fragName = sel.name.value;
524
+ if (visited.has(fragName)) return {
525
+ fields: selectionLimit,
526
+ aliases: aliasLimit,
527
+ hitCycle: true
528
+ };
529
+ const cached = fragMemo.get(fragName);
530
+ if (cached !== void 0) {
531
+ fields = Math.min(selectionLimit, fields + cached.fields);
532
+ aliases = Math.min(aliasLimit, aliases + cached.aliases);
533
+ continue;
534
+ }
535
+ const nextVisited = new Set(visited);
536
+ nextVisited.add(fragName);
537
+ const sub = measure(fragments.get(fragName) || [], nextVisited);
538
+ if (sub.hitCycle) return {
539
+ fields: selectionLimit,
540
+ aliases: aliasLimit,
541
+ hitCycle: true
542
+ };
543
+ const clamped = {
544
+ fields: Math.min(selectionLimit, sub.fields),
545
+ aliases: Math.min(aliasLimit, sub.aliases)
546
+ };
547
+ fragMemo.set(fragName, clamped);
548
+ fields = Math.min(selectionLimit, fields + clamped.fields);
549
+ aliases = Math.min(aliasLimit, aliases + clamped.aliases);
550
+ }
551
+ }
552
+ return {
553
+ fields,
554
+ aliases,
555
+ hitCycle: false
556
+ };
557
+ };
558
+ let totalFields = 0;
559
+ let totalAliases = 0;
560
+ for (const op of operations) {
561
+ const sub = measure(op.selectionSet?.selections || []);
562
+ totalFields = Math.min(selectionLimit, totalFields + sub.fields);
563
+ totalAliases = Math.min(aliasLimit, totalAliases + sub.aliases);
564
+ if (totalFields >= selectionLimit && totalAliases >= aliasLimit) break;
565
+ }
566
+ return {
567
+ fields: totalFields,
568
+ aliases: totalAliases
569
+ };
570
+ }
571
+ const parseEnvPositiveInt = (raw) => {
572
+ if (raw === void 0 || raw.trim() === "") return void 0;
573
+ const parsed = Number.parseInt(raw.trim(), 10);
574
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : void 0;
575
+ };
477
576
  const parseEnvBoolean = (raw) => {
478
577
  if (raw === void 0 || raw.trim() === "") return void 0;
479
578
  const normalized = raw.trim().toLowerCase();
480
579
  if (normalized === "1" || normalized === "true") return true;
481
580
  if (normalized === "0" || normalized === "false") return false;
482
581
  };
483
- const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH }) => {
582
+ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
484
583
  const schema = (0, graphql.buildSchema)(_ocap_schema.default);
485
584
  const rootValue = createResolvers(resolver);
486
585
  const formatGraphQLError = (err) => {
@@ -539,6 +638,13 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
539
638
  if (maxQueryDepth > 0) {
540
639
  if (getMaxSelectionDepth(documentAST, maxQueryDepth + 1) > maxQueryDepth) throw new Error(`Query depth exceeded allowed maximum depth of ${maxQueryDepth}`);
541
640
  }
641
+ if (maxTotalSelections > 0 || maxAliases > 0) {
642
+ const selLimit = maxTotalSelections > 0 ? maxTotalSelections + 1 : Number.MAX_SAFE_INTEGER;
643
+ const aliasLimit = maxAliases > 0 ? maxAliases + 1 : Number.MAX_SAFE_INTEGER;
644
+ const { fields, aliases } = countTotalSelectionsAndAliases(documentAST, selLimit, aliasLimit);
645
+ if (maxTotalSelections > 0 && fields > maxTotalSelections) throw new Error(`Query complexity exceeded allowed maximum of ${maxTotalSelections} total field selections`);
646
+ if (maxAliases > 0 && aliases > maxAliases) throw new Error(`Query aliases exceeded allowed maximum of ${maxAliases} aliases`);
647
+ }
542
648
  } catch (batchError) {
543
649
  return sendJSON(500, { errors: [formatAnyError(batchError)] });
544
650
  }
package/lib/http.d.cts CHANGED
@@ -32,6 +32,8 @@ interface HttpHandlerOptions {
32
32
  introspection?: boolean;
33
33
  showStack?: boolean;
34
34
  maxQueryDepth?: number;
35
+ maxTotalSelections?: number;
36
+ maxAliases?: number;
35
37
  }
36
38
  /**
37
39
  * Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.30.25",
6
+ "version": "1.30.26",
7
7
  "description": "Resolver middleware for ocap adapters",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -51,8 +51,8 @@
51
51
  ],
52
52
  "license": "MIT",
53
53
  "dependencies": {
54
- "@arcblock/ws": "1.30.25",
55
- "@ocap/schema": "1.30.25",
54
+ "@arcblock/ws": "1.30.26",
55
+ "@ocap/schema": "1.30.26",
56
56
  "graphql": "^16.13.2",
57
57
  "lodash": "^4.17.23"
58
58
  },