Skip to content

Commit 45a6bf2

Browse files
authored
fix: gradle component analysis duplicate artifacts in sbom (#130)
Signed-off-by: Zvi Grinberg <[email protected]>
1 parent a89e625 commit 45a6bf2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/providers/java_gradle.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ function stripString(depPart) {
3232
return depPart.replaceAll(/["']/g,"")
3333
}
3434

35+
/** this function checks whether a line from `gradle dependencies` output contains a version or not
36+
*
37+
* @param line the line from `gradle dependencies` output.
38+
* @return {*|boolean}
39+
*/
40+
function containsVersion(line) {
41+
let lineStriped = line.replace("(n)","").trim()
42+
return (lineStriped.match(/\W*[a-z0-9.-]+:[a-z0-9.-]+:[0-9]+[.][0-9]+(.[0-9]+)?(.*)?.*/)
43+
|| lineStriped.match(/.*version:\s?(')?[0-9]+[.][0-9]+(.[0-9]+)?(')?/)) && !lineStriped.includes("libs.")
44+
}
45+
3546
export default class Java_gradle extends Base_java {
3647

3748
/**
@@ -210,10 +221,14 @@ export default class Java_gradle extends Base_java {
210221
// transform gradle dependency tree to the form of maven dependency tree to use common sbom build algorithm in Base_java parent */
211222
let arrayForSbom = lines.map(dependency => dependency.replaceAll("---", "-").replaceAll(" ", " "))
212223
.map(dependency => dependency.replaceAll(/:(.*):(.*) -> (.*)$/g, ":$1:$3"))
224+
.map(dependency => dependency.replaceAll(/:(.*)\W*->\W*(.*)$/g, ":$1:$2"))
213225
.map(dependency => dependency.replaceAll(/(.*):(.*):(.*)$/g, "$1:$2:jar:$3"))
214226
.map(dependency => dependency.replaceAll(/(n)$/g), "")
215227
.map(dependency => `${dependency}:compile`);
216-
this.parseDependencyTree(root, 0, arrayForSbom.slice(1), sbom)
228+
if(!containsVersion(arrayForSbom[0])) {
229+
arrayForSbom = arrayForSbom.slice(1)
230+
}
231+
this.parseDependencyTree(root + ":compile", 0, arrayForSbom, sbom)
217232
let ignoredDeps = this.#getIgnoredDeps(manifestPath)
218233
return sbom.filterIgnoredDepsIncludingVersion(ignoredDeps).getAsJsonString();
219234
}
@@ -236,7 +251,9 @@ export default class Java_gradle extends Base_java {
236251
}
237252

238253
if (startFound && dependency.trim() !== "") {
239-
resultList.push(dependenciesList[dependency])
254+
if(startMarker === 'runtimeClasspath' || containsVersion(dependenciesList[dependency])) {
255+
resultList.push(dependenciesList[dependency])
256+
}
240257
}
241258

242259
if (startFound && dependenciesList[dependency].trim() === "") {

test/it/test_manifests/gradle/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ dependencies {
2121
implementation "jakarta.validation:jakarta.validation-api:2.0.2"
2222
implementation "io.quarkus:quarkus-resteasy-multipart:2.13.7.Final"
2323
implementation "io.quarkus:quarkus-hibernate-orm-deployment:2.0.2.Final"
24-
implementation "log4j:log4j:1.2.17" // exhortignore
24+
implementation "log4j:log4j:1.2.17"
25+
implementation group: 'log4j', name: 'log4j'
2526
}
2627
test {
2728
useJUnitPlatform()

0 commit comments

Comments
 (0)