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

Not adding the leading slash leads to an anonymous NoMethodError: undefined method `downcase' for nil:NilClass #194

Closed
ferdi2005 opened this issue Jul 4, 2021 · 2 comments · Fixed by #211
Assignees

Comments

@ferdi2005
Copy link

ferdi2005 commented Jul 4, 2021

Not adding the traling slash in the OAuth consumer leads to an uneasily debuggable NoMethodError: undefined method 'downcase' for nil:NilClass

Example, using:
oauth_consumer = OAuth::Consumer.new(ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"], :site => "https://commons.wikimedia.org", :request_token_path => "w/index.php?title=Special:OAuth/initiate", :authorize_path => "wiki/Special:OAuth/authorize", :access_token_path => "wiki/Special:OAuth/token",)

instead of:

oauth_consumer = OAuth::Consumer.new(ENV["CONSUMER_KEY"], ENV["CONSUMER_SECRET"], :site => "https://commons.wikimedia.org", :request_token_path => "/w/index.php?title=Special:OAuth/initiate", :authorize_path => "/wiki/Special:OAuth/authorize", :access_token_path => "/wiki/Special:OAuth/token",)

leads to a NoMethodError: undefined method 'downcase' for nil:NilClass and not to a clearer error.

@cyrillefr
Copy link

Hello, been through this problem too.

When playing with auth with the Twitter API:
access_token.get('/2/users/117345550/tweets') -> OK
access_token.get('2/users/117345550/tweets') -> KO (note there is no / at pos 0).

The problem lies in the lib/oauth/consumer.rb file:

def request(http_method, path, token = nil, request_options = {}, *arguments)
   if path !~ /^\//
     @http = create_http(path) # we go here if no leading slash

Which leads us to (same file):

def create_http(_url = nil) 
  . . .
  if _url.nil? || _url[0] =~ /^\//
    our_uri = URI.parse(site)
  else
    our_uri = URI.parse(_url) # we are here since url[0] is not a "/"
  end

So, instead of parsing something like api.twitter.com (the host), we parse a path (2/users/etc)
The resulting http object has a host attribute of nil

All the way down, we hit (file request_proxy/base.rb)

def normalized_uri
  "#{u.scheme.downcase}://#{u.host.downcase} # since u.host is nil, raise Error

I am not sure why, we would parse a path if it does not begin with a slash.
But the fix would be within these methods.

@pboling
Copy link
Member

pboling commented Oct 31, 2021

Thanks for the report @ferdi2005 , and the analysis @cyrillefr . I'll work on fixing this issue for the next release, which will be at least a minor bump.

@pboling pboling self-assigned this Oct 31, 2021
@pboling pboling changed the title Not adding the trailing slash leads to an anonymous NoMethodError: undefined method `downcase' for nil:NilClass Not adding the leading slash leads to an anonymous NoMethodError: undefined method `downcase' for nil:NilClass Oct 31, 2021
pboling added a commit that referenced this issue Oct 31, 2021
Signed-off-by: Peter Boling <peter.boling@gmail.com>
pboling added a commit that referenced this issue Oct 31, 2021
…sh-error

🐛 Fix #194: NoMethodError on missing leading slash
pboling added a commit that referenced this issue Nov 1, 2021
Signed-off-by: Peter Boling <peter.boling@gmail.com>
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

Successfully merging a pull request may close this issue.

3 participants