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

Handle an odd case with the user agent #242

Merged
merged 3 commits into from
Aug 3, 2018

Conversation

aub
Copy link
Contributor

@aub aub commented Aug 2, 2018

Fixes #243

I'm getting a JS crash due to an odd useragent from the client. The agent I'm getting is:

Mozilla/5.0 (iPad; CPU OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 KurogoVersion/2.7.7 (Kurogo iOS Tablet) KurogoOSVersion/11.4.1 KurogoAppVersion/2.0.1 (com.telerik.TridentUniversity)

This Kurogo browser is used by universities, and it includes the name of the university in the useragent string. In this case, the university is called Trident. In the isMsie code in utils.js, the first line tests for the presence of msie or trident, and the positive side of the unary operator assumes this means it will be checking a standard IE useragent string to get the revision. In my case, that is not the case, so navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i) return null and trying to index into null as an array makes a crash.

What I've done is split out the unary to get the result of the match and make sure it exists before indexing into it. This fixes the crash.

This is an attempt at solving the crash described above. It fixes issue #243.

Unfortunately, I was unable to figure out how to spoof the useragent in a test after a bunch of searching. To validate this, I copied the useragent string i'm getting directly into the code, made a test that isMsie was working as expected, and then deleted that hack. It worked as expected both with my hack in place and with the user agent provided by phantomjs.

@Haroenv
Copy link
Contributor

Haroenv commented Aug 2, 2018

Thanks, this seems to be correct, I think a test could be added where navigator.userAgent is set before doing the test of isMsie, what do you think? There's currently no test of it yet, so a new file for utils can be made

@aub
Copy link
Contributor Author

aub commented Aug 2, 2018

@Haroenv I've tried that but unfortunately the useragent doesn't carry through to the test. Here's the test I wrote in utils_spec.js:

  it('should handle a user agent that is not IE but includes the string "trident"', function() {
    navigator.userAgent = 'FOO!!!!!!!!!!!!!!!!!!!!!!!!!!!';
    var actual = _.isMsie();
    expect(actual).toEqual(false);
  });

Then I added a console.log to the isMsie function to print out the useragent, but it always prints out:

Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1

@coveralls
Copy link

coveralls commented Aug 2, 2018

Coverage Status

Coverage increased (+0.05%) to 88.806% when pulling 0e6227e on aub:handle-odd-useragent into 55807b8 on algolia:master.

@Haroenv
Copy link
Contributor

Haroenv commented Aug 2, 2018

Could you try: https://stackoverflow.com/a/1307088/3185307

navigator.__defineGetter__('userAgent', function(){
    return 'foo' // customized user agent
});

navigator.userAgent; // 'foo'

preferably wrapped in a function :)

@aub
Copy link
Contributor Author

aub commented Aug 2, 2018

@Haroenv unfortunately that doesn't work either 😭 I've added a test of isMsie now so that the code coverage doesn't go down, but it is also fragile... if the test were run under actual IE then it would fail.

@Haroenv
Copy link
Contributor

Haroenv commented Aug 2, 2018

Yes, I understand, thanks for trying to find a solution!

@Haroenv
Copy link
Contributor

Haroenv commented Aug 2, 2018

I'll still leave this open so someone else can take a look at it, see if they find a way to add a proper test for that. Maybe we should change _isMsie to accept the user agent instead, that way it becomes testable

@aub
Copy link
Contributor Author

aub commented Aug 2, 2018

I'd be happy to make that change if you want... I thought it would be bad to change the function signature just for test support, but it would be super easy to add an optional argument that would default back to the navigator's useragent if not provided.

@Haroenv
Copy link
Contributor

Haroenv commented Aug 2, 2018

I think the advantage to make it testable would be worth it, feel free to do so in this PR and add some tests with various real user agents :)

@aub
Copy link
Contributor Author

aub commented Aug 2, 2018

@Haroenv I added the tests.

@@ -18,10 +18,14 @@ module.exports = {
map: null,
mixin: null,

isMsie: function() {
isMsie: function(agentString) {
if (agentString === undefined) { agentString = navigator.userAgent; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I didn't think about adding it like that, seems fine to me, I originally suggested to change the usage everywhere, but this is easier and makes sense too.

@Haroenv Haroenv requested a review from bobylito August 2, 2018 14:25
Copy link
Contributor

@bobylito bobylito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this change. Since I wasn't able to get much info on this browser, so I hope that it is indeed like MS IE.

@Haroenv Haroenv merged commit c194736 into algolia:master Aug 3, 2018
@Haroenv
Copy link
Contributor

Haroenv commented Aug 3, 2018

I will release this next Monday, together with a fix for #241

@Haroenv
Copy link
Contributor

Haroenv commented Aug 8, 2018

@aub, sorry for the delay, the other feature proved to be complicated to test, this is now released in 0.31.0

@aub aub mentioned this pull request Aug 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash resulting from an odd useragent
4 participants