【问题标题】:How do I send CSRF token authenticity from a php app that I own to a rails app that I own?如何将 CSRF 令牌真实性从我拥有的 php 应用程序发送到我拥有的 rails 应用程序?
【发布时间】:2015-09-04 17:30:49
【问题描述】:

我是一个sugarCRM 实例的管理员,我在heroku 上有一个rails 应用程序。如果在 SugarCRM 中添加联系人,我希望能够自动将联系人添加到 rails 应用程序。我在我的 SugarCRM 中写了一个 before_save logic_hook:

    function pushConts($bean, $event, $arguments)
    {
        $r = new HttpRequest('http://localhost:3000/contacts/', HttpRequest::METH_POST);
        $r->addPostFields(array('first_name' => $bean->first_name, 'last_name' => $bean->last_name, 'phone' => $bean->phone_mobile,'email' => $bean->email1, ));
        //$r->addHeaders(array('X-CSRF-Token'=> 'testing-csrf-token');
        try
        {
            echo $r->send()->getBody();
        } catch(HttpException $ex){
            SugarApplication::appendErrorMessage("<span style='color: red; font-size: 1.8em;'>Could Not Save Contact: Rails app is down.</span>");
            $queryParams = array('module' => 'Contacts', 'action' => 'ListView');
            SugarApplication::redirect('index.php?' . http_build_query($queryParams));          
        }

当我尝试按原样发​​送时,rails 应用程序的控制台输出是:

Started POST "/contacts/" for 127.0.0.1 at 2015-06-18 15:30:14 -0500
Processing by ContactsController#create as */*
Parameters: {"first_name"=>"contact", "last_name"=>"one", "phone"=>"(555) 555-5555", "email"=>"phone.support@example.us"}
Can't verify CSRF token authenticity
Completed 422 Unprocessable Entity in 1ms

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
actionpack (4.2.0) lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
actionpack (4.2.0) lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'
devise (3.4.1) lib/devise/controllers/helpers.rb:251:in `handle_unverified_request'

<stack trace continues>

我知道逻辑挂钩本身可以工作,因为我尝试将skip_before_filter :verify_authenticity_token 添加到我的联系人控制器,然后它按预期工作,但出于安全原因,这不是一个可行的解决方案。

正如您所看到的,我尝试发送带有标题的X-CSRF-Token,但这也不起作用。

我可以向这个逻辑挂钩或我的 rails 应用程序本身(或两者)添加什么,以便我可以将 Http 请求从我的 sugarCRM 发送到我的 rails 应用程序而不会损害(太多)安全性?

【问题讨论】:

    标签: php ruby-on-rails httprequest csrf sugarcrm


    【解决方案1】:

    Rails CSRF 系统并非真正旨在跨域或服务器工作。 It leverages synchronizer tokens (cryptographically random tokens) which are bound to the user's session.

    由于 Rails 和 SugarCRM 不共享用户会话,Rails 无法验证来自 SugarCRM 的 CSRF 令牌。

    最好的办法是使用skip_before_filter, only: [:create] 将其关闭。

    如果您需要真正安全的东西来验证请求来自您的 SugarCRM 服务器,您需要使用类似 token based authentication 的东西。

    许多基于 Rails 的 API 使用特殊的控制器和路由来执行可由 API 客户端执行的操作。在您的情况下,它看起来像这样:

    POST /api/v1/contacts

    # routes.rb
    namespace :api do
      namespace :v1 do
        resources :contacts
      end
    end
    
    # controllers/api/v1/api_controller
    class ApiController < ActionController::Base
      skip_before_filter, only: [:create] 
    
      def authenticate
        # @todo implement token based auth
      end
    end
    
    # controllers/api/v1/contacts_controller
    class Api::V1::ContactsController
    
      before_action :authenticate
    
      def create
        @contact = Contact.new(contact_params)
    
        # ...
      end
    
      # ...
    end
    

    【讨论】:

    • 所以我阅读了您链接的基于令牌的身份验证文章,如果我理解正确,我想在我的 HttpRequest 的标头中从 Sugar 发送一个令牌,然后在我的 Rails 应用程序中发送 authenticate_or_request_with_http_token是否会从 HttpRequest 的标头接收此令牌?但是糖怎么知道正确的标记是什么?而且,这种基于令牌的身份验证是完全独立的身份验证机制吗?这意味着我是否必须更改为所有控制器处理身份验证的方式?或者这就是skip_before_filter 的用途?
    猜你喜欢
    • 2015-07-14
    • 2021-10-17
    • 2020-10-18
    • 2011-01-23
    • 2023-01-13
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    相关资源
    最近更新 更多