Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better test for group ID in filename #2565

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion syft/pkg/cataloger/java/parse_java_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"strconv"
"strings"
"unicode"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/pkg"
Expand Down Expand Up @@ -162,13 +163,40 @@ func extractNameFromArchiveFilename(a archiveFilename) string {
return a.name
}

// Maybe the filename is like groupid + . + artifactid. If so, return artifact id.
fields := strings.Split(a.name, ".")
return fields[len(fields)-1]
maybeGroupID := true
for _, f := range fields {
if !isValidJavaIdentifier(f) {
maybeGroupID = false
break
}
}
if maybeGroupID {
return fields[len(fields)-1]
}
}

return a.name
}

func isValidJavaIdentifier(field string) bool {
runes := []rune(field)
if len(runes) == 0 {
return false
}
// check whether first rune can start an identifier name in Java
// Java identifier start = [Lu]|[Ll]|[Lt]|[Lm]|[Lo]|[Nl]|[Sc]|[Pc]
// see https://developer.classpath.org/doc/java/lang/Character-source.html
// line 3295
r := runes[0]
return unicode.Is(unicode.Lu, r) ||
unicode.Is(unicode.Ll, r) || unicode.Is(unicode.Lt, r) ||
unicode.Is(unicode.Lm, r) || unicode.Is(unicode.Lo, r) ||
unicode.Is(unicode.Nl, r) ||
unicode.Is(unicode.Sc, r) || unicode.Is(unicode.Pc, r)
}

func selectName(manifest *pkg.JavaManifest, filenameObj archiveFilename) string {
name := extractNameFromApacheMavenBundlePlugin(manifest)
if name != "" {
Expand Down
16 changes: 16 additions & 0 deletions syft/pkg/cataloger/java/parse_java_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@ func TestSelectName(t *testing.T) {
archive: newJavaArchiveFilename("/something/com.atlassian.gadgets.atlassian-gadgets-api.jar"),
expected: "atlassian-gadgets-api",
},
{
desc: "Filename has period that is not groupid + artifact id",
manifest: pkg.JavaManifest{
Main: map[string]string{},
},
archive: newJavaArchiveFilename("/something/http4s-crypto_2.12-0.1.0.jar"),
expected: "http4s-crypto_2.12",
},
{
desc: "Filename has period that is not groupid + artifact id, kafka",
manifest: pkg.JavaManifest{
Main: map[string]string{},
},
archive: newJavaArchiveFilename("/something//kafka_2.13-3.2.2.jar"),
expected: "kafka_2.13", // see https://mvnrepository.com/artifact/org.apache.kafka/kafka_2.13/3.2.2
},
{
desc: "Skip stripping groupId prefix from archive filename for org.eclipse",
manifest: pkg.JavaManifest{
Expand Down
Loading