Skip to content

Commit 910c181

Browse files
committed
fix: incorrect license lookup issues
Signed-off-by: Keith Zantow <[email protected]>
1 parent 1037e0f commit 910c181

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

syft/pkg/cataloger/java/archive_parser.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -562,36 +562,49 @@ func getDigestsFromArchive(ctx context.Context, archivePath string) ([]file.Dige
562562
}
563563

564564
func (j *archiveParser) getLicenseFromFileInArchive(ctx context.Context) ([]pkg.License, error) {
565-
var out []pkg.License
566-
var licenseMatches []string
567-
for _, f := range j.fileManifest.GlobMatch(true, "/META-INF/*") {
568-
if licenses.IsLicenseFile(f) {
569-
licenseMatches = append(licenseMatches, f)
570-
}
571-
}
572-
if len(licenseMatches) == 0 {
573-
for _, f := range j.fileManifest.GlobMatch(true, "/*") {
574-
if licenses.IsLicenseFile(f) {
565+
// prefer identified licenses, fall back to unknown
566+
var identified []pkg.License
567+
var unidentified []pkg.License
568+
569+
for _, glob := range []string{"/META-INF/*", "/*"} {
570+
var licenseMatches []string
571+
for _, f := range j.fileManifest.GlobMatch(true, glob) {
572+
if licenses.IsLicenseFile(path.Base(f)) {
575573
licenseMatches = append(licenseMatches, f)
576574
}
577575
}
578-
}
579-
if len(licenseMatches) > 0 {
580-
contents, err := intFile.ContentsFromZip(j.archivePath, licenseMatches...)
581-
if err != nil {
582-
return nil, fmt.Errorf("unable to extract java license (%s): %w", j.location, err)
583-
}
584576

585-
for _, licenseMatch := range licenseMatches {
586-
licenseContents := contents[licenseMatch]
587-
r := strings.NewReader(licenseContents)
588-
lics := pkg.NewLicensesFromReadCloserWithContext(ctx, file.NewLocationReadCloser(j.location, io.NopCloser(r)))
589-
if len(lics) > 0 {
590-
out = append(out, lics...)
577+
if len(licenseMatches) > 0 {
578+
contents, err := intFile.ContentsFromZip(j.archivePath, licenseMatches...)
579+
if err != nil {
580+
return nil, fmt.Errorf("unable to extract java license (%s): %w", j.location, err)
581+
}
582+
583+
for _, licenseMatch := range licenseMatches {
584+
licenseContents := contents[licenseMatch]
585+
r := strings.NewReader(licenseContents)
586+
foundLicenses := pkg.NewLicensesFromReadCloserWithContext(ctx, file.NewLocationReadCloser(j.location, io.NopCloser(r)))
587+
for _, l := range foundLicenses {
588+
if l.SPDXExpression != "" {
589+
identified = append(identified, l)
590+
} else {
591+
unidentified = append(unidentified, l)
592+
}
593+
}
594+
}
595+
596+
// prefer licenses found in /META-INF
597+
if len(identified) > 0 {
598+
break
591599
}
592600
}
593601
}
594-
return out, nil
602+
603+
if len(identified) == 0 {
604+
return unidentified, nil
605+
}
606+
607+
return identified, nil
595608
}
596609

597610
func (j *archiveParser) discoverPkgsFromNestedArchives(ctx context.Context, parentPkg *pkg.Package) ([]pkg.Package, []artifact.Relationship, error) {

syft/pkg/cataloger/python/parse_wheel_egg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func findLicenses(ctx context.Context, resolver file.Resolver, m parsedData) pkg
280280
}
281281
}
282282

283-
out = licenses.FindInDirs(ctx, resolver, candidatePaths.List()...)
283+
out = licenses.FindAtPaths(ctx, resolver, candidatePaths.List()...)
284284
}
285285
return pkg.NewLicenseSet(out...)
286286
}

0 commit comments

Comments
 (0)