Skip to content

Commit

Permalink
🐛 Fix Windows PURLs for locally collected packages (#5220)
Browse files Browse the repository at this point in the history
Fixes #5219

Signed-off-by: Christian Zunker <christian@mondoo.com>
  • Loading branch information
czunker authored Feb 14, 2025
1 parent 727efc4 commit 350117b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 8 additions & 5 deletions providers/os/resources/packages/windows_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (w *WinPkgManager) getLocalInstalledApps() ([]Package, error) {
continue
}
for _, c := range children {
p, err := getPackageFromRegistryKey(c)
p, err := getPackageFromRegistryKey(c, w.platform)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (w *WinPkgManager) getFsInstalledApps() ([]Package, error) {
continue
}
for _, c := range children {
p, err := getPackageFromRegistryKey(c)
p, err := getPackageFromRegistryKey(c, w.platform)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -420,16 +420,16 @@ func parseAppxManifest(input []byte) (winAppxPackages, error) {
return pkg, nil
}

func getPackageFromRegistryKey(key registry.RegistryKeyChild) (*Package, error) {
func getPackageFromRegistryKey(key registry.RegistryKeyChild, platform *inventory.Platform) (*Package, error) {
items, err := registry.GetNativeRegistryKeyItems(key.Path + "\\" + key.Name)
if err != nil {
log.Debug().Err(err).Str("path", key.Path).Msg("could not read registry key children")
return nil, err
}
return getPackageFromRegistryKeyItems(items), nil
return getPackageFromRegistryKeyItems(items, platform), nil
}

func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem) *Package {
func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem, platform *inventory.Platform) *Package {
var uninstallString string
var displayName string
var displayVersion string
Expand Down Expand Up @@ -457,6 +457,9 @@ func getPackageFromRegistryKeyItems(children []registry.RegistryKeyItem) *Packag
Version: displayVersion,
Format: "windows/app",
Vendor: publisher,
PUrl: purl.NewPackageURL(
platform, purl.TypeWindows, displayName, displayVersion,
).String(),
}

if displayName != "" && displayVersion != "" {
Expand Down
15 changes: 12 additions & 3 deletions providers/os/resources/packages/windows_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func TestWindowsHotFixParser(t *testing.T) {
func TestGetPackageFromRegistryKeyItems(t *testing.T) {
t.Run("get package from registry key items that are empty", func(t *testing.T) {
items := []registry.RegistryKeyItem{}
p := getPackageFromRegistryKeyItems(items)
p := getPackageFromRegistryKeyItems(items, &inventory.Platform{
Family: []string{"windows"},
})
assert.Nil(t, p)
})
t.Run("get package from registry key items with missing required values", func(t *testing.T) {
Expand All @@ -152,7 +154,9 @@ func TestGetPackageFromRegistryKeyItems(t *testing.T) {
},
},
}
p := getPackageFromRegistryKeyItems(items)
p := getPackageFromRegistryKeyItems(items, &inventory.Platform{
Family: []string{"windows"},
})
assert.Nil(t, p)
})

Expand Down Expand Up @@ -187,7 +191,11 @@ func TestGetPackageFromRegistryKeyItems(t *testing.T) {
},
},
}
p := getPackageFromRegistryKeyItems(items)
p := getPackageFromRegistryKeyItems(items, &inventory.Platform{
Name: "windows",
Arch: "x86",
Family: []string{"windows"},
})
CPEs, err := cpe.NewPackage2Cpe(
"Microsoft Corporation",
"Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.28.29913",
Expand All @@ -204,6 +212,7 @@ func TestGetPackageFromRegistryKeyItems(t *testing.T) {
Format: "windows/app",
CPEs: CPEs,
Vendor: "Microsoft Corporation",
PUrl: "pkg:windows/windows/Microsoft%20Visual%20C%2B%2B%202015-2019%20Redistributable%20%28x86%29%20-%2014.28.29913@14.28.29913.0?arch=x86",
}
assert.NotNil(t, p)
assert.Equal(t, expected, p)
Expand Down

0 comments on commit 350117b

Please sign in to comment.