pusher/pusher-http-ruby

Accessing Rack::Request changed in rails 5?

l85m opened this issue · 1 comments

l85m commented

Webhooks keep failing due to authentication issues in my rails 5 app. Looks like maybe the type of the request object has changed.

Relevant Gem Versions:

  • ruby (2.2.4)
  • pusher (1.2.0)
  • rails (5.0.0.1)
  • rack (2.0.1)
class SomethingController < ApplicationController

  def webhook
    webhook = Pusher::WebHook.new(request)
    if webhook.valid?
      webhook.events.each do |event|
        p event.inspect
      end
      render plain: 'ok'
    else
      render plain: 'invalid', status: 401
    end
  end

Webhooks failing with message:

Received webhook with unknown key:

Inspecting the request object in the controller shows that the key and signature are properly set:

 @env=
  {"rack.version"=>[1, 3],
   "rack.errors"=>#<IO:<STDERR>>,
   "rack.multithread"=>true,
   "rack.multiprocess"=>false,
   "rack.run_once"=>false,
   "SCRIPT_NAME"=>"",
   "QUERY_STRING"=>"",
   "SERVER_PROTOCOL"=>"HTTP/1.1",
   "SERVER_SOFTWARE"=>"puma 3.6.0 Sleepy Sunday Serenity",
   "GATEWAY_INTERFACE"=>"CGI/1.2",
   "REQUEST_METHOD"=>"POST",
   "REQUEST_PATH"=>{REDACTED},
   "REQUEST_URI"=>{REDACTED},
   "HTTP_VERSION"=>"HTTP/1.0",
   "HTTP_HOST"=>{REDACTED},
   "CONTENT_LENGTH"=>"1069",
   "HTTP_X_PUSHER_KEY"=>{REDACTED},
   "HTTP_X_PUSHER_SIGNATURE"=>{REDACTED},
   ...

This line in the source calls request.kind_of?(Rack::Request). However this returns false in my app. Calling request.kind_of?(ActionDispatch::Request) returns true.

Temporary work around seems to be to create a rack request from the environment:

    > rack_request = Rack::Request.new(request.env)
    > webhook = Pusher::WebHook.new(rack_request)
    > webhook.valid?
    => true

Here's what the relevant method in my controller looks like with the work around:

  def webhook
    webhook = Pusher::WebHook.new(Rack::Request.new(request.env))
    if webhook.valid?
      webhook.events.each do |event|
        p event.inspect
      end
      render plain: 'ok'
    else
      render plain: 'invalid', status: 401
    end
  end

Am I doing something wrong or should the check on webhooks.rb be changed?

Thanks a lot for that lovely report @l85m ! I've just make a fix to the code so you don't have to allocate that Rack::Request object on each request and pushing it out to version 1.2.1