graphql/yoga-server/docs/features/csrf-prevention #2740
Replies: 2 comments 1 reply
-
Hey @enri90, I saw that you downvoted this page. Is there any specific reason for this? Is this page not teaching what you expected? Is there anything we can improve? |
Beta Was this translation helpful? Give feedback.
1 reply
-
i tried to build with a plugin in yoga import { ServerAdapterPlugin } from '@whatwg-node/server';
import csrf from 'csrf';
const tokens = new csrf();
const secret = process.env.SECRET_CSRF as string;
declare global {
interface Request {
tokenCsrf: string;
}
}
export function useCSRF<TServerContext>(): ServerAdapterPlugin<TServerContext> {
return {
onRequest({ request }) {
request.tokenCsrf = request.headers.get('X-CSRF-Token') || '';
},
onResponse({ request, response }) {
if (request.method === 'OPTIONS') {
response.headers.set('X-CSRF-Token', tokens.create(secret));
}
},
};
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
graphql/yoga-server/docs/features/csrf-prevention
If you have CORS enabled, almost all requests coming from the browser will have a preflight request - however, some requests are deemed "simple" and don't make a preflight.
https://the-guild.dev/graphql/yoga-server/docs/features/csrf-prevention
Beta Was this translation helpful? Give feedback.
All reactions