From: Simon Ruderich Date: Sun, 9 May 2021 09:36:44 +0000 (+0200) Subject: safcm: strip invalid characters from detected os/arch groups X-Git-Url: https://ruderich.org/simon/gitweb/?p=safcm%2Fsafcm.git;a=commitdiff_plain;h=da47b542155706887750f8a0338f8cd2ced654a6;hp=5d6cc7f14a4bacc36bf3a23cd735a75ad4a90f1d safcm: strip invalid characters from detected os/arch groups Handle them like any other detected group because the remote can send invalid values. The current code can handle arbitrary group names just fine but it's better to treat all untrusted input the same. --- diff --git a/cmd/safcm/sync_info.go b/cmd/safcm/sync_info.go index 0df33e3..5ba6ac8 100644 --- a/cmd/safcm/sync_info.go +++ b/cmd/safcm/sync_info.go @@ -50,14 +50,19 @@ var infoGroupDetectedRegexp = regexp.MustCompile(`[^a-z0-9_-]+`) func hostInfoRespToGroups(resp safcm.MsgInfoResp) []string { groups := []string{ - config.GroupDetectedPrefix + "_" + resp.Goos, - config.GroupDetectedPrefix + "_" + resp.Goarch, + hostInfoDetectedGroupName(resp.Goos), + hostInfoDetectedGroupName(resp.Goarch), } for _, x := range resp.Output { + groups = append(groups, hostInfoDetectedGroupName(x)) + } + return groups +} + +func hostInfoDetectedGroupName(x string) string { x = strings.TrimSpace(x) x = strings.ToLower(x) x = infoGroupDetectedRegexp.ReplaceAllString(x, "_") - groups = append(groups, config.GroupDetectedPrefix+"_"+x) - } - return groups + x = config.GroupDetectedPrefix + "_" + x + return x } diff --git a/cmd/safcm/sync_info_test.go b/cmd/safcm/sync_info_test.go index 7ad13c9..b1da4c1 100644 --- a/cmd/safcm/sync_info_test.go +++ b/cmd/safcm/sync_info_test.go @@ -63,6 +63,19 @@ func TestHostInfoRespToGroups(t *testing.T) { "detected_with_utf-8_hello_", }, }, + + { + "invalid goos/goarch", + safcm.MsgInfoResp{ + Goos: " INVALID goos! ", + Goarch: "Hello, 世界", + Output: nil, + }, + []string{ + "detected_invalid_goos_", + "detected_hello_", + }, + }, } for _, tc := range tests {