From 13c0ba6e8a271c43892f1a2bc733e2d4a20fbe10 Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Wed, 3 Nov 2021 08:30:33 +0100 Subject: [PATCH] Strip arguments from logged command (#18862) --- model/auditconv.go | 7 ++++++- model/auditconv_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/model/auditconv.go b/model/auditconv.go index 8de21124b6..1bcb2363bc 100644 --- a/model/auditconv.go +++ b/model/auditconv.go @@ -4,6 +4,8 @@ package model import ( + "strings" + "github.com/francoispqt/gojay" ) @@ -268,7 +270,10 @@ func newAuditCommandArgs(ca *CommandArgs) auditCommandArgs { cmdargs.ChannelID = ca.ChannelId cmdargs.TeamID = ca.TeamId cmdargs.TriggerID = ca.TriggerId - cmdargs.Command = ca.Command + cmdFields := strings.Fields(ca.Command) + if len(cmdFields) > 0 { + cmdargs.Command = cmdFields[0] + } } return cmdargs } diff --git a/model/auditconv_test.go b/model/auditconv_test.go index 49c1b08a13..27511fdddc 100644 --- a/model/auditconv_test.go +++ b/model/auditconv_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type Sample struct { @@ -53,3 +54,45 @@ func TestAuditModelTypeConv(t *testing.T) { }) } } + +func TestAuditModelTypeConvCommandArgs(t *testing.T) { + tcs := []struct { + name string + input CommandArgs + expectedCommand string + }{ + { + name: "empty input", + input: CommandArgs{}, + expectedCommand: "", + }, + { + name: "no arguments", + input: CommandArgs{ + Command: "/command", + }, + expectedCommand: "/command", + }, + { + name: "some arguments", + input: CommandArgs{ + Command: "/command --test test --test2 test", + }, + expectedCommand: "/command", + }, + { + name: "with multiple spaces and tabs", + input: CommandArgs{ + Command: "/command --test test --test2 test", + }, + expectedCommand: "/command", + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + args := newAuditCommandArgs(&tc.input) + require.Equal(t, tc.expectedCommand, args.Command) + }) + } +}