-
Notifications
You must be signed in to change notification settings - Fork 1k
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
feature: Support GraphQL Trusted Documents aka Persisted Operations for added security #9416
Conversation
@Tobbe since @Josh-Walker-GM and I paired, your review is appreciated. Note the way the gql and graphql function is imported. |
I tried to reproduce locally, here what I did:
RSC app seems to work ok: |
Thanks! Co-authored-by: Tobbe Lundberg <[email protected]>
I'll merge this one even though it fails one of the RSC tests. @dthyresson and I looked at this together, and I'm fairly sure this is down to a limitation in |
this is huge! |
@n1ru4l would be so happy |
RedwoodJS can be setup to enforce persisted operations -- alternatively called Trusted Documents.
Use trusted documents if your GraphQL API is only for your own apps (which is the case for most GraphQL APIs) for a massively decreased attack-surface, increased performance, and decreased bandwidth usage.
At app build time, Redwood will extract the GraphQL documents (queries, etc) and make them available to the server. At run time, you can then send documentId or "hash" instead of the whole document; only accept requests with a documentId and one that it knows about.
This prevents malicious attackers from executing arbitrary GraphQL thus helping with unwanted resolver traversal or information leaking.
This PR:
graphql
project toml configUpdates GraphQL Code gen to use the client preset which generates the following including documents with the query/mutation and hash

Has some smarts to make the
gql
-tag compatible with the trusted documents' use of the graphql function that can access the precomputed document hashUpdates the Apollo web client to use the persistent query link to just send the document hash
Configure GraphQL Handler to use the Trusted Document store
customErrors: { persistedQueryOnly: 'really trust me' },
Is sent rather than full query.
This PR also updates the GraphQL docs.
Issues
The way the gql and graphql function is "swapped" could be refactored.
In this PR, the auto-import plugin is used:
So, if the toml says to use trustedDocuments, either gql from graphql-tag is used or we import a aliased function that was generated as part of the client present
replaceGqlTagWithTrustedDocumentGraphql
function.See: packages/internal/src/generate/trustedDocuments.ts
The graphql aliased function is appended:
This is done because the babel auto import plugin cannot use aliases.
However, one could make the case for writing a proper babel plugin instead of appending this function and importing.
This can be a refactor.