Skip to content

Commit 617c3e3

Browse files
feat(java): mark dependencies from maven-invoker-plugin integration tests pom.xml files as Dev (aquasecurity#6213)
Signed-off-by: knqyf263 <[email protected]> Co-authored-by: knqyf263 <[email protected]>
1 parent 56cedc0 commit 617c3e3

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

docs/docs/coverage/language/java.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ The vulnerability database will be downloaded anyway.
5555
!!! Warning
5656
Trivy may skip some dependencies (that were not found on your local machine) when the `--offline-scan` flag is passed.
5757

58+
59+
### maven-invoker-plugin
60+
Typically, the integration tests directory (`**/[src|target]/it/*/pom.xml`) of [maven-invoker-plugin][maven-invoker-plugin] doesn't contain actual `pom.xml` files and should be skipped to avoid noise.
61+
62+
Trivy marks dependencies from these files as the development dependencies and skip them by default.
63+
If you need to show them, use the `--include-dev-deps` flag.
64+
65+
5866
## Gradle.lock
5967
`gradle.lock` files contain all necessary information about used dependencies.
6068
Trivy simply parses the file, extract dependencies, and finds vulnerabilities for them.
@@ -69,4 +77,5 @@ It doesn't require the internet access.
6977
[^6]: `/Users/<username>/.m2/repository` (for Linux and Mac) and `C:/Users/<username>/.m2/repository` (for Windows) by default
7078
[^7]: To avoid confusion, Trivy only finds locations for direct dependencies from the base pom.xml file.
7179

72-
[dependency-graph]: ../../configuration/reporting.md#show-origins-of-vulnerable-dependencies
80+
[dependency-graph]: ../../configuration/reporting.md#show-origins-of-vulnerable-dependencies
81+
[maven-invoker-plugin]: https://maven.apache.org/plugins/maven-invoker-plugin/usage.html

pkg/fanal/analyzer/language/java/pom/pom.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"os"
66
"path/filepath"
7+
"strings"
78

89
"golang.org/x/xerrors"
910

@@ -23,11 +24,22 @@ const version = 1
2324
type pomAnalyzer struct{}
2425

2526
func (a pomAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
26-
p := pom.NewParser(filepath.Join(input.Dir, input.FilePath), pom.WithOffline(input.Options.Offline))
27+
filePath := filepath.Join(input.Dir, input.FilePath)
28+
p := pom.NewParser(filePath, pom.WithOffline(input.Options.Offline))
2729
res, err := language.Analyze(types.Pom, input.FilePath, input.Content, p)
2830
if err != nil {
2931
return nil, xerrors.Errorf("%s parse error: %w", input.FilePath, err)
3032
}
33+
34+
// Mark integration test pom files for `maven-invoker-plugin` as Dev to skip them by default.
35+
if isIntegrationTestDir(filePath) {
36+
for i := range res.Applications {
37+
for j := range res.Applications[i].Libraries {
38+
res.Applications[i].Libraries[j].Dev = true
39+
}
40+
}
41+
}
42+
3143
return res, nil
3244
}
3345

@@ -42,3 +54,14 @@ func (a pomAnalyzer) Type() analyzer.Type {
4254
func (a pomAnalyzer) Version() int {
4355
return version
4456
}
57+
58+
// isIntegrationTestDir checks that pom file is in directory with integration tests of `maven-invoker-plugin`
59+
// https://maven.apache.org/plugins/maven-invoker-plugin/usage.html
60+
func isIntegrationTestDir(filePath string) bool {
61+
dirs := strings.Split(filepath.ToSlash(filePath), "/")
62+
// filepath pattern: `**/[src|target]/it/*/pom.xml`
63+
if len(dirs) < 4 {
64+
return false
65+
}
66+
return (dirs[len(dirs)-4] == "src" || dirs[len(dirs)-4] == "target") && dirs[len(dirs)-3] == "it"
67+
}

pkg/fanal/analyzer/language/java/pom/pom_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ func Test_pomAnalyzer_Analyze(t *testing.T) {
8989
},
9090
},
9191
},
92+
{
93+
name: "happy path for maven-invoker-plugin integration tests",
94+
inputFile: "testdata/mark-as-dev/src/it/example/pom.xml",
95+
want: &analyzer.AnalysisResult{
96+
Applications: []types.Application{
97+
{
98+
Type: types.Pom,
99+
FilePath: "testdata/mark-as-dev/src/it/example/pom.xml",
100+
Libraries: types.Packages{
101+
{
102+
ID: "com.example:example-api:@example.version@",
103+
Name: "com.example:example-api",
104+
Version: "@example.version@",
105+
Locations: []types.Location{
106+
{
107+
StartLine: 28,
108+
EndLine: 32,
109+
},
110+
},
111+
Dev: true,
112+
},
113+
{
114+
ID: "com.example:example:1.0.0",
115+
Name: "com.example:example",
116+
Version: "1.0.0",
117+
Licenses: []string{"Apache-2.0"},
118+
DependsOn: []string{
119+
"com.example:example-api:@example.version@",
120+
},
121+
Dev: true,
122+
},
123+
},
124+
},
125+
},
126+
},
127+
},
92128
{
93129
name: "unsupported requirement",
94130
inputFile: "testdata/requirements/pom.xml",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
<groupId>com.example</groupId>
6+
<artifactId>example</artifactId>
7+
<version>1.0.0</version>
8+
9+
<name>example</name>
10+
<description>Example</description>
11+
12+
<licenses>
13+
<license>
14+
<name>Apache 2.0</name>
15+
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
16+
<distribution>repo</distribution>
17+
</license>
18+
</licenses>
19+
20+
<developers>
21+
<developer>
22+
<id>knqyf263</id>
23+
<url>https://github.com/knqyf263</url>
24+
</developer>
25+
</developers>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.example</groupId>
30+
<artifactId>example-api</artifactId>
31+
<version>@example.version@</version>
32+
</dependency>
33+
</dependencies>
34+
</project>

0 commit comments

Comments
 (0)