Skip to content

Commit 7c409fd

Browse files
authored
fix(java): parse modules from pom.xml files once (aquasecurity#6312)
1 parent 1b68327 commit 7c409fd

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

pkg/dependency/parser/java/pom/parse.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ func (p *parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
105105
// Cache root POM
106106
p.cache.put(result.artifact, result)
107107

108-
return p.parseRoot(root.artifact())
108+
return p.parseRoot(root.artifact(), make(map[string]struct{}))
109109
}
110110

111-
func (p *parser) parseRoot(root artifact) ([]types.Library, []types.Dependency, error) {
111+
func (p *parser) parseRoot(root artifact, uniqModules map[string]struct{}) ([]types.Library, []types.Dependency, error) {
112112
// Prepare a queue for dependencies
113113
queue := newArtifactQueue()
114114

@@ -132,7 +132,12 @@ func (p *parser) parseRoot(root artifact) ([]types.Library, []types.Dependency,
132132
// Modules should be handled separately so that they can have independent dependencies.
133133
// It means multi-module allows for duplicate dependencies.
134134
if art.Module {
135-
moduleLibs, moduleDeps, err := p.parseRoot(art)
135+
if _, ok := uniqModules[art.String()]; ok {
136+
continue
137+
}
138+
uniqModules[art.String()] = struct{}{}
139+
140+
moduleLibs, moduleDeps, err := p.parseRoot(art, uniqModules)
136141
if err != nil {
137142
return nil, nil, err
138143
}

pkg/dependency/parser/java/pom/parse_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,43 @@ func TestPom_Parse(t *testing.T) {
959959
},
960960
},
961961
},
962+
{
963+
name: "Infinity loop for modules",
964+
inputFile: filepath.Join("testdata", "modules-infinity-loop", "pom.xml"),
965+
local: true,
966+
want: []types.Library{
967+
// as module
968+
{
969+
ID: "org.example:module-1:2.0.0",
970+
Name: "org.example:module-1",
971+
Version: "2.0.0",
972+
},
973+
// as dependency
974+
{
975+
ID: "org.example:module-1:2.0.0",
976+
Name: "org.example:module-1",
977+
Version: "2.0.0",
978+
},
979+
{
980+
ID: "org.example:module-2:3.0.0",
981+
Name: "org.example:module-2",
982+
Version: "3.0.0",
983+
},
984+
{
985+
ID: "org.example:root:1.0.0",
986+
Name: "org.example:root",
987+
Version: "1.0.0",
988+
},
989+
},
990+
wantDeps: []types.Dependency{
991+
{
992+
ID: "org.example:module-2:3.0.0",
993+
DependsOn: []string{
994+
"org.example:module-1:2.0.0",
995+
},
996+
},
997+
},
998+
},
962999
{
9631000
name: "multi module soft requirement",
9641001
inputFile: filepath.Join("testdata", "multi-module-soft-requirement", "pom.xml"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>module-2</artifactId>
6+
<groupId>org.example</groupId>
7+
<version>3.0.0</version>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.example</groupId>
12+
<artifactId>module-1</artifactId>
13+
<version>2.0.0</version>
14+
</dependency>
15+
</dependencies>
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>module-1</artifactId>
6+
<groupId>org.example</groupId>
7+
<version>2.0.0</version>
8+
9+
<modules>
10+
<module>module-2</module>
11+
</modules>
12+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>root</artifactId>
6+
<groupId>org.example</groupId>
7+
<version>1.0.0</version>
8+
9+
<modules>
10+
<module>module-1</module>
11+
<module>module-2</module>
12+
</modules>
13+
</project>

0 commit comments

Comments
 (0)