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

Exception with invalid value for Float(): "0..5" #315

Closed
pts-owentran opened this issue Sep 18, 2017 · 4 comments
Closed

Exception with invalid value for Float(): "0..5" #315

pts-owentran opened this issue Sep 18, 2017 · 4 comments

Comments

@pts-owentran
Copy link

Description

browser passes invalid number raises exception when calling is_mobile?

How to reproduce

Not sure, did not have the full browser info, agent was Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

What do you expect

Expect to be false instead of exception

What happened instead

Received exception

Software:

  • Rails version: 5.1.3
  • Browser gem version: 2.4.0

Full backtrace

An ArgumentError occurred in background at 2017-09-17 05:51:18 UTC :

  invalid value for Float(): "0..5"
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:50:in `Float'

  -------------------------------
Backtrace:
-------------------------------

  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:50:in `Float'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:50:in `quality'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:16:in `block in parse'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:16:in `reject'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/accept_language.rb:16:in `parse'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/base.rb:14:in `initialize'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/browser.rb:88:in `new'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/browser.rb:88:in `block in new'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/browser.rb:88:in `map'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/browser.rb:88:in `new'
  /var/www/pointshound/shared/bundle/ruby/2.3.0/gems/browser-2.4.0/lib/browser/action_controller.rb:16:in `browser'

Report

Not applicable, was a customer's browser

@fnando
Copy link
Owner

fnando commented Sep 26, 2017

As per your backtrace, the error is not related to browser.device.mobile? at all; it's related to browser.accept_language instead.

In any case, both methods work just fine for me on v2.5.1. Can you please upgrade and see what happens?

2.4.2 (main)> browser = Browser.new 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
=> #<Browser::InternetExplorer:0x00007fe819925960 @accept_language=[], @ua="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko">
2.4.2 (main)> browser.device.mobile?
=> nil
2.4.2 (main)> browser.accept_language
=> []

@pts-owentran
Copy link
Author

Hi. Sorry, yeah, it's with accept_language. Here's a simple pry with the latest version (2.5.1 also happens with my version 2.4.0)...

[1] pry(main)> browser = Browser.new 'Some User Agent', accept_language: 'fr-CH, fr;q=0.9, en;q=0.8, de;q=0..7, *;q=0.5'
ArgumentError: invalid value for Float(): "0..7"
from /usr/local/lib/ruby/gems/2.3.0/gems/browser-2.5.1/lib/browser/accept_language.rb:50:in `Float'

@fnando
Copy link
Owner

fnando commented Sep 26, 2017

I see… where does that Accept-Language header come from? That 0..7 isn't a valid value for qvalues (which should be a number >= 0 and <= 1.

In case you're feeling adventurous, here's the RFC, from which I quote:

Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range, using the qvalue scale from 0 to 1. The default value is q=1.

@pts-owentran
Copy link
Author

Oh, it was probably some bad browser agent out there. How about rescue with 0 if it's not a number? We get lots of security scans and they like to send bogus browser agents.

@fnando fnando closed this as completed in 4b7d9ca Oct 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants