From 309594cedf9f39be5f557de34cc741e218cd3668 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Tue, 6 Mar 2018 13:22:07 -0800 Subject: [PLT-7574] Update and Simplify Compatibility Check and Replace UA Dependency (#7427) * Add fix to not run browser check on Mobile Browsers or the App * remove non safari and IE checks * Replace useragent checking dependency and update UA tests * change some wording * change dependancy again to one with compatible licence * Fix typos and clarify wording * fix typo and comvert tests to use array --- web/web.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'web/web.go') diff --git a/web/web.go b/web/web.go index e0edd1b7a..22fe43923 100644 --- a/web/web.go +++ b/web/web.go @@ -8,12 +8,12 @@ import ( "strings" "github.com/NYTimes/gziphandler" + "github.com/avct/uasurfer" l4g "github.com/alecthomas/log4go" "github.com/mattermost/mattermost-server/api" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" - "github.com/mssola/user_agent" ) func Init(api3 *api.API) { @@ -65,26 +65,27 @@ func pluginHandler(config model.ConfigFunc, handler http.Handler) http.Handler { }) } -var browsersNotSupported string = "MSIE/8;MSIE/9;MSIE/10;Internet Explorer/8;Internet Explorer/9;Internet Explorer/10;Safari/7;Safari/8" +// Due to the complexities of UA detection and the ramifications of a misdetection only older Safari and IE browsers throw incompatibility errors. -func CheckBrowserCompatability(c *api.Context, r *http.Request) bool { - ua := user_agent.New(r.UserAgent()) - bname, bversion := ua.Browser() +// Map should be of minimum required browser version. +var browserMinimumSupported = map[string]int{ + "BrowserIE": 11, + "BrowserSafari": 9, +} - browsers := strings.Split(browsersNotSupported, ";") - for _, browser := range browsers { - version := strings.Split(browser, "/") +func CheckClientCompatability(agentString string) bool { + ua := uasurfer.Parse(agentString) - if strings.HasPrefix(bname, version[0]) && strings.HasPrefix(bversion, version[1]) { - return false - } + if version, exist := browserMinimumSupported[ua.Browser.Name.String()]; exist && ua.Browser.Version.Major < version { + return false } return true } func root(c *api.Context, w http.ResponseWriter, r *http.Request) { - if !CheckBrowserCompatability(c, r) { + + if !CheckClientCompatability(r.UserAgent()) { w.Header().Set("Cache-Control", "no-store") page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser") page.Props["Title"] = c.T("web.error.unsupported_browser.title") -- cgit v1.2.3-1-g7c22