diff --git a/syft/pkg/cataloger/python/cataloger_test.go b/syft/pkg/cataloger/python/cataloger_test.go index 8da2c6092..cc0682246 100644 --- a/syft/pkg/cataloger/python/cataloger_test.go +++ b/syft/pkg/cataloger/python/cataloger_test.go @@ -301,6 +301,83 @@ func Test_InstalledPackageCataloger(t *testing.T) { }, }, }, + { + name: "detect license files under dist-info/licenses (PEP 639)", + fixture: "testdata/site-packages/license-pep639", + expectedPackages: []pkg.Package{ + { + Name: "pep639-example", + Version: "1.0.0", + PURL: "pkg:pypi/pep639-example@1.0.0", + Type: pkg.PythonPkg, + Language: pkg.Python, + Locations: file.NewLocationSet( + file.NewLocation("pep639_example-1.0.0.dist-info/METADATA"), + file.NewLocation("pep639_example-1.0.0.dist-info/RECORD"), + ), + Licenses: pkg.NewLicenseSet( + pkg.License{ + Value: "BSD-3-Clause", + SPDXExpression: "BSD-3-Clause", + Type: "concluded", + Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/licenses/LICENSE", 0, 1475), + // no License-File is declared, so the license file is found through RECORD + Locations: file.NewLocationSet(file.NewLocation("pep639_example-1.0.0.dist-info/licenses/LICENSE")), + }, + ), + FoundBy: "python-installed-package-cataloger", + Metadata: pkg.PythonPackage{ + Name: "pep639-example", + Version: "1.0.0", + SitePackagesRootPath: ".", + Files: []pkg.PythonFileRecord{ + {Path: "pep639_example-1.0.0.dist-info/METADATA", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "Z_-WBDzq246VHj56DO8yfBNNtlTqMj6q9xw_OGgeEMw"}, Size: "218"}, + {Path: "pep639_example-1.0.0.dist-info/licenses/LICENSE", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o"}, Size: "1475"}, + {Path: "pep639_example-1.0.0.dist-info/RECORD"}, + }, + RequiresPython: ">=3.9", + }, + }, + { + Name: "pep639-declared", + Version: "1.0.0", + PURL: "pkg:pypi/pep639-declared@1.0.0", + Type: pkg.PythonPkg, + Language: pkg.Python, + Locations: file.NewLocationSet( + file.NewLocation("pep639_declared-1.0.0.dist-info/METADATA"), + file.NewLocation("pep639_declared-1.0.0.dist-info/RECORD"), + ), + // found only through License-File, since RECORD does not list them; AUTHORS.rst is not a license file + Licenses: pkg.NewLicenseSet( + pkg.License{ + Value: "BSD-3-Clause", + SPDXExpression: "BSD-3-Clause", + Type: "concluded", + Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD", 0, 1475), + Locations: file.NewLocationSet(file.NewLocation("pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD")), + }, + pkg.License{ + Value: "MIT", + SPDXExpression: "MIT", + Type: "concluded", + Contents: mustContentsFromLocation(t, "testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT", 13, 1072), + Locations: file.NewLocationSet(file.NewLocation("pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT")), + }, + ), + FoundBy: "python-installed-package-cataloger", + Metadata: pkg.PythonPackage{ + Name: "pep639-declared", + Version: "1.0.0", + SitePackagesRootPath: ".", + Files: []pkg.PythonFileRecord{ + {Path: "pep639_declared-1.0.0.dist-info/METADATA", Digest: &pkg.PythonFileDigest{Algorithm: "sha256", Value: "QM0YKaGTutHOanSl_CjBQDgF2cayCxbgyEPE0xhMGks"}, Size: "255"}, + {Path: "pep639_declared-1.0.0.dist-info/RECORD"}, + }, + }, + }, + }, + }, { name: "malformed-record", fixture: "testdata/site-packages/malformed-record", diff --git a/syft/pkg/cataloger/python/parse_wheel_egg.go b/syft/pkg/cataloger/python/parse_wheel_egg.go index b33ed693a..7615d252f 100644 --- a/syft/pkg/cataloger/python/parse_wheel_egg.go +++ b/syft/pkg/cataloger/python/parse_wheel_egg.go @@ -251,6 +251,15 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg } else { licenseLocations.Add(locs...) } + } else { + metadataDir := path.Dir(m.DistInfoLocation.Path()) + for _, name := range m.LicenseFiles { + // License-File may also list notices such as AUTHORS, which are not licenses + if !licenses.IsLicenseFile(path.Base(name)) { + continue + } + licenseLocations.Add(resolveDeclaredLicenseFile(resolver, metadataDir, name)...) + } } switch { @@ -264,7 +273,7 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg parent := path.Base(path.Dir(m.DistInfoLocation.LocalPath())) candidatePaths := strset.New() for _, f := range m.Files { - if !strings.HasPrefix(f.Path, parent) || strings.Count(f.Path, "/") > 1 { + if !isDistInfoLicenseCandidate(parent, f.Path) { continue } @@ -277,3 +286,29 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg } return pkg.NewLicenseSet(out...) } + +// resolveDeclaredLicenseFile finds a License-File entry, which PEP 639 (Metadata-Version 2.4) makes relative to +// .dist-info/licenses/ and older metadata makes relative to the metadata directory itself. +func resolveDeclaredLicenseFile(resolver file.Resolver, metadataDir, name string) []file.Location { + for _, p := range []string{path.Join(metadataDir, name), path.Join(metadataDir, "licenses", name)} { + locs, err := resolver.FilesByPath(p) + if err != nil { + log.WithFields("error", err, "path", p).Trace("unable to resolve python license file") + continue + } + if len(locs) > 0 { + return locs + } + } + return nil +} + +// isDistInfoLicenseCandidate reports whether a RECORD path sits directly in the .dist-info directory, +// or anywhere under its licenses/ subdirectory, where PEP 639 places license files. +func isDistInfoLicenseCandidate(distInfoDir, recordPath string) bool { + rel, found := strings.CutPrefix(recordPath, distInfoDir+"/") + if !found { + return false + } + return !strings.Contains(rel, "/") || strings.HasPrefix(rel, "licenses/") +} diff --git a/syft/pkg/cataloger/python/parse_wheel_egg_metadata.go b/syft/pkg/cataloger/python/parse_wheel_egg_metadata.go index 35874ebff..89f46dd35 100644 --- a/syft/pkg/cataloger/python/parse_wheel_egg_metadata.go +++ b/syft/pkg/cataloger/python/parse_wheel_egg_metadata.go @@ -23,13 +23,14 @@ type parsedData struct { // license info - Licenses string `mapstructure:"License"` - LicenseFile string `mapstructure:"LicenseFile"` - LicenseExpression string `mapstructure:"LicenseExpression"` + Licenses string `mapstructure:"License"` + LicenseFiles []string `mapstructure:"LicenseFile"` + LicenseExpression string `mapstructure:"LicenseExpression"` LicenseFilePath string } var pluralFields = map[string]bool{ + "LicenseFile": true, "ProvidesExtra": true, "RequiresDist": true, } @@ -53,8 +54,6 @@ func parseWheelOrEggMetadata(locationReader file.LocationReadCloser) (parsedData pd.SitePackagesRootPath = determineSitePackagesRootPath(metadataPath) if pd.Licenses != "" || pd.LicenseExpression != "" { pd.LicenseFilePath = metadataPath - } else if pd.LicenseFile != "" { - pd.LicenseFilePath = path.Join(path.Dir(metadataPath), pd.LicenseFile) } pd.DistInfoLocation = locationReader.Location diff --git a/syft/pkg/cataloger/python/parse_wheel_egg_metadata_test.go b/syft/pkg/cataloger/python/parse_wheel_egg_metadata_test.go index 6bbeb9e2f..cbd74f473 100644 --- a/syft/pkg/cataloger/python/parse_wheel_egg_metadata_test.go +++ b/syft/pkg/cataloger/python/parse_wheel_egg_metadata_test.go @@ -26,7 +26,6 @@ func TestParseWheelEggMetadata(t *testing.T) { ExpectedMetadata: parsedData{ DistInfoLocation: file.NewLocation("testdata/site-packages/nested/egg-name/egg-info/PKG-INFO"), Licenses: "Apache 2.0", - LicenseFile: "", LicenseExpression: "", LicenseFilePath: "testdata/site-packages/nested/egg-name/egg-info/PKG-INFO", PythonPackage: pkg.PythonPackage{ @@ -46,7 +45,6 @@ func TestParseWheelEggMetadata(t *testing.T) { ExpectedMetadata: parsedData{ DistInfoLocation: file.NewLocation("testdata/site-packages/nested/dist-name/dist-info/METADATA"), Licenses: "BSD License", - LicenseFile: "", LicenseExpression: "", LicenseFilePath: "testdata/site-packages/nested/dist-name/dist-info/METADATA", PythonPackage: pkg.PythonPackage{ @@ -156,7 +154,6 @@ func TestParseWheelEggMetadataInvalid(t *testing.T) { DistInfoLocation: file.NewLocation("testdata/site-packages/nested/egg-name/egg-info/PKG-INFO-INVALID"), Licenses: "", LicenseExpression: "", - LicenseFile: "", LicenseFilePath: "", PythonPackage: pkg.PythonPackage{ Name: "mxnet", diff --git a/syft/pkg/cataloger/python/parse_wheel_egg_test.go b/syft/pkg/cataloger/python/parse_wheel_egg_test.go new file mode 100644 index 000000000..baae02132 --- /dev/null +++ b/syft/pkg/cataloger/python/parse_wheel_egg_test.go @@ -0,0 +1,27 @@ +package python + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_isDistInfoLicenseCandidate(t *testing.T) { + tests := []struct { + name string + recordPath string + want bool + }{ + {name: "directly in dist-info", recordPath: "x.dist-info/LICENSE", want: true}, + {name: "under licenses", recordPath: "x.dist-info/licenses/LICENSE", want: true}, + {name: "nested under licenses", recordPath: "x.dist-info/licenses/vendor/LICENSE", want: true}, + {name: "other subdirectory of dist-info", recordPath: "x.dist-info/sub/LICENSE", want: false}, + {name: "sibling directory sharing the prefix", recordPath: "x.dist-info2/LICENSE", want: false}, + {name: "outside dist-info", recordPath: "x/LICENSE", want: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, isDistInfoLicenseCandidate("x.dist-info", tt.recordPath)) + }) + } +} diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/METADATA b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/METADATA new file mode 100644 index 000000000..2fc29751f --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/METADATA @@ -0,0 +1,8 @@ +Metadata-Version: 2.4 +Name: pep639-declared +Version: 1.0.0 +Summary: A package that declares two license files under .dist-info/licenses/ (PEP 639), which RECORD does not list. +License-File: LICENSE.BSD +License-File: LICENSE.MIT +License-File: AUTHORS.rst + diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/RECORD b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/RECORD new file mode 100644 index 000000000..3a752423e --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/RECORD @@ -0,0 +1,2 @@ +pep639_declared-1.0.0.dist-info/METADATA,sha256=QM0YKaGTutHOanSl_CjBQDgF2cayCxbgyEPE0xhMGks,255 +pep639_declared-1.0.0.dist-info/RECORD,, diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/AUTHORS.rst b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/AUTHORS.rst new file mode 100644 index 000000000..b8ab3da40 --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/AUTHORS.rst @@ -0,0 +1 @@ +Example Authors diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD new file mode 100644 index 000000000..9d227a0cc --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.BSD @@ -0,0 +1,28 @@ +Copyright 2010 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT new file mode 100644 index 000000000..1b3edae77 --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_declared-1.0.0.dist-info/licenses/LICENSE.MIT @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Example Authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/METADATA b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/METADATA new file mode 100644 index 000000000..2893aafe0 --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/METADATA @@ -0,0 +1,7 @@ +Metadata-Version: 2.4 +Name: pep639-example +Version: 1.0.0 +Summary: A package that ships its license only under .dist-info/licenses/ (PEP 639). +Classifier: License :: OSI Approved :: BSD License +Requires-Python: >=3.9 + diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/RECORD b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/RECORD new file mode 100644 index 000000000..9f93033a5 --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/RECORD @@ -0,0 +1,3 @@ +pep639_example-1.0.0.dist-info/METADATA,sha256=Z_-WBDzq246VHj56DO8yfBNNtlTqMj6q9xw_OGgeEMw,218 +pep639_example-1.0.0.dist-info/licenses/LICENSE,sha256=SJqOEQhQntmKN7uYPhHg9-HTHwvY-Zp5yESOf_N9B-o,1475 +pep639_example-1.0.0.dist-info/RECORD,, diff --git a/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/licenses/LICENSE b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/licenses/LICENSE new file mode 100644 index 000000000..9d227a0cc --- /dev/null +++ b/syft/pkg/cataloger/python/testdata/site-packages/license-pep639/pep639_example-1.0.0.dist-info/licenses/LICENSE @@ -0,0 +1,28 @@ +Copyright 2010 Pallets + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.