Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 931c507

Browse files
DmitriyLewenfhielpos
authored andcommittedDec 20, 2024
feat(java): add test scope support for pom.xml files (aquasecurity#7414)
1 parent c8df0ae commit 931c507

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed
 

‎docs/docs/coverage/language/java.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ Each artifact supports the following scanners:
1212

1313
The following table provides an outline of the features Trivy offers.
1414

15-
| Artifact | Internet access | Dev dependencies | [Dependency graph][dependency-graph] | Position | [Detection Priority][detection-priority] |
16-
|------------------|:---------------------:|:----------------:|:------------------------------------:|:--------:|:----------------------------------------:|
17-
| JAR/WAR/PAR/EAR | Trivy Java DB | Include | - | - | Not needed |
18-
| pom.xml | Maven repository [^1] | Exclude ||[^7] | - |
19-
| *gradle.lockfile | - | Exclude ||| Not needed |
20-
| *.sbt.lock | - | Exclude | - || Not needed |
15+
| Artifact | Internet access | Dev dependencies | [Dependency graph][dependency-graph] | Position | [Detection Priority][detection-priority] |
16+
|------------------|:---------------------:|:------------------:|:------------------------------------:|:--------:|:----------------------------------------:|
17+
| JAR/WAR/PAR/EAR | Trivy Java DB | Include | - | - | Not needed |
18+
| pom.xml | Maven repository [^1] | [Exclude](#scopes) ||[^7] | - |
19+
| *gradle.lockfile | - | Exclude ||| Not needed |
20+
| *.sbt.lock | - | Exclude | - || Not needed |
2121

2222
These may be enabled or disabled depending on the target.
2323
See [here](./index.md) for the detail.
@@ -69,6 +69,11 @@ The vulnerability database will be downloaded anyway.
6969
!!! Warning
7070
Trivy may skip some dependencies (that were not found on your local machine) when the `--offline-scan` flag is passed.
7171

72+
### scopes
73+
Trivy supports `runtime`, `compile`, `test` and `import` (for `dependencyManagement`) [dependency scopes][dependency-scopes].
74+
Dependencies without scope are also detected.
75+
76+
By default, Trivy doesn't report dependencies with `test` scope. Use the `--include-dev-deps` flag to include them.
7277

7378
### maven-invoker-plugin
7479
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.
@@ -120,3 +125,4 @@ Make sure that you have cache[^8] directory to find licenses from `*.pom` depend
120125
[maven-pom-repos]: https://maven.apache.org/settings.html#repositories
121126
[sbt-dependency-lock]: https://stringbean.github.io/sbt-dependency-lock
122127
[detection-priority]: ../../scanner/vulnerability.md#detection-priority
128+
[dependency-scopes]: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

‎pkg/dependency/parser/java/pom/artifact.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type artifact struct {
2727

2828
Module bool
2929
Relationship ftypes.Relationship
30+
Test bool
3031

3132
Locations ftypes.Locations
3233
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func (p *Parser) parseRoot(root artifact, uniqModules map[string]struct{}) ([]ft
214214
Licenses: result.artifact.Licenses,
215215
Relationship: art.Relationship,
216216
Locations: art.Locations,
217+
Test: art.Test,
217218
}
218219

219220
// save only dependency names
@@ -234,6 +235,7 @@ func (p *Parser) parseRoot(root artifact, uniqModules map[string]struct{}) ([]ft
234235
Licenses: art.Licenses,
235236
Relationship: art.Relationship,
236237
Locations: art.Locations,
238+
Dev: art.Test,
237239
}
238240
pkgs = append(pkgs, pkg)
239241

@@ -400,7 +402,7 @@ func (p *Parser) parseDependencies(deps []pomDependency, props map[string]string
400402
// Resolve dependencies
401403
d = d.Resolve(props, depManagement, rootDepManagement)
402404

403-
if (d.Scope != "" && d.Scope != "compile" && d.Scope != "runtime") || d.Optional {
405+
if (d.Scope != "" && d.Scope != "compile" && d.Scope != "runtime" && d.Scope != "test") || d.Optional {
404406
continue
405407
}
406408

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

+28
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,27 @@ func TestPom_Parse(t *testing.T) {
6161
},
6262
},
6363
},
64+
{
65+
ID: "org.example:example-test:2.0.0",
66+
Name: "org.example:example-test",
67+
Version: "2.0.0",
68+
Relationship: ftypes.RelationshipDirect,
69+
Dev: true,
70+
Locations: ftypes.Locations{
71+
{
72+
StartLine: 49,
73+
EndLine: 54,
74+
},
75+
},
76+
},
6477
},
6578
wantDeps: []ftypes.Dependency{
6679
{
6780
ID: "com.example:happy:1.0.0",
6881
DependsOn: []string{
6982
"org.example:example-api:1.7.30",
7083
"org.example:example-runtime:1.0.0",
84+
"org.example:example-test:2.0.0",
7185
},
7286
},
7387
},
@@ -109,13 +123,27 @@ func TestPom_Parse(t *testing.T) {
109123
},
110124
},
111125
},
126+
{
127+
ID: "org.example:example-test:2.0.0",
128+
Name: "org.example:example-test",
129+
Version: "2.0.0",
130+
Relationship: ftypes.RelationshipDirect,
131+
Dev: true,
132+
Locations: ftypes.Locations{
133+
{
134+
StartLine: 49,
135+
EndLine: 54,
136+
},
137+
},
138+
},
112139
},
113140
wantDeps: []ftypes.Dependency{
114141
{
115142
ID: "com.example:happy:1.0.0",
116143
DependsOn: []string{
117144
"org.example:example-api:1.7.30",
118145
"org.example:example-runtime:1.0.0",
146+
"org.example:example-test:2.0.0",
119147
},
120148
},
121149
},

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

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ func (d pomDependency) ToArtifact(opts analysisOptions) artifact {
303303
Exclusions: exclusions,
304304
Locations: locations,
305305
Relationship: ftypes.RelationshipIndirect, // default
306+
Test: d.Scope == "test",
306307
}
307308
}
308309

‎pkg/dependency/parser/java/pom/testdata/happy/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@
4646
<version>999</version>
4747
<scope>provided</scope>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.example</groupId>
51+
<artifactId>example-test</artifactId>
52+
<version>2.0.0</version>
53+
<scope>test</scope>
54+
</dependency>
4955
</dependencies>
5056
</project>

0 commit comments

Comments
 (0)
Please sign in to comment.