omniauth icon indicating copy to clipboard operation
omniauth copied to clipboard

nil error in `callback_path` after omniauth 2 update

Open thisismydesign opened this issue 3 years ago • 4 comments

Take a very simple strategy, such as the example from the readme. Then the following spec fails for me

  # https://github.com/omniauth/omniauth-oauth2/blob/3a43234ab5dd36a75f9c125c58fcfe1a37b26805/spec/omniauth/strategies/oauth2_spec.rb#L4-L8
  def app
    lambda do |_env|
      [200, {}, ["Hello."]]
    end
  end

  subject { described_class.new(app) }

  describe '#callback_path' do
    it 'has the correct callback path' do
      expect(subject.callback_path).to eq('/users/auth/custom/callback')
    end
  end
  1) OmniAuth::Strategies::Custom#callback_path has the correct callback path
     Failure/Error: expect(subject.callback_path).to eq('/users/auth/custom/callback')
     
     NoMethodError:
       undefined method `[]' for nil:NilClass
     
             @env['SCRIPT_NAME'] || ''
                 ^^^^^^^^^^^^^^^
     # /usr/local/bundle/gems/omniauth-2.1.0/lib/omniauth/strategy.rb:501:in `script_name'
     # /usr/local/bundle/gems/omniauth-2.1.0/lib/omniauth/strategy.rb:450:in `callback_path'

thisismydesign avatar Jul 13 '22 09:07 thisismydesign

Do you have Omniauth.config.test_mode set?

BobbyMcWho avatar Jul 13 '22 12:07 BobbyMcWho

@BobbyMcWho yes, I got this error with both true and false

thisismydesign avatar Jul 13 '22 13:07 thisismydesign

The issue is that @env is nil, and actually lives in the Omniauth gem and not this one: https://github.com/omniauth/omniauth/blob/master/lib/omniauth/strategy.rb#L500-L502

We could return '' if @env is nil there, if you want to spin up a PR, I can take a look, else I'll get to it when I can find some time.

BobbyMcWho avatar Jul 13 '22 16:07 BobbyMcWho

@BobbyMcWho Thanks. This works in production though, so I'm wondering if there's a way to set env in the test as a workaround. But yeah env can be nil so it should be treated accordingly. I can provide a PR tomorrow

thisismydesign avatar Jul 13 '22 20:07 thisismydesign

@BobbyMcWho I have opened a PR for the same. Please do check.

shreyakurian02 avatar Oct 04 '22 07:10 shreyakurian02

Is there any workaround while we wait for the PR to be merged? I have the same issue with updating to 2.1.

jeremygottfried avatar Jan 20 '23 17:01 jeremygottfried

Hmm, actually I don't believe callback path is supposed to be called without an @env having been created

BobbyMcWho avatar Jan 20 '23 17:01 BobbyMcWho

I've released a fix for this in 2.1.1 which has been pushed to rubygems

BobbyMcWho avatar Jan 20 '23 18:01 BobbyMcWho