From a752d7de5a0a1aa1679790f76f2f8dc35e322c41 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Wed, 9 May 2018 12:25:03 -0400 Subject: MM-10495 Updated user agent detection to match output of previous library (#8748) * MM-10495 Updated user agent detection to match output of previous library * Fixed missing license header --- app/login.go | 25 ++------ app/user_agent.go | 132 ++++++++++++++++++++++++++++++++++++++++++ app/user_agent_test.go | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 287 insertions(+), 21 deletions(-) create mode 100644 app/user_agent.go create mode 100644 app/user_agent_test.go diff --git a/app/login.go b/app/login.go index 43b022749..a2f06dbc3 100644 --- a/app/login.go +++ b/app/login.go @@ -6,7 +6,6 @@ package app import ( "fmt" "net/http" - "strings" "time" "github.com/avct/uasurfer" @@ -73,26 +72,10 @@ func (a *App) DoLogin(w http.ResponseWriter, r *http.Request, user *model.User, ua := uasurfer.Parse(r.UserAgent()) - plat := ua.OS.Platform.String() - if plat == "" { - plat = "unknown" - } - - os := ua.OS.Name.String() - if os == "" { - os = "unknown" - } - - bname := ua.Browser.Name.String() - if bname == "" { - bname = "unknown" - } - - if strings.Contains(r.UserAgent(), "Mattermost") { - bname = "Desktop App" - } - - bversion := ua.Browser.Version + plat := getPlatformName(ua) + os := getOSName(ua) + bname := getBrowserName(ua, r.UserAgent()) + bversion := getBrowserVersion(ua, r.UserAgent()) session.AddProp(model.SESSION_PROP_PLATFORM, plat) session.AddProp(model.SESSION_PROP_OS, os) diff --git a/app/user_agent.go b/app/user_agent.go new file mode 100644 index 000000000..d731fd2a8 --- /dev/null +++ b/app/user_agent.go @@ -0,0 +1,132 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package app + +import ( + "fmt" + "strings" + + "github.com/avct/uasurfer" +) + +var platformNames = map[uasurfer.Platform]string{ + uasurfer.PlatformUnknown: "Windows", + uasurfer.PlatformWindows: "Windows", + uasurfer.PlatformMac: "Macintosh", + uasurfer.PlatformLinux: "Linux", + uasurfer.PlatformiPad: "iPad", + uasurfer.PlatformiPhone: "iPhone", + uasurfer.PlatformiPod: "iPod", + uasurfer.PlatformBlackberry: "BlackBerry", + uasurfer.PlatformWindowsPhone: "Windows Phone", +} + +func getPlatformName(ua *uasurfer.UserAgent) string { + platform := ua.OS.Platform + + if name, ok := platformNames[platform]; !ok { + return platformNames[uasurfer.PlatformUnknown] + } else { + return name + } +} + +var osNames = map[uasurfer.OSName]string{ + uasurfer.OSUnknown: "", + uasurfer.OSWindowsPhone: "Windows Phone", + uasurfer.OSWindows: "Windows", + uasurfer.OSMacOSX: "Mac OS", + uasurfer.OSiOS: "iOS", + uasurfer.OSAndroid: "Android", + uasurfer.OSBlackberry: "BlackBerry", + uasurfer.OSChromeOS: "Chrome OS", + uasurfer.OSKindle: "Kindle", + uasurfer.OSWebOS: "webOS", + uasurfer.OSLinux: "Linux", +} + +func getOSName(ua *uasurfer.UserAgent) string { + os := ua.OS + + if os.Name == uasurfer.OSWindows { + major := os.Version.Major + minor := os.Version.Minor + + name := "Windows" + + // Adapted from https://github.com/mssola/user_agent/blob/master/operating_systems.go#L26 + if major == 5 { + if minor == 0 { + name = "Windows 2000" + } else if minor == 1 { + name = "Windows XP" + } else if minor == 2 { + name = "Windows XP x64 Edition" + } + } else if major == 6 { + if minor == 0 { + name = "Windows Vista" + } else if minor == 1 { + name = "Windows 7" + } else if minor == 2 { + name = "Windows 8" + } else if minor == 3 { + name = "Windows 8.1" + } + } else if major == 10 { + name = "Windows 10" + } + + return name + } else if name, ok := osNames[os.Name]; ok { + return name + } else { + return osNames[uasurfer.OSUnknown] + } +} + +func getBrowserVersion(ua *uasurfer.UserAgent, userAgentString string) string { + if index := strings.Index(userAgentString, "Mattermost/"); index != -1 { + afterVersion := userAgentString[index+len("Mattermost/"):] + return strings.Fields(afterVersion)[0] + } else if index := strings.Index(userAgentString, "Franz/"); index != -1 { + afterVersion := userAgentString[index+len("Franz/"):] + return strings.Fields(afterVersion)[0] + } else { + return getUAVersion(ua.Browser.Version) + } +} + +func getUAVersion(version uasurfer.Version) string { + if version.Patch == 0 { + return fmt.Sprintf("%v.%v", version.Major, version.Minor) + } else { + return fmt.Sprintf("%v.%v.%v", version.Major, version.Minor, version.Patch) + } +} + +var browserNames = map[uasurfer.BrowserName]string{ + uasurfer.BrowserUnknown: "Unknown", + uasurfer.BrowserChrome: "Chrome", + uasurfer.BrowserIE: "Internet Explorer", + uasurfer.BrowserSafari: "Safari", + uasurfer.BrowserFirefox: "Firefox", + uasurfer.BrowserAndroid: "Android", + uasurfer.BrowserOpera: "Opera", + uasurfer.BrowserBlackberry: "BlackBerry", +} + +func getBrowserName(ua *uasurfer.UserAgent, userAgentString string) string { + browser := ua.Browser.Name + + if strings.Contains(userAgentString, "Mattermost") { + return "Desktop App" + } else if browser == uasurfer.BrowserIE && ua.Browser.Version.Major > 11 { + return "Edge" + } else if name, ok := browserNames[browser]; ok { + return name + } else { + return browserNames[uasurfer.BrowserUnknown] + } +} diff --git a/app/user_agent_test.go b/app/user_agent_test.go new file mode 100644 index 000000000..e4680bfc9 --- /dev/null +++ b/app/user_agent_test.go @@ -0,0 +1,151 @@ +package app + +import ( + "fmt" + "testing" + + "github.com/avct/uasurfer" +) + +type testUserAgent struct { + Name string + UserAgent string +} + +var testUserAgents = []testUserAgent{ + {"Mozilla 40.1", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"}, + {"Chrome 60", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"}, + {"Chrome Mobile", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36"}, + {"MM Classic App", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR6.170623.013; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/61.0.3163.81 Mobile Safari/537.36 Web-Atoms-Mobile-WebView"}, + {"MM App 3.7.1", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Mattermost/3.7.1 Chrome/56.0.2924.87 Electron/1.6.11 Safari/537.36"}, + {"Franz 4.0.4", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Franz/4.0.4 Chrome/52.0.2743.82 Electron/1.3.1 Safari/537.36"}, + {"Edge 14", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"}, + {"Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0"}, + {"Internet Explorer 11", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}, + {"Internet Explorer 11 2", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0; rv:11.0) like Gecko"}, + {"Internet Explorer 11 (Compatibility Mode) 1", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.3; Zoom 3.6.0)"}, + {"Internet Explorer 11 (Compatibility Mode) 2", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Zoom 3.6.0)"}, + {"Safari 9", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38"}, + {"Safari 8", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"}, + {"Safari Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1"}, +} + +func TestGetPlatformName(t *testing.T) { + expected := []string{ + "Windows", + "Macintosh", + "Linux", + "Linux", + "Macintosh", + "Macintosh", + "Windows", + "Windows", + "Windows", + "Windows", + "Windows", + "Windows", + "Macintosh", + "Macintosh", + "iPhone", + } + + for i, userAgent := range testUserAgents { + t.Run(fmt.Sprintf("GetPlatformName_%v", i), func(t *testing.T) { + ua := uasurfer.Parse(userAgent.UserAgent) + + if actual := getPlatformName(ua); actual != expected[i] { + t.Fatalf("%v Got %v, expected %v", userAgent.Name, actual, expected[i]) + } + }) + } +} + +func TestGetOSName(t *testing.T) { + expected := []string{ + "Windows 7", + "Mac OS", + "Android", + "Android", + "Mac OS", + "Mac OS", + "Windows 10", + "Windows", + "Windows 10", + "Windows 10", + "Windows 10", + "Windows 10", + "Mac OS", + "Mac OS", + "iOS", + } + + for i, userAgent := range testUserAgents { + t.Run(fmt.Sprintf("GetOSName_%v", i), func(t *testing.T) { + ua := uasurfer.Parse(userAgent.UserAgent) + + if actual := getOSName(ua); actual != expected[i] { + t.Fatalf("Got %v, expected %v", actual, expected[i]) + } + }) + } +} + +func TestGetBrowserName(t *testing.T) { + expected := []string{ + "Firefox", + "Chrome", + "Chrome", + "Chrome", + "Desktop App", + "Chrome", + "Edge", + "Internet Explorer", + "Internet Explorer", + "Internet Explorer", + "Internet Explorer", + "Internet Explorer", + "Safari", + "Safari", + "Safari", + } + + for i, userAgent := range testUserAgents { + t.Run(fmt.Sprintf("GetBrowserName_%v", i), func(t *testing.T) { + ua := uasurfer.Parse(userAgent.UserAgent) + + if actual := getBrowserName(ua, userAgent.UserAgent); actual != expected[i] { + t.Fatalf("Got %v, expected %v", actual, expected[i]) + } + }) + } +} + +func TestGetBrowserVersion(t *testing.T) { + expected := []string{ + "40.1", + "60.0.3112", // Doesn't report the fourth part of the version + "60.0.3112", // Doesn't report the fourth part of the version + "61.0.3163", + "3.7.1", + "4.0.4", + "14.14393", + "9.0", + "11.0", + "11.0", + "7.0", + "7.0", + "11.0", + "8.0.7", + "9.0", + } + + for i, userAgent := range testUserAgents { + t.Run(fmt.Sprintf("GetBrowserVersion_%v", i), func(t *testing.T) { + ua := uasurfer.Parse(userAgent.UserAgent) + + if actual := getBrowserVersion(ua, userAgent.UserAgent); actual != expected[i] { + t.Fatalf("Got %v, expected %v", actual, expected[i]) + } + }) + } +} -- cgit v1.2.3-1-g7c22 From 823b22c403510a52d56bc96428cf9977b80a9dfc Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Thu, 10 May 2018 14:43:49 +0200 Subject: fix test and add new (#8758) --- api4/system_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/api4/system_test.go b/api4/system_test.go index d4134f8e2..f46ae7436 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -618,9 +618,14 @@ func TestS3TestConnection(t *testing.T) { config.FileSettings.AmazonS3Bucket = "Wrong_bucket" _, resp = th.SystemAdminClient.TestS3Connection(&config) CheckInternalErrorStatus(t, resp) - if resp.Error.Message != "Error checking if bucket exists." { + if resp.Error.Message != "Unable to create bucket" { t.Fatal("should return error ") } + + config.FileSettings.AmazonS3Bucket = "shouldcreatenewbucket" + _, resp = th.SystemAdminClient.TestS3Connection(&config) + CheckOKStatus(t, resp) + } func TestSupportedTimezones(t *testing.T) { -- cgit v1.2.3-1-g7c22 From 68340d4715ba470462c4e870f824533f6559c6f1 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 10 May 2018 11:22:10 -0400 Subject: Prevent divide by zero if there are no hubs (#8763) --- app/web_hub.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/web_hub.go b/app/web_hub.go index 18eb97c8e..c9ca2f4f5 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -125,6 +125,10 @@ func (a *App) HubStop() { } func (a *App) GetHubForUserId(userId string) *Hub { + if len(a.Hubs) == 0 { + return nil + } + hash := fnv.New32a() hash.Write([]byte(userId)) index := hash.Sum32() % uint32(len(a.Hubs)) @@ -132,11 +136,17 @@ func (a *App) GetHubForUserId(userId string) *Hub { } func (a *App) HubRegister(webConn *WebConn) { - a.GetHubForUserId(webConn.UserId).Register(webConn) + hub := a.GetHubForUserId(webConn.UserId) + if hub != nil { + hub.Register(webConn) + } } func (a *App) HubUnregister(webConn *WebConn) { - a.GetHubForUserId(webConn.UserId).Unregister(webConn) + hub := a.GetHubForUserId(webConn.UserId) + if hub != nil { + hub.Unregister(webConn) + } } func (a *App) Publish(message *model.WebSocketEvent) { -- cgit v1.2.3-1-g7c22 From 84499825763b32dad3b6c8a3a50290246c895a71 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 10 May 2018 14:17:01 -0400 Subject: Attempt to fix hub nil panic (#8766) --- app/web_hub.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/web_hub.go b/app/web_hub.go index c9ca2f4f5..f69645f50 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -343,7 +343,7 @@ func (h *Hub) Unregister(webConn *WebConn) { } func (h *Hub) Broadcast(message *model.WebSocketEvent) { - if message != nil { + if h != nil && h.broadcast != nil && message != nil { h.broadcast <- message } } -- cgit v1.2.3-1-g7c22 From 21d3b247d9645efa9471877cc36b46de92dc1d09 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 10 May 2018 15:31:23 -0400 Subject: Comment out problematic TestS3TestConnection test (#8770) --- api4/system_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api4/system_test.go b/api4/system_test.go index f46ae7436..8e15f6ed2 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -570,7 +570,7 @@ func TestGetAnalyticsOld(t *testing.T) { CheckUnauthorizedStatus(t, resp) } -func TestS3TestConnection(t *testing.T) { +/*func TestS3TestConnection(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer th.TearDown() Client := th.Client @@ -626,7 +626,7 @@ func TestS3TestConnection(t *testing.T) { _, resp = th.SystemAdminClient.TestS3Connection(&config) CheckOKStatus(t, resp) -} +}*/ func TestSupportedTimezones(t *testing.T) { th := Setup().InitBasic() -- cgit v1.2.3-1-g7c22 From 52674eb4c70865fadcde6dbd3d294ffbbbcb844c Mon Sep 17 00:00:00 2001 From: Derrick Anderson Date: Fri, 11 May 2018 10:44:42 -0400 Subject: add s3 test back in for master --- api4/system_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api4/system_test.go b/api4/system_test.go index 8e15f6ed2..f46ae7436 100644 --- a/api4/system_test.go +++ b/api4/system_test.go @@ -570,7 +570,7 @@ func TestGetAnalyticsOld(t *testing.T) { CheckUnauthorizedStatus(t, resp) } -/*func TestS3TestConnection(t *testing.T) { +func TestS3TestConnection(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer th.TearDown() Client := th.Client @@ -626,7 +626,7 @@ func TestGetAnalyticsOld(t *testing.T) { _, resp = th.SystemAdminClient.TestS3Connection(&config) CheckOKStatus(t, resp) -}*/ +} func TestSupportedTimezones(t *testing.T) { th := Setup().InitBasic() -- cgit v1.2.3-1-g7c22