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

nil can't be coerced into Fixnum #10

Open
fbjork opened this issue Jun 14, 2017 · 3 comments
Open

nil can't be coerced into Fixnum #10

fbjork opened this issue Jun 14, 2017 · 3 comments

Comments

@fbjork
Copy link

fbjork commented Jun 14, 2017

When I share a session created in Phoenix with my Rails 4.2 app I get the following exception in my views when calling form_authenticity_token or csrf_meta_tags. Any ideas?

nil can't be coerced into Fixnum

comes from this line:

vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.8/lib/action_controller/metal/request_forgery_protection.rb:321

def xor_byte_strings(s1, s2)
  s1.bytes.zip(s2.bytes).map { |(c1,c2)| c1 ^ c2 }.pack('c*')
end
@josevalim
Copy link

The tokens generated by Rails and Plug seem to have different length and Rails does not expect that:

irb(main):004:0> [1,2,3].zip([4,5]).map { |(c1,c2)| c1 ^ c2 }
TypeError: nil can't be coerced into Fixnum
	from (irb):4:in `^'

The best solution here is probably to guarantee that Rails and Plug use different CSRF tokens names in the session. I have just pushed a commit to Plug that will allow us to do so. In your deps in mix.exs:

{:plug, github: "elixir-lang/plug", override: true}

And in your router:

plug :protect_from_forgery, session_key: "_my_csrf_token_key"

Please let me know how it goes. The downside of this approach is that you can't POST from a Phoenix page to a Rails page though. If that's important, it may be necessary to guarantee their CSRF tokens are compatible (which would probably be an effort for this project).

Also note you will be using Plug master, which is unreleased, so if you find any failure, please let us know.

@paulnicholson
Copy link

@fbjork I just saw this issue, not sure if you already solved your problem. I created plug_rails_csrf_protection for this very reason. It's not just the token length but also how it is encoded/masked for the form that is different between Rails and the CSRFProtection plug. I have successfully used plug_rails_csrf_protection to post between Rails and Phoenix.

@cconstantin
Copy link
Owner

I missed this issue, duh. Is this still a problem?

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

4 participants