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

feat(java): add support of test scope for pom.xml files #7486

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions pkg/dependency/parser/java/pom/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (p *Parser) resolve(art artifact, rootDepManagement []pomDependency) (analy
result, err := p.analyze(pomContent, analysisOptions{
exclusions: art.Exclusions,
depManagement: rootDepManagement,
testScope: art.Test,
})
if err != nil {
return analysisResult{}, xerrors.Errorf("analyze error: %w", err)
Expand All @@ -325,6 +326,7 @@ type analysisOptions struct {
exclusions map[string]struct{}
depManagement []pomDependency // from the root POM
lineNumber bool // Save line numbers
testScope bool
}

func (p *Parser) analyze(pom *pom, opts analysisOptions) (analysisResult, error) {
Expand Down
49 changes: 49 additions & 0 deletions pkg/dependency/parser/java/pom/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,55 @@ func TestPom_Parse(t *testing.T) {
},
},
},
{
name: "include dependencies with test scope",
inputFile: filepath.Join("testdata", "test-scope", "pom.xml"),
local: true,
want: []ftypes.Package{
{
ID: "com.example:test-example:1.0.0",
Name: "com.example:test-example",
Version: "1.0.0",
Relationship: ftypes.RelationshipRoot,
},

{
ID: "org.example:example-dependency:1.2.3",
Name: "org.example:example-dependency",
Version: "1.2.3",
Relationship: ftypes.RelationshipDirect,
Dev: true,
Locations: ftypes.Locations{
{
StartLine: 13,
EndLine: 18,
},
},
},
{
ID: "org.example:example-api:2.0.0",
Name: "org.example:example-api",
Version: "2.0.0",
Licenses: []string{"The Apache Software License, Version 2.0"},
Relationship: ftypes.RelationshipIndirect,
Dev: true,
},
},
wantDeps: []ftypes.Dependency{
{
ID: "com.example:test-example:1.0.0",
DependsOn: []string{
"org.example:example-dependency:1.2.3",
},
},
{
ID: "org.example:example-dependency:1.2.3",
DependsOn: []string{
"org.example:example-api:2.0.0",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func (d pomDependency) ToArtifact(opts analysisOptions) artifact {
Exclusions: exclusions,
Locations: locations,
Relationship: ftypes.RelationshipIndirect, // default
Test: d.Scope == "test",
Test: d.Scope == "test" || opts.testScope,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it works as expected. For example, if two direct dependencies have the same transitive dependency, and one scope is "test" and the other is not, I guess it would not be considered devDependency. How does Maven work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... Nice catch.

I updated logic and added test for this case (comment before test shows how mvn works).
Can you take a look and check that this is what you meant?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it mean it'll include the test even if testScope is false? Unlike the other PR, this PR still includes test dependencies for me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... that's weird.
Do you see any vulnerabilities for test dependencies (or test packages in json format)?

debug logs for test dependencies are normal for this PR, because Trivy currently analyzes all dependencies and excludes Dev dependencies later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It takes ages to scan and then fails with:

semaphore acquire: context deadline exceeded

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is timeout error.
Trivy's runtime has increased because Trivy first checks all dependencies (including test ones), and only then excludes them from the report

}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/dependency/parser/java/pom/testdata/test-scope/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>test-example</artifactId>
<version>1.0.0</version>

<name>test-example</name>
<description>Example</description>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>example-dependency</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>