Skip to content

Commit 2c5b89b

Browse files
committed
🧹 ensure macOS asset name is ascii characters only
1 parent 05abf9b commit 2c5b89b

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

providers/os/provider/detector.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package provider
66
import (
77
"errors"
88
"slices"
9+
"strings"
910

1011
"github.com/rs/zerolog/log"
1112
"go.mondoo.com/cnquery/v12/providers-sdk/v1/inventory"
@@ -160,9 +161,21 @@ func (s *Service) assetName(asset *inventory.Asset, conn shared.Connection) {
160161
data, err := plist.Decode(f)
161162
if err == nil {
162163
if computerName, ok := data.GetString("System", "System", "ComputerName"); ok {
163-
asset.Name = computerName
164+
asset.Name = RemoveNonASCII(computerName)
164165
}
165166
}
166167
}
167168
}
168169
}
170+
171+
// RemoveNonASCII removes all non-ASCII characters
172+
// macOS may use non ASCII charaters for the computer name. This breaks the progress bar until v12.13.0
173+
func RemoveNonASCII(s string) string {
174+
var result strings.Builder
175+
for _, c := range s {
176+
if c <= 127 {
177+
result.WriteRune(c)
178+
}
179+
}
180+
return result.String()
181+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/muesli/reflow/ansi"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestAssetName(t *testing.T) {
11+
data := []byte{77, 97, 110, 97, 103, 101, 100, 226, 128, 153, 115, 32, 86, 105, 114, 116, 117, 97, 108, 32, 77, 97, 99, 104, 105, 110, 101}
12+
str := string(data)
13+
assert.Equal(t, "Managed’s Virtual Machine", str)
14+
assert.True(t, len(RemoveNonASCII(str)) <= ansi.PrintableRuneWidth(str))
15+
}

providers/os/provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
306306
}
307307

308308
// for some assets we need to do additional asset name detection, eg. macOS
309-
// s.assetName(req.Asset, conn)
309+
s.assetName(req.Asset, conn)
310310

311311
log.Debug().Str("asset", req.Asset.Name).Msg("detected asset")
312312

0 commit comments

Comments
 (0)