-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add allowedHeaders
option to allow filtering Response and Request headers
#3188
Open
EmrysMyrddin
wants to merge
6
commits into
main
Choose a base branch
from
feat/allowed-response-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5eac198
remove addPlugin trick, not necessary with latest envelop
EmrysMyrddin d90d74d
add allowed headers options
EmrysMyrddin d83f244
changeset
EmrysMyrddin 28ad3bd
add jsdoc for new options
EmrysMyrddin 1928261
Update packages/graphql-yoga/src/server.ts
EmrysMyrddin 2b34a0e
chore(dependencies): updated changesets for modified dependencies
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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/nestjs': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
10 changes: 10 additions & 0 deletions
10
.changeset/@graphql-yoga_nestjs-federation-3188-dependencies.md
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,10 @@ | ||
--- | ||
'@graphql-yoga/nestjs-federation': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@graphql-yoga/nestjs@3.2.0` | ||
↗︎](https://www.npmjs.com/package/@graphql-yoga/nestjs/v/3.2.0) (from `3.1.1`, in | ||
`dependencies`) | ||
- Updated dependency [`@graphql-yoga/plugin-apollo-inline-trace@3.2.0` | ||
↗︎](https://www.npmjs.com/package/@graphql-yoga/plugin-apollo-inline-trace/v/3.2.0) (from | ||
`3.1.1`, in `dependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-apollo-inline-trace-3188-dependencies.md
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/plugin-apollo-inline-trace': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
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/plugin-apq': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-csrf-prevention-3188-dependencies.md
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/plugin-csrf-prevention': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-defer-stream-3188-dependencies.md
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/plugin-defer-stream': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-disable-introspection-3188-dependencies.md
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/plugin-disable-introspection': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-graphql-sse-3188-dependencies.md
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/plugin-graphql-sse': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
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/plugin-jwt': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-persisted-operations-3188-dependencies.md
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/plugin-persisted-operations': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
9 changes: 9 additions & 0 deletions
9
.changeset/@graphql-yoga_plugin-prometheus-3188-dependencies.md
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,9 @@ | ||
--- | ||
'@graphql-yoga/plugin-prometheus': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@envelop/prometheus@^9.4.0` | ||
↗︎](https://www.npmjs.com/package/@envelop/prometheus/v/9.4.0) (from `^9.3.1`, in | ||
`dependencies`) | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_plugin-response-cache-3188-dependencies.md
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/plugin-response-cache': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
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/plugin-sofa': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
6 changes: 6 additions & 0 deletions
6
.changeset/@graphql-yoga_render-graphiql-3188-dependencies.md
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/render-graphiql': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, in `peerDependencies`) |
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,5 @@ | ||
--- | ||
'graphql-yoga': minor | ||
--- | ||
|
||
Add `allowedHeaders` option to allow filtering Response and Request headers |
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,10 @@ | ||
--- | ||
'graphql-yoga': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`@graphql-tools/executor@^1.2.2` | ||
↗︎](https://www.npmjs.com/package/@graphql-tools/executor/v/1.2.2) (from `^1.0.0`, in | ||
`dependencies`) | ||
- Updated dependency [`@graphql-tools/utils@^10.1.0` | ||
↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.1.0) (from `^10.0.0`, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'graphql-yoga-cloud-run-guide': patch | ||
--- | ||
dependencies updates: | ||
- Updated dependency [`graphql-yoga@^5.2.0` | ||
↗︎](https://www.npmjs.com/package/graphql-yoga/v/5.2.0) (from `^5.1.1`, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createSchema } from '../schema'; | ||
import { createYoga } from '../server'; | ||
import { useAllowedResponseHeaders } from './allowed-headers'; | ||
import { Plugin } from './types'; | ||
|
||
describe('useAllowedHeaders', () => { | ||
it('should strip headers from responses', async () => { | ||
const response = await query({ | ||
plugins: [ | ||
useAllowedResponseHeaders(['content-type', 'content-length', 'x-allowed-custom-header']), | ||
], | ||
responseHeaders: { | ||
'x-allowed-custom-header': 'value', | ||
// Verify that we can strip 2 headers in a row | ||
'x-disallowed-custom-header-1': 'value', | ||
'x-disallowed-custom-header-2': 'value', | ||
}, | ||
}); | ||
|
||
expect(response.headers.get('x-allowed-custom-header')).toEqual('value'); | ||
expect(response.headers.get('x-disallowed-custom-header-1')).toBeNull(); | ||
expect(response.headers.get('x-disallowed-custom-header-2')).toBeNull(); | ||
}); | ||
|
||
const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
_: String | ||
} | ||
`, | ||
}); | ||
|
||
function query({ | ||
responseHeaders = {}, | ||
requestHeaders = {}, | ||
plugins = [], | ||
}: { | ||
requestHeaders?: Record<string, string>; | ||
responseHeaders?: Record<string, string>; | ||
plugins?: Plugin[]; | ||
} = {}) { | ||
const yoga = createYoga({ | ||
schema, | ||
plugins: [ | ||
{ | ||
onResponse: ({ response }) => { | ||
for (const [header, value] of Object.entries(responseHeaders)) { | ||
response.headers.set(header, value); | ||
} | ||
}, | ||
}, | ||
...plugins, | ||
], | ||
}); | ||
return yoga.fetch('/graphql', { | ||
body: JSON.stringify({ query: '{ __typename }' }), | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
...requestHeaders, | ||
}, | ||
}); | ||
} | ||
}); |
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,25 @@ | ||
import { Plugin } from './types.js'; | ||
|
||
export function useAllowedResponseHeaders(allowedHeaders: string[]): Plugin { | ||
return { | ||
onResponse({ response }) { | ||
removeDisallowedHeaders(response.headers, allowedHeaders); | ||
}, | ||
}; | ||
} | ||
|
||
export function useAllowedRequestHeaders(allowedHeaders: string[]): Plugin { | ||
return { | ||
onRequest({ request }) { | ||
removeDisallowedHeaders(request.headers, allowedHeaders); | ||
}, | ||
}; | ||
} | ||
|
||
function removeDisallowedHeaders(headers: Headers, allowedHeaders: string[]) { | ||
for (const headerName of headers.keys()) { | ||
if (!allowedHeaders.includes(headerName)) { | ||
headers.delete(headerName); | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this ensures that those plugins are always at the end. Otherwise plugins that add new plugins come after these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addPlugin
no longer adds plugins at the end of the plugin list. It adds the plugin in place, so this is no longer needed :-)