-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix(java): add hash of GAV+root pom file path for pkgID for packages from pom.xml files #9880
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(java): add hash of GAV+root pom file path for pkgID for packages from pom.xml files #9880
Conversation
- add RootFilePath for analysisOptions - store analysisOptions for found deps - save analysisOptions for artifact from analysisResult
- dependency - analyzer
|
@knqyf263 I created PR with your idea (#7879 (comment)) |
d5108b3 to
c111673
Compare
knqyf263
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left one comment with my idea, but overall it looks good to me.
| func packageID(name, version, pomFilePath string) string { | ||
| v := map[string]any{ | ||
| "gav": dependency.ID(ftypes.Pom, name, version), | ||
| "path": filepath.ToSlash(pomFilePath), | ||
| } | ||
| h, err := hashstructure.Hash(v, hashstructure.FormatV2, &hashstructure.HashOptions{ | ||
| ZeroNil: true, | ||
| IgnoreZeroValue: true, | ||
| }) | ||
| if err != nil { | ||
| log.Warn("Failed to calculate the pom.xml hash", log.String("name", name), log.String("version", version), log.Err(err)) | ||
| } | ||
| return strconv.FormatUint(h, 16) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about including GAV in the ID for readability? Something like com.example:log4shell:1.0-SNAPSHOT::a302c021 (GAV + 8-char hash suffix).
| func packageID(name, version, pomFilePath string) string { | |
| v := map[string]any{ | |
| "gav": dependency.ID(ftypes.Pom, name, version), | |
| "path": filepath.ToSlash(pomFilePath), | |
| } | |
| h, err := hashstructure.Hash(v, hashstructure.FormatV2, &hashstructure.HashOptions{ | |
| ZeroNil: true, | |
| IgnoreZeroValue: true, | |
| }) | |
| if err != nil { | |
| log.Warn("Failed to calculate the pom.xml hash", log.String("name", name), log.String("version", version), log.Err(err)) | |
| } | |
| return strconv.FormatUint(h, 16) | |
| func packageID(name, version, pomFilePath string) string { | |
| gav := dependency.ID(ftypes.Pom, name, version) | |
| v := map[string]any{ | |
| "gav": gav, | |
| "path": filepath.ToSlash(pomFilePath), | |
| } | |
| h, err := hashstructure.Hash(v, hashstructure.FormatV2, &hashstructure.HashOptions{ | |
| ZeroNil: true, | |
| IgnoreZeroValue: true, | |
| }) | |
| if err != nil { | |
| log.Warn("Failed to calculate hash", log.Err(err)) | |
| return gav // fallback to GAV only | |
| } | |
| // Append 8-character hash suffix | |
| return fmt.Sprintf("%s::%s", gav, strconv.FormatUint(h, 16)[:8]) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good idea!
Updated in 1be9285
pkg/report/table/vulnerability.go
Outdated
| // vulnID builds human-readable vulnerability package ID for POM target type (override hash to groupId:artifactId:version) | ||
| // and returns package ID for other target types. | ||
| func vulnID(t ftypes.TargetType, pkg ftypes.Package) string { | ||
| if t == ftypes.Pom { | ||
| pomIDInfoOnce() | ||
| return dependency.ID(t, pkg.Name, pkg.Version) | ||
| } | ||
| return pkg.ID | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
com.example:log4shell:1.0-SNAPSHOT::a302c021 doesn't need this function. Or, we can just cut ::.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Let's use new IDs.
If users ask to remove the hash suffix, we will trim it in another PR.
Co-authored-by: Teppei Fukuda <[email protected]>
Description
This PR refactors Maven POM dependency parsing to use a composite package ID format that combines GAV (GroupId:ArtifactId:Version) coordinates with a hash of the POM file path. The new format is groupId:artifactId:version::hash8, where hash8 is an 8-character hash derived from both the GAV and the file path.
This change addresses issues with duplicate package identification in multi-module Maven projects where the same dependency can appear in different modules with potentially different transitive dependency trees.
Changes
Benefits
Reasons
Problem: In multi-module Maven projects, the same dependency (same GAV coordinates) can appear in multiple modules. Previously, Trivy used only the GAV as the package ID, which caused:
Related issues
Related PRs
pom.xmlfiles. #7879Checklist