-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: disable introspection plugin (#2231)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a0576a5
commit c5b1cc4
Showing
8 changed files
with
238 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'graphql-yoga': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/parser-cache@^5.0.4` ↗︎](https://www.npmjs.com/package/@envelop/parser-cache/v/5.0.4) (from `5.0.4`, in `dependencies`) | ||
- Updated dependency [`@envelop/validation-cache@^5.0.5` ↗︎](https://www.npmjs.com/package/@envelop/validation-cache/v/5.0.5) (from `5.0.4`, in `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
packages/plugins/disable-introspection/__tests__/disable-introspection.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { useDisableIntrospection } from '@graphql-yoga/plugin-disable-introspection' | ||
import { createYoga, createSchema } from 'graphql-yoga' | ||
|
||
describe('disable introspection', () => { | ||
test('can disable introspection', async () => { | ||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
_: Boolean | ||
} | ||
`, | ||
}) | ||
|
||
const yoga = createYoga({ schema, plugins: [useDisableIntrospection()] }) | ||
|
||
const response = await yoga.fetch('http://yoga/graphql', { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ query: `{ __schema { types { name } } }` }), | ||
}) | ||
|
||
expect(response.status).toEqual(200) | ||
const result = await response.json() | ||
expect(result.data).toEqual(undefined) | ||
expect(result.errors).toHaveLength(2) | ||
}) | ||
|
||
test('can disable introspection conditionally', async () => { | ||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
_: Boolean | ||
} | ||
`, | ||
}) | ||
|
||
const yoga = createYoga({ | ||
schema, | ||
plugins: [ | ||
useDisableIntrospection({ | ||
isDisabled: () => true, | ||
}), | ||
], | ||
}) | ||
|
||
const response = await yoga.fetch('http://yoga/graphql', { | ||
method: 'POST', | ||
headers: { 'content-type': 'application/json' }, | ||
body: JSON.stringify({ query: `{ __schema { types { name } } }` }), | ||
}) | ||
|
||
expect(response.status).toEqual(200) | ||
const result = await response.json() | ||
expect(result.data).toEqual(undefined) | ||
expect(result.errors).toHaveLength(2) | ||
}) | ||
|
||
test('can disable introspection based on headers', async () => { | ||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
_: Boolean | ||
} | ||
`, | ||
}) | ||
|
||
const yoga = createYoga({ | ||
schema, | ||
plugins: [ | ||
useDisableIntrospection({ | ||
isDisabled: (request) => | ||
request.headers.get('x-disable-introspection') === '1', | ||
}), | ||
], | ||
// uncomment this and the tests will pass | ||
// validationCache: false, | ||
}) | ||
|
||
// First request uses the header to disable introspection | ||
let response = await yoga.fetch('http://yoga/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
'x-disable-introspection': '1', | ||
}, | ||
body: JSON.stringify({ query: `{ __schema { types { name } } }` }), | ||
}) | ||
|
||
expect(response.status).toEqual(200) | ||
let result = await response.json() | ||
expect(result.data).toEqual(undefined) | ||
expect(result.errors).toHaveLength(2) | ||
|
||
// Seconds request does not disable introspection | ||
response = await yoga.fetch('http://yoga/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ query: `{ __schema { types { name } } }` }), | ||
}) | ||
|
||
expect(response.status).toEqual(200) | ||
result = await response.json() | ||
expect(result.data).toBeDefined() | ||
expect(result.errors).toBeUndefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "@graphql-yoga/plugin-disable-introspection", | ||
"version": "0.0.0", | ||
"description": "Disable Introspection plugin for GraphQL Yoga.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/dotansimha/graphql-yoga.git", | ||
"directory": "packages/plugins/disable-introspection" | ||
}, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"scripts": { | ||
"check": "tsc --pretty --noEmit" | ||
}, | ||
"author": "Laurin Quast <laurinquast@googlemail.com>", | ||
"license": "MIT", | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/typings/index.d.cts", | ||
"default": "./dist/cjs/index.js" | ||
}, | ||
"import": { | ||
"types": "./dist/typings/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
}, | ||
"default": { | ||
"types": "./dist/typings/index.d.ts", | ||
"default": "./dist/esm/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"typings": "dist/typings/index.d.ts", | ||
"typescript": { | ||
"definition": "dist/typings/index.d.ts" | ||
}, | ||
"publishConfig": { | ||
"directory": "dist", | ||
"access": "public" | ||
}, | ||
"peerDependencies": { | ||
"graphql-yoga": "^3.1.1", | ||
"graphql": "^15.2.0 || ^16.0.0" | ||
}, | ||
"devDependencies": { | ||
"graphql-yoga": "workspace:*" | ||
}, | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { Plugin, PromiseOrValue } from 'graphql-yoga' | ||
import { NoSchemaIntrospectionCustomRule } from 'graphql' | ||
|
||
type UseDisableIntrospectionArgs = { | ||
isDisabled?: (request: Request) => PromiseOrValue<boolean> | ||
} | ||
|
||
const store = new WeakMap<Request, boolean>() | ||
|
||
export const useDisableIntrospection = ( | ||
props?: UseDisableIntrospectionArgs, | ||
): Plugin => { | ||
return { | ||
async onRequest({ request }) { | ||
const isDisabled = props?.isDisabled | ||
? await props.isDisabled(request) | ||
: true | ||
store.set(request, isDisabled) | ||
}, | ||
onValidate({ addValidationRule, context }) { | ||
const isDisabled = store.get(context.request) ?? true | ||
if (isDisabled) { | ||
addValidationRule(NoSchemaIntrospectionCustomRule) | ||
} | ||
}, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters