From 7c222023300a55bb31dc601f0c67d1a47cebfff7 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 5 Aug 2021 14:25:07 +0530 Subject: [PATCH] MM-37575: Handle nil value in override_username (#18057) * MM-37575: Handle nil value in override_username Since props is a map[string]interface{}, an ", ok" check doesn't really help because even if a key is not present, by default, a map will always return the default value of the value, which would be a nil interface. So we can just check for the value of the map instead. https://mattermost.atlassian.net/browse/MM-37575 ```release-note NONE ``` * Fix for integer case as well ```release-note NONE ``` * Update app/notification.go Co-authored-by: Ibrahim Serdar Acikgoz Co-authored-by: Ibrahim Serdar Acikgoz --- app/notification.go | 6 ++++-- app/notification_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/notification.go b/app/notification.go index 5f9d5ec844..46dcb4e5c3 100644 --- a/app/notification.go +++ b/app/notification.go @@ -1068,8 +1068,10 @@ func (n *PostNotification) GetSenderName(userNameFormat string, overridesAllowed } if overridesAllowed && n.Channel.Type != model.ChannelTypeDirect { - if value, ok := n.Post.GetProps()["override_username"]; ok && n.Post.GetProp("from_webhook") == "true" { - return value.(string) + if value := n.Post.GetProps()["override_username"]; value != nil && n.Post.GetProp("from_webhook") == "true" { + if s, ok := value.(string); ok { + return s + } } } diff --git a/app/notification_test.go b/app/notification_test.go index 20231bf3ab..f82027849e 100644 --- a/app/notification_test.go +++ b/app/notification_test.go @@ -1799,6 +1799,20 @@ func TestPostNotificationGetSenderName(t *testing.T) { }, } + overriddenPost2 := &model.Post{ + Props: model.StringInterface{ + "override_username": nil, + "from_webhook": "true", + }, + } + + overriddenPost3 := &model.Post{ + Props: model.StringInterface{ + "override_username": 10, + "from_webhook": "true", + }, + } + for name, testCase := range map[string]struct { channel *model.Channel post *model.Post @@ -1841,6 +1855,16 @@ func TestPostNotificationGetSenderName(t *testing.T) { allowOverrides: false, expected: "@" + sender.Username, }, + "nil override_username": { + post: overriddenPost2, + allowOverrides: true, + expected: "@" + sender.Username, + }, + "integer override_username": { + post: overriddenPost3, + allowOverrides: true, + expected: "@" + sender.Username, + }, } { t.Run(name, func(t *testing.T) { channel := defaultChannel