From 5166bf5af1309b5ec04a6c27d5ceaf829051e879 Mon Sep 17 00:00:00 2001 From: Christopher Poile Date: Mon, 28 Aug 2023 12:28:06 -0400 Subject: [PATCH] MM-54259 - Fix: Panic in isMobilePostGA (#505) * remove isMobilePostGA code * Revert "remove isMobilePostGA code" This reverts commit 0ab4977e4472964e8c7470a8628e0fd3acef9ce9. * fix the crash --- server/utils.go | 6 +++++- server/utils_test.go | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/utils.go b/server/utils.go index f3370eda4..21abc1931 100644 --- a/server/utils.go +++ b/server/utils.go @@ -113,7 +113,11 @@ func isMobilePostGA(r *http.Request) (mobile, postGA bool) { // https://mattermost.atlassian.net/browse/MM-48929 userAgent := r.Header.Get("User-Agent") fields := strings.Fields(userAgent) - clientAgent := fields[0] // safe to assume there's at least one + if len(fields) == 0 { + return false, false + } + + clientAgent := fields[0] isMobile := strings.HasPrefix(clientAgent, "rnbeta") || strings.HasPrefix(clientAgent, "Mattermost") if !isMobile { return false, false diff --git a/server/utils_test.go b/server/utils_test.go index bd54afbe1..c2d71178d 100644 --- a/server/utils_test.go +++ b/server/utils_test.go @@ -47,6 +47,13 @@ func Test_isMobilePostGA(t *testing.T) { wantMobile: true, wantPostGA: true, }, + { + name: "no user agent", + userAgent: "", + params: "", + wantMobile: false, + wantPostGA: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {