8

I am using PaypalAdaptive. It sends ipn_notification properly. ipnNotification action method is as following -

def ipn_notification
    ipn = PaypalAdaptive::IpnNotification.new
    ipn.send_back(request.raw_post.to_json)

    print "=====================request.raw_post#{request.raw_post}=============="

    if ipn.verified?
        PaymentMailer.notify_unknown(request.raw_post).deliver
    else
        logger.info "IT DIDNT WORK"
    end
    render :nothing => true
end

but it's returning error

WARNING: Can't verify CSRF token authenticity rails

Any help for this problem.

3 Answers 3

19

In your controller:

skip_before_filter :verify_authenticity_token, :only => [:ipn_notification]

For people reading to quickly and distribute -1 (skipping an important part: it's not a POST call from the client...):

  • yes it skips a security BUT... Read after...

  • yes, it's the only way for external website POST requests

  • yes it's safe: you obviously check params and keys when receiving a call from Paypal or alike.

8
  • Answers are better when explained, too ;) Jul 18, 2013 at 11:08
  • It is not the very short answer that made me -1 it. It is the missing explanation of the security implications that come with an implementation like yours. You basically bypass the CSRF protection .. which is a bad thing in combination with payment ;) Jul 18, 2013 at 12:53
  • @matthiaskrull is there any alternative when receiving post from tierce services? I dont think so. Since the response is then checked using shared auth keys, it's not an issue. Jul 18, 2013 at 12:55
  • I don't see any. But that is not the point I wanted to make. Maybe it is just me but I think it is worth mentioning what a line of code really does if it changes security relevant behavior. Jul 18, 2013 at 13:07
  • @matthiaskrull when there is no alternative and the answer is good, your remark makes a great comment. I leave you happy with blaming power Jul 18, 2013 at 13:11
15

The correct solution for this problem without compromising security

In your ajax request send the csrf token value as header.

var csrfToken = $("meta[name='csrf-token']").attr("content");
$.ajaxSetup({
  headers: {
    'X-CSRF-Token': csrfToken
  }
});
2
  • 1
    @Magnum better approach? Maybe, alas, it does not answer the question – apneadiving 1 hour ago Aug 21, 2013 at 14:42
  • That is why I said it is 'better approach' not 'answer'
    – Grey
    Aug 22, 2013 at 9:56
4

Add the following line in your application.js

//= require jquery_ujs

And try.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.