Skip to content
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

Fixing ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken) #19

Open
ghost opened this issue Mar 20, 2020 · 5 comments

Comments

@ghost
Copy link

ghost commented Mar 20, 2020

just started following you tutorial (rails and graphql newbie) and got this error on my first query

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  
actionpack (6.0.2.2) lib/action_controller/metal/request_forgery_protection.rb:217:in `handle_unverified_request'
actionpack (6.0.2.2) lib/action_controller/metal/request_forgery_protection.rb:249:in `handle_unverified_request'
...

The fix was to

class GraphqlController < ApplicationController
  # If accessing from outside this domain, nullify the session
  # This allows for outside API access while preventing CSRF attacks,
  # but you'll have to authenticate your user separately
  # protect_from_forgery with: :null_session
  skip_before_action :verify_authenticity_token

def execute
...

(could also have chosen to uncomment the protect_from_forgery with: :null_session that was given by the graphql generator but followed the recommendation from https://stackoverflow.com/a/34252150/618099 )

just wanted to share that with you and others trying out the tutorial.

@DmitryTsepelev
Copy link
Contributor

Hi @NorfeldtAbtion! In first part of the tutorial we configure our front–end to send a proper CSRF token with each request (in the paragraph starting with Back to coding. Since we use HTTP POST as a transport, we need to attach a proper CSRF token...), have you tried it out? Sending CSRF token without disabling verification makes your app more secure 🙂

@ghost
Copy link
Author

ghost commented Mar 26, 2020

I realized that when I got further down - just wanted to test the queries like described in the tutorial.
Perhaps it would make sense to have it insecure in the start the tutorial and then teach how to make it secure (as you do later in the tutorial)

It's a great tutorial - I'm learning a lot.

@DmitryTsepelev
Copy link
Contributor

Great, I'm happy we've figured it out! Closing the issue for now, don't hesitate to reach out if you have any troubles

@ghost
Copy link
Author

ghost commented Mar 27, 2020

Great, I'm happy we've figured it out! Closing the issue for now, don't hesitate to reach out if you have any troubles

@DmitryTsepelev I still have one question related to this topic. I got to the part 3 and can't seem to figure out how to get GraphiQL to show the scheme with the current auth.

How did you get it to show:

graph schema

Guessing that I need to set the headers - but unsure about how to do the CSFR token

Screenshot 2020-03-27 at 14 16 02

@DmitryTsepelev
Copy link
Contributor

Looks like graphiql-rails should set up CSRF token for us, no action should be required. Could you please make sure it's being correctly set up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant