Skip to content

Commit a9580e0

Browse files
fix: disable graphql introspection queries when disableIntrospectionInProduction is true (#12982)
1 parent 57d00ad commit a9580e0

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

docs/graphql/overview.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ The labels you provide for your Collections and Globals are used to name the Gra
1616

1717
At the top of your Payload Config you can define all the options to manage GraphQL.
1818

19-
| Option | Description |
20-
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
21-
| `mutations` | Any custom Mutations to be added in addition to what Payload provides. [More](/docs/graphql/extending) |
22-
| `queries` | Any custom Queries to be added in addition to what Payload provides. [More](/docs/graphql/extending) |
23-
| `maxComplexity` | A number used to set the maximum allowed complexity allowed by requests [More](/docs/graphql/overview#query-complexity-limits) |
24-
| `disablePlaygroundInProduction` | A boolean that if false will enable the GraphQL playground, defaults to true. [More](/docs/graphql/overview#graphql-playground) |
25-
| `disable` | A boolean that if true will disable the GraphQL entirely, defaults to false. |
26-
| `validationRules` | A function that takes the ExecutionArgs and returns an array of ValidationRules. |
19+
| Option | Description |
20+
| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
21+
| `mutations` | Any custom Mutations to be added in addition to what Payload provides. [More](/docs/graphql/extending) |
22+
| `queries` | Any custom Queries to be added in addition to what Payload provides. [More](/docs/graphql/extending) |
23+
| `maxComplexity` | A number used to set the maximum allowed complexity allowed by requests [More](/docs/graphql/overview#query-complexity-limits) |
24+
| `disablePlaygroundInProduction` | A boolean that if false will enable the GraphQL playground in production environments, defaults to true. [More](/docs/graphql/overview#graphql-playground) |
25+
| `disableIntrospectionInProduction` | A boolean that if false will enable the GraphQL introspection in production environments, defaults to true. |
26+
| `disable` | A boolean that if true will disable the GraphQL entirely, defaults to false. |
27+
| `validationRules` | A function that takes the ExecutionArgs and returns an array of ValidationRules. |
2728

2829
## Collections
2930

packages/graphql/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export function configToSchema(config: SanitizedConfig): {
113113
variables: args.variableValues,
114114
// onComplete: (complexity) => { console.log('Query Complexity:', complexity); },
115115
}),
116+
...(config.graphQL.disableIntrospectionInProduction ? [NoProductionIntrospection] : []),
116117
...(typeof config?.graphQL?.validationRules === 'function'
117118
? config.graphQL.validationRules(args)
118119
: []),
@@ -123,3 +124,18 @@ export function configToSchema(config: SanitizedConfig): {
123124
validationRules,
124125
}
125126
}
127+
128+
const NoProductionIntrospection: GraphQL.ValidationRule = (context) => ({
129+
Field(node) {
130+
if (process.env.NODE_ENV === 'production') {
131+
if (node.name.value === '__schema' || node.name.value === '__type') {
132+
context.reportError(
133+
new GraphQL.GraphQLError(
134+
'GraphQL introspection is not allowed, but the query contained __schema or __type',
135+
{ nodes: [node] },
136+
),
137+
)
138+
}
139+
}
140+
},
141+
})

packages/payload/src/config/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export const addDefaultsToConfig = (config: Config): Config => {
123123
config.endpoints = config.endpoints ?? []
124124
config.globals = config.globals ?? []
125125
config.graphQL = {
126+
disableIntrospectionInProduction: true,
126127
disablePlaygroundInProduction: true,
127128
maxComplexity: 1000,
128129
schemaOutputFile: `${typeof process?.cwd === 'function' ? process.cwd() : ''}/schema.graphql`,

packages/payload/src/config/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,17 @@ export type Config = {
10291029
*/
10301030
graphQL?: {
10311031
disable?: boolean
1032+
/**
1033+
* Disable introspection queries in production.
1034+
*
1035+
* @default true
1036+
*/
1037+
disableIntrospectionInProduction?: boolean
1038+
/**
1039+
* Disable the GraphQL Playground in production.
1040+
*
1041+
* @default true
1042+
*/
10321043
disablePlaygroundInProduction?: boolean
10331044
maxComplexity?: number
10341045
/**

0 commit comments

Comments
 (0)