@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"archive/zip"
5
5
"bufio"
6
+ "errors"
6
7
"flag"
7
8
"fmt"
8
9
"io"
@@ -20,6 +21,16 @@ import (
20
21
"github.com/google/osv-scanner/experimental/javareach"
21
22
)
22
23
24
+ const MetaDirPath = "META-INF"
25
+
26
+ var (
27
+ ManifestFilePath = filepath .Join (MetaDirPath , "MANIFEST.MF" )
28
+ MavenDepDirPath = filepath .Join (MetaDirPath , "maven" )
29
+ ServiceDirPath = filepath .Join (MetaDirPath , "services" )
30
+
31
+ ErrMavenDependencyNotFound = errors .New (MavenDepDirPath + " directory not found" )
32
+ )
33
+
23
34
// Usage:
24
35
//
25
36
// go run ./cmd/reachable path/to/file.jar
@@ -99,6 +110,14 @@ func enumerateReachabilityForJar(jarPath string) error {
99
110
return err
100
111
}
101
112
113
+ // Reachability analysis is limited to Maven-built JARs for now.
114
+ // Check for the existence of the Maven metadata directory.
115
+ _ , err = os .Stat (filepath .Join (tmpDir , MavenDepDirPath ))
116
+ if err != nil {
117
+ slog .Error ("reachability analysis is only supported for JARs built with Maven." )
118
+ return ErrMavenDependencyNotFound
119
+ }
120
+
102
121
// Build .class -> Maven group ID:artifact ID mappings.
103
122
// TODO: Handle BOOT-INF and loading .jar dependencies from there.
104
123
classFinder , err := javareach .NewDefaultPackageFinder (allDeps , tmpDir )
@@ -107,7 +126,7 @@ func enumerateReachabilityForJar(jarPath string) error {
107
126
}
108
127
109
128
// Extract the main entrypoint.
110
- manifest , err := os .Open (filepath .Join (tmpDir , "META-INF/MANIFEST.MF" ))
129
+ manifest , err := os .Open (filepath .Join (tmpDir , ManifestFilePath ))
111
130
if err != nil {
112
131
return err
113
132
}
@@ -129,7 +148,7 @@ func enumerateReachabilityForJar(jarPath string) error {
129
148
130
149
// Look inside META-INF/services, which is used by
131
150
// https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html
132
- servicesDir := filepath .Join (tmpDir , "META-INF/services" )
151
+ servicesDir := filepath .Join (tmpDir , ServiceDirPath )
133
152
var optionalRootClasses []string
134
153
if _ , err := os .Stat (servicesDir ); err == nil {
135
154
var entries []string
@@ -194,7 +213,7 @@ func enumerateReachabilityForJar(jarPath string) error {
194
213
for _ , class := range result .Classes {
195
214
deps , err := classFinder .Find (class )
196
215
if err != nil {
197
- slog .Error ("Failed to find dep mapping" , "class" , class , "error" , err )
216
+ slog .Debug ("Failed to find dep mapping" , "class" , class , "error" , err )
198
217
continue
199
218
}
200
219
@@ -211,7 +230,7 @@ func enumerateReachabilityForJar(jarPath string) error {
211
230
slog .Info ("Found use of dynamic code loading" , "class" , class )
212
231
deps , err := classFinder .Find (class )
213
232
if err != nil {
214
- slog .Error ("Failed to find dep mapping" , "class" , class , "error" , err )
233
+ slog .Debug ("Failed to find dep mapping" , "class" , class , "error" , err )
215
234
continue
216
235
}
217
236
for _ , dep := range deps {
@@ -222,7 +241,7 @@ func enumerateReachabilityForJar(jarPath string) error {
222
241
slog .Info ("Found use of dependency injection" , "class" , class )
223
242
deps , err := classFinder .Find (class )
224
243
if err != nil {
225
- slog .Error ("Failed to find dep mapping" , "class" , class , "error" , err )
244
+ slog .Debug ("Failed to find dep mapping" , "class" , class , "error" , err )
226
245
continue
227
246
}
228
247
for _ , dep := range deps {
0 commit comments