1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.maven;
19
20 import com.github.packageurl.MalformedPackageURLException;
21 import com.github.packageurl.PackageURL.StandardTypes;
22 import com.github.packageurl.PackageURL;
23 import io.github.jeremylong.jcs3.slf4j.Slf4jAdapter;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.DefaultArtifact;
26 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
27 import org.apache.maven.artifact.versioning.ArtifactVersion;
28 import org.apache.maven.doxia.sink.Sink;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.model.License;
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.plugins.annotations.Component;
35 import org.apache.maven.plugins.annotations.Parameter;
36 import org.apache.maven.project.DefaultProjectBuildingRequest;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.ProjectBuildingRequest;
39 import org.apache.maven.reporting.MavenReport;
40 import org.apache.maven.reporting.MavenReportException;
41 import org.apache.maven.settings.Proxy;
42 import org.apache.maven.settings.Server;
43 import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
44 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
45 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
46 import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
47 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolver;
48 import org.apache.maven.shared.transfer.dependencies.resolve.DependencyResolverException;
49 import org.eclipse.aether.artifact.ArtifactType;
50 import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
51 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
52 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
53 import org.apache.maven.shared.dependency.graph.DependencyNode;
54 import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
55 import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
56 import org.apache.maven.shared.model.fileset.FileSet;
57 import org.apache.maven.shared.model.fileset.util.FileSetManager;
58 import org.owasp.dependencycheck.Engine;
59 import org.owasp.dependencycheck.analyzer.JarAnalyzer;
60 import org.owasp.dependencycheck.data.nexus.MavenArtifact;
61 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
62 import org.owasp.dependencycheck.dependency.Confidence;
63 import org.owasp.dependencycheck.dependency.Dependency;
64 import org.owasp.dependencycheck.dependency.EvidenceType;
65 import org.owasp.dependencycheck.dependency.Vulnerability;
66 import org.owasp.dependencycheck.exception.DependencyNotFoundException;
67 import org.owasp.dependencycheck.exception.ExceptionCollection;
68 import org.owasp.dependencycheck.exception.ReportException;
69 import org.owasp.dependencycheck.utils.Checksum;
70 import org.owasp.dependencycheck.utils.Filter;
71 import org.owasp.dependencycheck.utils.Downloader;
72 import org.owasp.dependencycheck.utils.InvalidSettingException;
73 import org.owasp.dependencycheck.utils.Settings;
74 import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
75 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
76 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
77
78 import java.io.File;
79 import java.io.FileNotFoundException;
80 import java.io.IOException;
81 import java.io.InputStream;
82 import java.util.ArrayList;
83 import java.util.Arrays;
84 import java.util.Collections;
85 import java.util.HashSet;
86 import java.util.List;
87 import java.util.Locale;
88 import java.util.Map;
89 import java.util.Objects;
90 import java.util.Optional;
91 import java.util.Set;
92 import org.apache.maven.artifact.repository.ArtifactRepository;
93
94 import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
95 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
96 import org.apache.maven.artifact.versioning.Restriction;
97 import org.apache.maven.artifact.versioning.VersionRange;
98
99 import org.owasp.dependencycheck.agent.DependencyCheckScanAgent;
100 import org.owasp.dependencycheck.dependency.naming.GenericIdentifier;
101 import org.owasp.dependencycheck.dependency.naming.Identifier;
102 import org.owasp.dependencycheck.dependency.naming.PurlIdentifier;
103 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
104 import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
105 import org.apache.maven.shared.transfer.dependencies.DefaultDependableCoordinate;
106 import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
107 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
108 import org.owasp.dependencycheck.reporting.ReportGenerator;
109 import org.owasp.dependencycheck.utils.SeverityUtil;
110 import org.owasp.dependencycheck.xml.pom.Model;
111 import org.owasp.dependencycheck.xml.pom.PomUtils;
112
113
114
115
116
117 public abstract class BaseDependencyCheckMojo extends AbstractMojo implements MavenReport {
118
119
120
121
122
123 private static final String PROPERTIES_FILE = "mojo.properties";
124
125
126
127 private static final String NEW_LINE = System.getProperty("line.separator", "\n").intern();
128
129
130
131 private static final String INCLUDE_ALL = "**/*";
132
133
134
135 private boolean generatingSite = false;
136
137
138
139 private Settings settings = null;
140
141
142
143 private final List<File> scannedFiles = new ArrayList<>();
144
145
146
147
148
149 @SuppressWarnings("CanBeFinal")
150 @Parameter(property = "failOnError", defaultValue = "true", required = true)
151 private boolean failOnError;
152
153
154
155
156 @SuppressWarnings("CanBeFinal")
157 @Parameter(property = "project", required = true, readonly = true)
158 private MavenProject project;
159
160
161
162 @SuppressWarnings("CanBeFinal")
163 @Parameter(readonly = true, required = true, property = "reactorProjects")
164 private List<MavenProject> reactorProjects;
165
166
167
168
169
170 @SuppressWarnings("CanBeFinal")
171 @Component
172 private ArtifactResolver artifactResolver;
173
174
175
176
177
178
179
180 @SuppressWarnings("CanBeFinal")
181 @Component
182 private DependencyResolver dependencyResolver;
183
184
185
186
187 @SuppressWarnings("CanBeFinal")
188 @Parameter(defaultValue = "${session}", readonly = true, required = true)
189 private MavenSession session;
190
191
192
193
194 @Component
195 private DependencyGraphBuilder dependencyGraphBuilder;
196
197
198
199
200 @SuppressWarnings("CanBeFinal")
201 @Parameter(defaultValue = "${project.build.directory}", required = true, property = "odc.outputDirectory")
202 private File outputDirectory;
203
204
205
206
207
208 @Parameter(property = "project.reporting.outputDirectory", readonly = true)
209 private File reportOutputDirectory;
210
211
212
213
214
215 @SuppressWarnings("CanBeFinal")
216 @Parameter(property = "failBuildOnCVSS", defaultValue = "11", required = true)
217 private float failBuildOnCVSS = 11f;
218
219
220
221
222
223 @SuppressWarnings("CanBeFinal")
224 @Parameter(property = "junitFailOnCVSS", defaultValue = "0", required = true)
225 private float junitFailOnCVSS = 0;
226
227
228
229
230
231
232 @SuppressWarnings("CanBeFinal")
233 @Parameter(property = "failBuildOnAnyVulnerability", defaultValue = "false", required = true)
234 @Deprecated
235 private boolean failBuildOnAnyVulnerability = false;
236
237
238
239
240 @SuppressWarnings("CanBeFinal")
241 @Parameter(property = "autoUpdate")
242 private Boolean autoUpdate;
243
244
245
246 @SuppressWarnings("CanBeFinal")
247 @Parameter(property = "enableExperimental")
248 private Boolean enableExperimental;
249
250
251
252 @SuppressWarnings("CanBeFinal")
253 @Parameter(property = "enableRetired")
254 private Boolean enableRetired;
255
256
257
258 @SuppressWarnings("CanBeFinal")
259 @Parameter(property = "golangDepEnabled")
260 private Boolean golangDepEnabled;
261
262
263
264
265 @SuppressWarnings("CanBeFinal")
266 @Parameter(property = "golangModEnabled")
267 private Boolean golangModEnabled;
268
269
270
271 @SuppressWarnings("CanBeFinal")
272 @Parameter(property = "pathToGo")
273 private String pathToGo;
274
275
276
277
278 @SuppressWarnings("CanBeFinal")
279 @Parameter(property = "pathToYarn")
280 private String pathToYarn;
281
282
283
284 @SuppressWarnings("CanBeFinal")
285 @Parameter(property = "pathToPnpm")
286 private String pathToPnpm;
287
288
289
290
291 @Parameter(property = "dependency-check.virtualSnapshotsFromReactor", defaultValue = "true")
292 private Boolean virtualSnapshotsFromReactor;
293
294
295
296
297
298 @SuppressWarnings("CanBeFinal")
299 @Parameter(property = "format", defaultValue = "HTML", required = true)
300 private String format = "HTML";
301
302
303
304
305
306 @Parameter(property = "prettyPrint")
307 private Boolean prettyPrint;
308
309
310
311
312
313 @Parameter(property = "formats", required = true)
314 private String[] formats;
315
316
317
318 @SuppressWarnings("CanBeFinal")
319 @Parameter(property = "mavenSettings", defaultValue = "${settings}")
320 private org.apache.maven.settings.Settings mavenSettings;
321
322
323
324
325 @SuppressWarnings("CanBeFinal")
326 @Parameter(property = "mavenSettingsProxyId")
327 private String mavenSettingsProxyId;
328
329
330
331
332 @SuppressWarnings("CanBeFinal")
333 @Parameter(property = "connectionTimeout")
334 private String connectionTimeout;
335
336
337
338 @SuppressWarnings("CanBeFinal")
339 @Parameter(property = "readTimeout")
340 private String readTimeout;
341
342
343
344
345 @SuppressWarnings("CanBeFinal")
346 @Parameter(property = "versionCheckEnabled", defaultValue = "true")
347 private boolean versionCheckEnabled;
348
349
350
351
352
353
354 @SuppressWarnings("CanBeFinal")
355 @Parameter(property = "suppressionFiles")
356 private String[] suppressionFiles;
357
358
359
360
361
362
363 @SuppressWarnings("CanBeFinal")
364 @Parameter(property = "suppressionFile")
365 private String suppressionFile;
366
367
368
369 @Parameter(property = "suppressionFileUser")
370 private String suppressionFileUser;
371
372
373
374 @Parameter(property = "suppressionFilePassword")
375 private String suppressionFilePassword;
376
377
378
379
380 @SuppressWarnings("CanBeFinal")
381 @Parameter(property = "suppressionFileServerId")
382 private String suppressionFileServerId;
383
384
385
386 @SuppressWarnings("CanBeFinal")
387 @Parameter(property = "hintsFile")
388 private String hintsFile;
389
390
391
392
393 @SuppressWarnings("CanBeFinal")
394 @Parameter(property = "showSummary", defaultValue = "true")
395 private boolean showSummary = true;
396
397
398
399
400 @SuppressWarnings("CanBeFinal")
401 @Parameter(property = "jarAnalyzerEnabled")
402 private Boolean jarAnalyzerEnabled;
403
404
405
406
407 @SuppressWarnings("CanBeFinal")
408 @Parameter(property = "dartAnalyzerEnabled")
409 private Boolean dartAnalyzerEnabled;
410
411
412
413
414 @SuppressWarnings("CanBeFinal")
415 @Parameter(property = "archiveAnalyzerEnabled")
416 private Boolean archiveAnalyzerEnabled;
417
418
419
420 @SuppressWarnings("CanBeFinal")
421 @Parameter(property = "knownExploitedEnabled")
422 private Boolean knownExploitedEnabled;
423
424
425
426 @SuppressWarnings("CanBeFinal")
427 @Parameter(property = "knownExploitedUrl")
428 private String knownExploitedUrl;
429
430
431
432 @SuppressWarnings("CanBeFinal")
433 @Parameter(property = "pyDistributionAnalyzerEnabled")
434 private Boolean pyDistributionAnalyzerEnabled;
435
436
437
438 @Parameter(property = "pyPackageAnalyzerEnabled")
439 private Boolean pyPackageAnalyzerEnabled;
440
441
442
443 @SuppressWarnings("CanBeFinal")
444 @Parameter(property = "rubygemsAnalyzerEnabled")
445 private Boolean rubygemsAnalyzerEnabled;
446
447
448
449 @SuppressWarnings("CanBeFinal")
450 @Parameter(property = "opensslAnalyzerEnabled")
451 private Boolean opensslAnalyzerEnabled;
452
453
454
455 @SuppressWarnings("CanBeFinal")
456 @Parameter(property = "cmakeAnalyzerEnabled")
457 private Boolean cmakeAnalyzerEnabled;
458
459
460
461 @SuppressWarnings("CanBeFinal")
462 @Parameter(property = "autoconfAnalyzerEnabled")
463 private Boolean autoconfAnalyzerEnabled;
464
465
466
467 @SuppressWarnings("CanBeFinal")
468 @Parameter(property = "mavenInstallAnalyzerEnabled")
469 private Boolean mavenInstallAnalyzerEnabled;
470
471
472
473 @SuppressWarnings("CanBeFinal")
474 @Parameter(property = "pipAnalyzerEnabled")
475 private Boolean pipAnalyzerEnabled;
476
477
478
479 @SuppressWarnings("CanBeFinal")
480 @Parameter(property = "pipfileAnalyzerEnabled")
481 private Boolean pipfileAnalyzerEnabled;
482
483
484
485 @SuppressWarnings("CanBeFinal")
486 @Parameter(property = "poetryAnalyzerEnabled")
487 private Boolean poetryAnalyzerEnabled;
488
489
490
491 @Parameter(property = "composerAnalyzerEnabled")
492 private Boolean composerAnalyzerEnabled;
493
494
495
496 @Parameter(property = "composerAnalyzerSkipDev")
497 private boolean composerAnalyzerSkipDev;
498
499
500
501 @Parameter(property = "cpanfileAnalyzerEnabled")
502 private Boolean cpanfileAnalyzerEnabled;
503
504
505
506 @SuppressWarnings("CanBeFinal")
507 @Parameter(property = "nodeAnalyzerEnabled")
508 private Boolean nodeAnalyzerEnabled;
509
510
511
512 @SuppressWarnings("CanBeFinal")
513 @Parameter(property = "nodeAuditAnalyzerEnabled")
514 private Boolean nodeAuditAnalyzerEnabled;
515
516
517
518
519 @SuppressWarnings("CanBeFinal")
520 @Parameter(property = "nodeAuditAnalyzerUrl")
521 private String nodeAuditAnalyzerUrl;
522
523
524
525
526 @SuppressWarnings("CanBeFinal")
527 @Parameter(property = "yarnAuditAnalyzerEnabled")
528 private Boolean yarnAuditAnalyzerEnabled;
529
530
531
532
533 @SuppressWarnings("CanBeFinal")
534 @Parameter(property = "pnpmAuditAnalyzerEnabled")
535 private Boolean pnpmAuditAnalyzerEnabled;
536
537
538
539
540 @SuppressWarnings("CanBeFinal")
541 @Parameter(property = "nodeAuditAnalyzerUseCache")
542 private Boolean nodeAuditAnalyzerUseCache;
543
544
545
546 @SuppressWarnings("CanBeFinal")
547 @Parameter(property = "nodeAuditSkipDevDependencies")
548 private Boolean nodeAuditSkipDevDependencies;
549
550
551
552 @SuppressWarnings("CanBeFinal")
553 @Parameter(property = "nodePackageSkipDevDependencies")
554 private Boolean nodePackageSkipDevDependencies;
555
556
557
558 @SuppressWarnings("CanBeFinal")
559 @Parameter(property = "retireJsAnalyzerEnabled")
560 private Boolean retireJsAnalyzerEnabled;
561
562
563
564 @SuppressWarnings("CanBeFinal")
565 @Parameter(property = "retireJsUrl")
566 private String retireJsUrl;
567
568
569
570 @Parameter(property = "retireJsUser")
571 private String retireJsUser;
572
573
574
575 @Parameter(property = "retireJsPassword")
576 private String retireJsPassword;
577
578
579
580
581 @SuppressWarnings("CanBeFinal")
582 @Parameter(property = "retireJsUrlServerId")
583 private String retireJsUrlServerId;
584
585
586
587
588 @SuppressWarnings("CanBeFinal")
589 @Parameter(property = "retireJsForceUpdate")
590 private Boolean retireJsForceUpdate;
591
592
593
594 @Parameter(property = "assemblyAnalyzerEnabled")
595 private Boolean assemblyAnalyzerEnabled;
596
597
598
599 @Parameter(property = "msbuildAnalyzerEnabled")
600 private Boolean msbuildAnalyzerEnabled;
601
602
603
604 @SuppressWarnings("CanBeFinal")
605 @Parameter(property = "nuspecAnalyzerEnabled")
606 private Boolean nuspecAnalyzerEnabled;
607
608
609
610
611 @SuppressWarnings("CanBeFinal")
612 @Parameter(property = "nugetconfAnalyzerEnabled")
613 private Boolean nugetconfAnalyzerEnabled;
614
615
616
617
618 @SuppressWarnings("CanBeFinal")
619 @Parameter(property = "libmanAnalyzerEnabled")
620 private Boolean libmanAnalyzerEnabled;
621
622
623
624
625 @SuppressWarnings("CanBeFinal")
626 @Parameter(property = "centralAnalyzerEnabled")
627 private Boolean centralAnalyzerEnabled;
628
629
630
631
632 @SuppressWarnings("CanBeFinal")
633 @Parameter(property = "centralAnalyzerUseCache")
634 private Boolean centralAnalyzerUseCache;
635
636
637
638
639 @SuppressWarnings("CanBeFinal")
640 @Parameter(property = "artifactoryAnalyzerEnabled")
641 private Boolean artifactoryAnalyzerEnabled;
642
643
644
645
646 @SuppressWarnings("CanBeFinal")
647 @Parameter(property = "artifactoryAnalyzerServerId")
648 private String artifactoryAnalyzerServerId;
649
650
651
652
653 @SuppressWarnings("CanBeFinal")
654 @Parameter(property = "artifactoryAnalyzerUsername")
655 private String artifactoryAnalyzerUsername;
656
657
658
659 @SuppressWarnings("CanBeFinal")
660 @Parameter(property = "artifactoryAnalyzerApiToken")
661 private String artifactoryAnalyzerApiToken;
662
663
664
665 @SuppressWarnings("CanBeFinal")
666 @Parameter(property = "artifactoryAnalyzerBearerToken")
667 private String artifactoryAnalyzerBearerToken;
668
669
670
671 @SuppressWarnings("CanBeFinal")
672 @Parameter(property = "artifactoryAnalyzerUrl")
673 private String artifactoryAnalyzerUrl;
674
675
676
677 @SuppressWarnings("CanBeFinal")
678 @Parameter(property = "artifactoryAnalyzerUseProxy")
679 private Boolean artifactoryAnalyzerUseProxy;
680
681
682
683 @SuppressWarnings("CanBeFinal")
684 @Parameter(property = "artifactoryAnalyzerParallelAnalysis", defaultValue = "true")
685 private Boolean artifactoryAnalyzerParallelAnalysis;
686
687
688
689 @SuppressWarnings("CanBeFinal")
690 @Parameter(property = "nexusAnalyzerEnabled")
691 private Boolean nexusAnalyzerEnabled;
692
693
694
695
696 @SuppressWarnings("CanBeFinal")
697 @Parameter(property = "ossindexAnalyzerEnabled")
698 private Boolean ossindexAnalyzerEnabled;
699
700
701
702 @SuppressWarnings("CanBeFinal")
703 @Parameter(property = "ossindexAnalyzerUseCache")
704 private Boolean ossindexAnalyzerUseCache;
705
706
707
708 @SuppressWarnings("CanBeFinal")
709 @Parameter(property = "ossindexAnalyzerUrl")
710 private String ossindexAnalyzerUrl;
711
712
713
714
715
716 @SuppressWarnings("CanBeFinal")
717 @Parameter(property = "ossIndexServerId")
718 private String ossIndexServerId;
719
720
721
722
723
724 @SuppressWarnings("CanBeFinal")
725 @Parameter(property = "ossIndexWarnOnlyOnRemoteErrors")
726 private Boolean ossIndexWarnOnlyOnRemoteErrors;
727
728
729
730
731 @Parameter(property = "mixAuditAnalyzerEnabled")
732 private Boolean mixAuditAnalyzerEnabled;
733
734
735
736
737 @SuppressWarnings("CanBeFinal")
738 @Parameter(property = "mixAuditPath")
739 private String mixAuditPath;
740
741
742
743
744 @Parameter(property = "bundleAuditAnalyzerEnabled")
745 private Boolean bundleAuditAnalyzerEnabled;
746
747
748
749
750 @SuppressWarnings("CanBeFinal")
751 @Parameter(property = "bundleAuditPath")
752 private String bundleAuditPath;
753
754
755
756
757
758 @SuppressWarnings("CanBeFinal")
759 @Parameter(property = "bundleAuditWorkingDirectory")
760 private String bundleAuditWorkingDirectory;
761
762
763
764
765 @SuppressWarnings("CanBeFinal")
766 @Parameter(property = "cocoapodsAnalyzerEnabled")
767 private Boolean cocoapodsAnalyzerEnabled;
768
769
770
771
772 @SuppressWarnings("CanBeFinal")
773 @Parameter(property = "carthageAnalyzerEnabled")
774 private Boolean carthageAnalyzerEnabled;
775
776
777
778
779 @SuppressWarnings("CanBeFinal")
780 @Parameter(property = "swiftPackageManagerAnalyzerEnabled")
781 private Boolean swiftPackageManagerAnalyzerEnabled;
782
783
784
785 @SuppressWarnings("CanBeFinal")
786 @Parameter(property = "swiftPackageResolvedAnalyzerEnabled")
787 private Boolean swiftPackageResolvedAnalyzerEnabled;
788
789
790
791
792 @SuppressWarnings("CanBeFinal")
793 @Parameter(property = "nexusUrl")
794 private String nexusUrl;
795
796
797
798
799
800
801 @SuppressWarnings("CanBeFinal")
802 @Parameter(property = "nexusServerId")
803 private String nexusServerId;
804
805
806
807 @SuppressWarnings("CanBeFinal")
808 @Parameter(property = "nexusUsesProxy")
809 private Boolean nexusUsesProxy;
810
811
812
813 @SuppressWarnings("CanBeFinal")
814 @Parameter(property = "connectionString")
815 private String connectionString;
816
817
818
819
820 @SuppressWarnings("CanBeFinal")
821 @Parameter(property = "databaseDriverName")
822 private String databaseDriverName;
823
824
825
826 @SuppressWarnings("CanBeFinal")
827 @Parameter(property = "databaseDriverPath")
828 private String databaseDriverPath;
829
830
831
832 @SuppressWarnings("CanBeFinal")
833 @Parameter(defaultValue = "${settings}", readonly = true, required = true)
834 private org.apache.maven.settings.Settings settingsXml;
835
836
837
838 @Component(role = SecDispatcher.class, hint = "default")
839 private SecDispatcher securityDispatcher;
840
841
842
843 @Parameter(property = "databaseUser")
844 private String databaseUser;
845
846
847
848 @Parameter(property = "databasePassword")
849 private String databasePassword;
850
851
852
853
854 @SuppressWarnings("CanBeFinal")
855 @Parameter(property = "zipExtensions")
856 private String zipExtensions;
857
858
859
860 @SuppressWarnings("CanBeFinal")
861 @Parameter(property = "dependency-check.skip", defaultValue = "false")
862 private boolean skip = false;
863
864
865
866 @SuppressWarnings("CanBeFinal")
867 @Parameter(property = "skipTestScope", defaultValue = "true")
868 private boolean skipTestScope = true;
869
870
871
872 @SuppressWarnings("CanBeFinal")
873 @Parameter(property = "skipRuntimeScope", defaultValue = "false")
874 private boolean skipRuntimeScope = false;
875
876
877
878 @SuppressWarnings("CanBeFinal")
879 @Parameter(property = "skipProvidedScope", defaultValue = "false")
880 private boolean skipProvidedScope = false;
881
882
883
884
885 @SuppressWarnings("CanBeFinal")
886 @Parameter(property = "skipSystemScope", defaultValue = "false")
887 private boolean skipSystemScope = false;
888
889
890
891
892 @SuppressWarnings("CanBeFinal")
893 @Parameter(property = "skipDependencyManagement", defaultValue = "true")
894 private boolean skipDependencyManagement = true;
895
896
897
898
899
900
901 @SuppressWarnings("CanBeFinal")
902 @Parameter(property = "skipArtifactType")
903 private String skipArtifactType;
904
905
906
907
908 @SuppressWarnings("CanBeFinal")
909 @Parameter(property = "dataDirectory")
910 private String dataDirectory;
911
912
913
914
915 @SuppressWarnings("CanBeFinal")
916 @Parameter(property = "dbFilename")
917 private String dbFilename;
918
919
920
921
922
923 @SuppressWarnings("CanBeFinal")
924 @Parameter(property = "serverId")
925 private String serverId;
926
927
928
929
930
931 @SuppressWarnings("CanBeFinal")
932 @Parameter(property = "nvdApiKey")
933 private String nvdApiKey;
934
935
936
937 @SuppressWarnings("CanBeFinal")
938 @Parameter(property = "nvdMaxRetryCount")
939 private Integer nvdMaxRetryCount;
940
941
942
943
944
945
946 @SuppressWarnings("CanBeFinal")
947 @Parameter(property = "nvdApiServerId")
948 private String nvdApiServerId;
949
950
951
952
953
954 @SuppressWarnings("CanBeFinal")
955 @Parameter(property = "nvdApiKeyEnvironmentVariable")
956 private String nvdApiKeyEnvironmentVariable;
957
958
959
960 @SuppressWarnings("CanBeFinal")
961 @Parameter(property = "nvdValidForHours")
962 private Integer nvdValidForHours;
963
964
965
966 @SuppressWarnings("CanBeFinal")
967 @Parameter(property = "nvdApiEndpoint")
968 private String nvdApiEndpoint;
969
970
971
972 @SuppressWarnings("CanBeFinal")
973 @Parameter(property = "nvdDatafeedUrl")
974 private String nvdDatafeedUrl;
975
976
977
978
979
980 @SuppressWarnings("CanBeFinal")
981 @Parameter(property = "nvdDatafeedServerId")
982 private String nvdDatafeedServerId;
983
984
985
986 @SuppressWarnings("CanBeFinal")
987 @Parameter(property = "nvdUser")
988 private String nvdUser;
989
990
991
992 @SuppressWarnings("CanBeFinal")
993 @Parameter(property = "nvdPassword")
994 private String nvdPassword;
995
996
997
998 @SuppressWarnings("CanBeFinal")
999 @Parameter(property = "nvdApiDelay")
1000 private Integer nvdApiDelay;
1001
1002
1003
1004
1005 @SuppressWarnings("CanBeFinal")
1006 @Parameter(property = "nvdApiResultsPerPage")
1007 private Integer nvdApiResultsPerPage;
1008
1009
1010
1011
1012 @SuppressWarnings("CanBeFinal")
1013 @Parameter(property = "pathToCore")
1014 private String pathToCore;
1015
1016
1017
1018 @SuppressWarnings("CanBeFinal")
1019 @Parameter(property = "hostedSuppressionsUrl")
1020 private String hostedSuppressionsUrl;
1021
1022
1023
1024
1025 @SuppressWarnings("CanBeFinal")
1026 @Parameter(property = "hostedSuppressionsForceUpdate")
1027 private Boolean hostedSuppressionsForceUpdate;
1028
1029
1030
1031 @SuppressWarnings("CanBeFinal")
1032 @Parameter(property = "hostedSuppressionsEnabled")
1033 private Boolean hostedSuppressionsEnabled;
1034
1035
1036
1037
1038 @SuppressWarnings("CanBeFinal")
1039 @Parameter(property = "hostedSuppressionsValidForHours")
1040 private Integer hostedSuppressionsValidForHours;
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057 @SuppressWarnings("CanBeFinal")
1058 @Parameter(property = "retirejs")
1059 private Retirejs retirejs;
1060
1061
1062
1063
1064
1065 @Parameter(property = "odc.excludes")
1066 private List<String> excludes;
1067
1068
1069
1070
1071 private Filter<String> artifactScopeExcluded;
1072
1073
1074
1075
1076 private Filter<String> artifactTypeExcluded;
1077
1078
1079
1080
1081
1082
1083
1084
1085 @Parameter
1086 private List<FileSet> scanSet;
1087
1088
1089
1090
1091
1092 @Parameter(property = "scanDirectory")
1093 private List<String> scanDirectory;
1094
1095
1096
1097
1098 @SuppressWarnings("CanBeFinal")
1099 @Parameter(property = "odc.plugins.scan", defaultValue = "false", required = false)
1100 private boolean scanPlugins = false;
1101
1102
1103
1104 @SuppressWarnings("CanBeFinal")
1105 @Parameter(property = "odc.dependencies.scan", defaultValue = "true", required = false)
1106 private boolean scanDependencies = true;
1107
1108
1109
1110 @Parameter
1111 private ProxyConfig proxy;
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123 private static boolean artifactsMatch(org.apache.maven.model.Dependency d, Artifact a) {
1124 return isEqualOrNull(a.getArtifactId(), d.getArtifactId())
1125 && isEqualOrNull(a.getGroupId(), d.getGroupId())
1126 && isEqualOrNull(a.getVersion(), d.getVersion());
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 private static boolean isEqualOrNull(String left, String right) {
1139 return (left != null && left.equals(right)) || (left == null && right == null);
1140 }
1141
1142
1143
1144
1145
1146
1147
1148
1149 @Override
1150 public void execute() throws MojoExecutionException, MojoFailureException {
1151 generatingSite = false;
1152 final boolean shouldSkip = Boolean.parseBoolean(System.getProperty("dependency-check.skip", Boolean.toString(skip)));
1153 if (shouldSkip) {
1154 getLog().info("Skipping " + getName(Locale.US));
1155 } else {
1156 project.setContextValue("dependency-check-output-dir", this.outputDirectory);
1157 runCheck();
1158 }
1159 }
1160
1161
1162
1163
1164
1165
1166 protected boolean isGeneratingSite() {
1167 return generatingSite;
1168 }
1169
1170
1171
1172
1173
1174
1175 protected String getConnectionString() {
1176 return connectionString;
1177 }
1178
1179
1180
1181
1182
1183
1184 protected boolean isFailOnError() {
1185 return failOnError;
1186 }
1187
1188
1189
1190
1191
1192
1193
1194
1195 public void generate(Sink sink, Locale locale) throws MavenReportException {
1196 final boolean shouldSkip = Boolean.parseBoolean(System.getProperty("dependency-check.skip", Boolean.toString(skip)));
1197 if (shouldSkip) {
1198 getLog().info("Skipping report generation " + getName(Locale.US));
1199 return;
1200 }
1201
1202 generatingSite = true;
1203 project.setContextValue("dependency-check-output-dir", getReportOutputDirectory());
1204 try {
1205 runCheck();
1206 } catch (MojoExecutionException ex) {
1207 throw new MavenReportException(ex.getMessage(), ex);
1208 } catch (MojoFailureException ex) {
1209 getLog().warn("Vulnerabilities were identifies that exceed the CVSS threshold for failing the build");
1210 }
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221 protected File getCorrectOutputDirectory() throws MojoExecutionException {
1222 return getCorrectOutputDirectory(this.project);
1223 }
1224
1225
1226
1227
1228
1229
1230
1231
1232 protected File getCorrectOutputDirectory(MavenProject current) {
1233 final Object obj = current.getContextValue("dependency-check-output-dir");
1234 if (obj != null && obj instanceof File) {
1235 return (File) obj;
1236 }
1237
1238 File target = new File(current.getBuild().getDirectory());
1239 if (target.getParentFile() != null && "target".equals(target.getParentFile().getName())) {
1240 target = target.getParentFile();
1241 }
1242 return target;
1243 }
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254 protected ExceptionCollection scanArtifacts(MavenProject project, Engine engine) {
1255 return scanArtifacts(project, engine, false);
1256 }
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 protected ExceptionCollection scanArtifacts(MavenProject project, Engine engine, boolean aggregate) {
1269 try {
1270 final List<String> filterItems = Collections.singletonList(String.format("%s:%s", project.getGroupId(), project.getArtifactId()));
1271 final ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest(project, project.getRemoteArtifactRepositories());
1272
1273
1274 final DependencyNode dn = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, null);
1275
1276 final CollectingRootDependencyGraphVisitor collectorVisitor = new CollectingRootDependencyGraphVisitor();
1277
1278
1279 final DependencyNodeVisitor transitiveFilterVisitor = new FilteringDependencyTransitiveNodeVisitor(collectorVisitor,
1280 new ArtifactDependencyNodeFilter(new PatternExcludesArtifactFilter(getExcludes())));
1281
1282
1283 final DependencyNodeVisitor artifactFilter = new FilteringDependencyNodeVisitor(transitiveFilterVisitor,
1284 new ArtifactDependencyNodeFilter(new ExcludesArtifactFilter(filterItems)));
1285 dn.accept(artifactFilter);
1286
1287
1288 final Map<DependencyNode, List<DependencyNode>> nodes = collectorVisitor.getNodes();
1289
1290 return collectDependencies(engine, project, nodes, buildingRequest, aggregate);
1291 } catch (DependencyGraphBuilderException ex) {
1292 final String msg = String.format("Unable to build dependency graph on project %s", project.getName());
1293 getLog().debug(msg, ex);
1294 return new ExceptionCollection(ex);
1295 }
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309 protected ExceptionCollection scanPlugins(MavenProject project, Engine engine, ExceptionCollection exCollection) {
1310 ExceptionCollection exCol = exCollection;
1311 final Set<Artifact> plugins = new HashSet<>();
1312 final Set<Artifact> buildPlugins = getProject().getPluginArtifacts();
1313 final Set<Artifact> reportPlugins = getProject().getReportArtifacts();
1314 final Set<Artifact> extensions = getProject().getExtensionArtifacts();
1315
1316 plugins.addAll(buildPlugins);
1317 plugins.addAll(reportPlugins);
1318 plugins.addAll(extensions);
1319
1320 final ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest(project, project.getPluginArtifactRepositories());
1321 for (Artifact plugin : plugins) {
1322 try {
1323 final Artifact resolved = artifactResolver.resolveArtifact(buildingRequest, plugin).getArtifact();
1324
1325 exCol = addPluginToDependencies(project, engine, resolved, "pom.xml (plugins)", exCol);
1326
1327 final DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
1328 pluginCoordinate.setGroupId(resolved.getGroupId());
1329 pluginCoordinate.setArtifactId(resolved.getArtifactId());
1330 pluginCoordinate.setVersion(resolved.getVersion());
1331
1332 final String parent = buildReference(resolved.getGroupId(), resolved.getArtifactId(), resolved.getVersion());
1333 for (Artifact artifact : resolveArtifactDependencies(pluginCoordinate, project)) {
1334 exCol = addPluginToDependencies(project, engine, artifact, parent, exCol);
1335 }
1336 } catch (ArtifactResolverException ex) {
1337 throw new RuntimeException(ex);
1338 } catch (IllegalArgumentException ex) {
1339 throw new RuntimeException(ex);
1340 } catch (DependencyResolverException ex) {
1341 throw new RuntimeException(ex);
1342 }
1343 }
1344
1345 return null;
1346
1347 }
1348
1349 private ExceptionCollection addPluginToDependencies(MavenProject project, Engine engine, Artifact artifact, String parent, ExceptionCollection exCollection) {
1350 ExceptionCollection exCol = exCollection;
1351 final String groupId = artifact.getGroupId();
1352 final String artifactId = artifact.getArtifactId();
1353 final String version = artifact.getVersion();
1354 final File artifactFile = artifact.getFile();
1355 if (artifactFile.isFile()) {
1356 final List<ArtifactVersion> availableVersions = artifact.getAvailableVersions();
1357
1358 final List<Dependency> deps = engine.scan(artifactFile.getAbsoluteFile(),
1359 project.getName() + " (plugins)");
1360 if (deps != null) {
1361 Dependency d = null;
1362 if (deps.size() == 1) {
1363 d = deps.get(0);
1364 } else {
1365 for (Dependency possible : deps) {
1366 if (artifactFile.getAbsoluteFile().equals(possible.getActualFile())) {
1367 d = possible;
1368 break;
1369 }
1370 }
1371 for (Dependency dep : deps) {
1372 if (d != null && d != dep) {
1373 final String includedBy = buildReference(groupId, artifactId, version);
1374 dep.addIncludedBy(includedBy, "plugins");
1375 }
1376 }
1377 }
1378 if (d != null) {
1379 final MavenArtifact ma = new MavenArtifact(groupId, artifactId, version);
1380 d.addAsEvidence("pom", ma, Confidence.HIGHEST);
1381 if (parent != null) {
1382 d.addIncludedBy(parent, "plugins");
1383 } else {
1384 final String includedby = buildReference(
1385 project.getGroupId(),
1386 project.getArtifactId(),
1387 project.getVersion());
1388 d.addIncludedBy(includedby, "plugins");
1389 }
1390 if (availableVersions != null) {
1391 for (ArtifactVersion av : availableVersions) {
1392 d.addAvailableVersion(av.toString());
1393 }
1394 }
1395 }
1396 }
1397 } else {
1398 if (exCol == null) {
1399 exCol = new ExceptionCollection();
1400 }
1401 exCol.addException(new DependencyNotFoundException("Unable to resolve plugin: "
1402 + groupId + ":" + artifactId + ":" + version));
1403 }
1404
1405 return exCol;
1406 }
1407
1408 private String buildReference(final String groupId, final String artifactId, final String version) {
1409 String includedBy;
1410 try {
1411 final PackageURL purl = new PackageURL("maven", groupId, artifactId, version, null, null);
1412 includedBy = purl.toString();
1413 } catch (MalformedPackageURLException ex) {
1414 getLog().warn("Unable to generate build reference for " + groupId
1415 + ":" + artifactId + ":" + version, ex);
1416 includedBy = groupId + ":" + artifactId + ":" + version;
1417 }
1418 return includedBy;
1419 }
1420
1421 protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact, MavenProject project)
1422 throws DependencyResolverException {
1423 final ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest(project, project.getRemoteArtifactRepositories());
1424
1425 final Iterable<ArtifactResult> artifactResults = dependencyResolver.resolveDependencies(buildingRequest, artifact, null);
1426
1427 final Set<Artifact> artifacts = new HashSet<>();
1428
1429 for (ArtifactResult artifactResult : artifactResults) {
1430 artifacts.add(artifactResult.getArtifact());
1431 }
1432
1433 return artifacts;
1434
1435 }
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448 private DependencyNode toDependencyNode(List<DependencyNode> nodes, ProjectBuildingRequest buildingRequest,
1449 DependencyNode parent, org.apache.maven.model.Dependency dependency) throws ArtifactResolverException {
1450
1451 final DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
1452
1453 coordinate.setGroupId(dependency.getGroupId());
1454 coordinate.setArtifactId(dependency.getArtifactId());
1455 String version = null;
1456 final VersionRange vr;
1457 try {
1458 vr = VersionRange.createFromVersionSpec(dependency.getVersion());
1459 } catch (InvalidVersionSpecificationException ex) {
1460 throw new ArtifactResolverException("Invalid version specification: "
1461 + dependency.getGroupId() + ":"
1462 + dependency.getArtifactId() + ":"
1463 + dependency.getVersion(), ex);
1464 }
1465 if (vr.hasRestrictions()) {
1466 version = findVersion(nodes, dependency.getGroupId(), dependency.getArtifactId());
1467 if (version == null) {
1468
1469
1470 if (vr.getRecommendedVersion() != null) {
1471 version = vr.getRecommendedVersion().toString();
1472 } else if (vr.hasRestrictions()) {
1473 for (Restriction restriction : vr.getRestrictions()) {
1474 if (restriction.getLowerBound() != null) {
1475 version = restriction.getLowerBound().toString();
1476 }
1477 if (restriction.getUpperBound() != null) {
1478 version = restriction.getUpperBound().toString();
1479 }
1480 }
1481 } else {
1482 version = vr.toString();
1483 }
1484 }
1485 }
1486 if (version == null) {
1487 version = dependency.getVersion();
1488 }
1489 coordinate.setVersion(version);
1490
1491 final ArtifactType type = session.getRepositorySession().getArtifactTypeRegistry().get(dependency.getType());
1492 coordinate.setExtension(type.getExtension());
1493 coordinate.setClassifier((null == dependency.getClassifier() || dependency.getClassifier().isEmpty())
1494 ? type.getClassifier() : dependency.getClassifier());
1495 final Artifact artifact = artifactResolver.resolveArtifact(buildingRequest, coordinate).getArtifact();
1496 artifact.setScope(dependency.getScope());
1497 return new DefaultDependencyNode(parent, artifact, dependency.getVersion(), dependency.getScope(), null);
1498 }
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510 private String findVersion(List<DependencyNode> nodes, String groupId, String artifactId) {
1511 final Optional<DependencyNode> f = nodes.stream().filter(p
1512 -> groupId.equals(p.getArtifact().getGroupId())
1513 && artifactId.equals(p.getArtifact().getArtifactId())).findFirst();
1514 if (f.isPresent()) {
1515 return f.get().getArtifact().getVersion();
1516 }
1517 return null;
1518 }
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531 private ExceptionCollection collectDependencyManagementDependencies(Engine engine, ProjectBuildingRequest buildingRequest,
1532 MavenProject project, List<DependencyNode> nodes, boolean aggregate) {
1533 if (skipDependencyManagement || project.getDependencyManagement() == null) {
1534 return null;
1535 }
1536
1537 ExceptionCollection exCol = null;
1538 for (org.apache.maven.model.Dependency dependency : project.getDependencyManagement().getDependencies()) {
1539 try {
1540 nodes.add(toDependencyNode(nodes, buildingRequest, null, dependency));
1541 } catch (ArtifactResolverException ex) {
1542 getLog().debug(String.format("Aggregate : %s", aggregate));
1543 boolean addException = true;
1544
1545 if (!aggregate) {
1546
1547 } else if (addReactorDependency(engine,
1548 new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(),
1549 dependency.getVersion(), dependency.getScope(), dependency.getType(), dependency.getClassifier(),
1550 new DefaultArtifactHandler()), project)) {
1551 addException = false;
1552 }
1553
1554 if (addException) {
1555 if (exCol == null) {
1556 exCol = new ExceptionCollection();
1557 }
1558 exCol.addException(ex);
1559 }
1560 }
1561 }
1562 return exCol;
1563 }
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579 private ExceptionCollection collectMavenDependencies(Engine engine, MavenProject project,
1580 Map<DependencyNode, List<DependencyNode>> nodeMap, ProjectBuildingRequest buildingRequest, boolean aggregate) {
1581
1582 final List<ArtifactResult> allResolvedDeps = new ArrayList<>();
1583
1584
1585 final List<DependencyNode> dmNodes = new ArrayList<>();
1586 ExceptionCollection exCol = collectDependencyManagementDependencies(engine, buildingRequest, project, dmNodes, aggregate);
1587 for (DependencyNode dependencyNode : dmNodes) {
1588 exCol = scanDependencyNode(dependencyNode, null, engine, project, allResolvedDeps, buildingRequest, aggregate, exCol);
1589 }
1590
1591
1592 for (Map.Entry<DependencyNode, List<DependencyNode>> entry : nodeMap.entrySet()) {
1593 exCol = scanDependencyNode(entry.getKey(), null, engine, project, allResolvedDeps, buildingRequest, aggregate, exCol);
1594 for (DependencyNode dependencyNode : entry.getValue()) {
1595 exCol = scanDependencyNode(dependencyNode, entry.getKey(), engine, project, allResolvedDeps, buildingRequest, aggregate, exCol);
1596 }
1597 }
1598 return exCol;
1599 }
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613 private Artifact findInAllDeps(final List<ArtifactResult> allDeps, final Artifact unresolvedArtifact,
1614 final MavenProject project)
1615 throws DependencyNotFoundException {
1616 Artifact result = null;
1617 for (final ArtifactResult res : allDeps) {
1618 if (sameArtifact(res, unresolvedArtifact)) {
1619 result = res.getArtifact();
1620 break;
1621 }
1622 }
1623 if (result == null) {
1624 throw new DependencyNotFoundException(String.format("Expected dependency not found in resolved artifacts for "
1625 + "dependency %s of project-artifact %s", unresolvedArtifact, project.getArtifactId()));
1626 }
1627 return result;
1628 }
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639 private boolean sameArtifact(final ArtifactResult res, final Artifact unresolvedArtifact) {
1640 if (res == null || res.getArtifact() == null || unresolvedArtifact == null) {
1641 return false;
1642 }
1643 boolean result = Objects.equals(res.getArtifact().getGroupId(), unresolvedArtifact.getGroupId());
1644 result &= Objects.equals(res.getArtifact().getArtifactId(), unresolvedArtifact.getArtifactId());
1645
1646 if ("RELEASE".equals(unresolvedArtifact.getBaseVersion())) {
1647 result &= !res.getArtifact().isSnapshot();
1648 } else if (!"LATEST".equals(unresolvedArtifact.getBaseVersion())) {
1649 result &= Objects.equals(res.getArtifact().getBaseVersion(), unresolvedArtifact.getBaseVersion());
1650 }
1651 result &= Objects.equals(res.getArtifact().getClassifier(), unresolvedArtifact.getClassifier());
1652 result &= Objects.equals(res.getArtifact().getType(), unresolvedArtifact.getType());
1653 return result;
1654 }
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665 protected String createProjectReferenceName(MavenProject project, DependencyNode dependencyNode) {
1666 return project.getName() + ":" + dependencyNode.getArtifact().getScope();
1667 }
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682 private ExceptionCollection collectDependencies(Engine engine, MavenProject project,
1683 Map<DependencyNode, List<DependencyNode>> nodes, ProjectBuildingRequest buildingRequest, boolean aggregate) {
1684
1685 ExceptionCollection exCol;
1686 exCol = collectMavenDependencies(engine, project, nodes, buildingRequest, aggregate);
1687
1688 final List<FileSet> projectScan;
1689
1690 if (scanDirectory != null && !scanDirectory.isEmpty()) {
1691 if (scanSet == null) {
1692 scanSet = new ArrayList<>();
1693 }
1694 scanDirectory.forEach(d -> {
1695 final FileSet fs = new FileSet();
1696 fs.setDirectory(d);
1697 fs.addInclude(INCLUDE_ALL);
1698 scanSet.add(fs);
1699 });
1700 }
1701
1702 if (scanSet == null || scanSet.isEmpty()) {
1703
1704 final FileSet resourcesSet = new FileSet();
1705 final FileSet filtersSet = new FileSet();
1706 final FileSet webappSet = new FileSet();
1707 final FileSet mixedLangSet = new FileSet();
1708 try {
1709 resourcesSet.setDirectory(new File(project.getBasedir(), "src/main/resources").getCanonicalPath());
1710 resourcesSet.addInclude(INCLUDE_ALL);
1711 filtersSet.setDirectory(new File(project.getBasedir(), "src/main/filters").getCanonicalPath());
1712 filtersSet.addInclude(INCLUDE_ALL);
1713 webappSet.setDirectory(new File(project.getBasedir(), "src/main/webapp").getCanonicalPath());
1714 webappSet.addInclude(INCLUDE_ALL);
1715 mixedLangSet.setDirectory(project.getBasedir().getCanonicalPath());
1716 mixedLangSet.addInclude("package.json");
1717 mixedLangSet.addInclude("package-lock.json");
1718 mixedLangSet.addInclude("npm-shrinkwrap.json");
1719 mixedLangSet.addInclude("Gopkg.lock");
1720 mixedLangSet.addInclude("go.mod");
1721 mixedLangSet.addInclude("yarn.lock");
1722 mixedLangSet.addInclude("pnpm-lock.yaml");
1723 mixedLangSet.addExclude("/node_modules/");
1724 } catch (IOException ex) {
1725 if (exCol == null) {
1726 exCol = new ExceptionCollection();
1727 }
1728 exCol.addException(ex);
1729 }
1730 projectScan = new ArrayList<>();
1731 projectScan.add(resourcesSet);
1732 projectScan.add(filtersSet);
1733 projectScan.add(webappSet);
1734 projectScan.add(mixedLangSet);
1735
1736 } else if (aggregate) {
1737 projectScan = new ArrayList<>();
1738 for (FileSet copyFrom : scanSet) {
1739
1740 final FileSet fsCopy = new FileSet();
1741 final File f = new File(copyFrom.getDirectory());
1742 if (f.isAbsolute()) {
1743 fsCopy.setDirectory(copyFrom.getDirectory());
1744 } else {
1745 try {
1746 fsCopy.setDirectory(new File(project.getBasedir(), copyFrom.getDirectory()).getCanonicalPath());
1747 } catch (IOException ex) {
1748 if (exCol == null) {
1749 exCol = new ExceptionCollection();
1750 }
1751 exCol.addException(ex);
1752 fsCopy.setDirectory(copyFrom.getDirectory());
1753 }
1754 }
1755 fsCopy.setDirectoryMode(copyFrom.getDirectoryMode());
1756 fsCopy.setExcludes(copyFrom.getExcludes());
1757 fsCopy.setFileMode(copyFrom.getFileMode());
1758 fsCopy.setFollowSymlinks(copyFrom.isFollowSymlinks());
1759 fsCopy.setIncludes(copyFrom.getIncludes());
1760 fsCopy.setLineEnding(copyFrom.getLineEnding());
1761 fsCopy.setMapper(copyFrom.getMapper());
1762 fsCopy.setModelEncoding(copyFrom.getModelEncoding());
1763 fsCopy.setOutputDirectory(copyFrom.getOutputDirectory());
1764 fsCopy.setUseDefaultExcludes(copyFrom.isUseDefaultExcludes());
1765 projectScan.add(fsCopy);
1766 }
1767 } else {
1768 projectScan = scanSet;
1769 }
1770
1771
1772 final FileSetManager fileSetManager = new FileSetManager();
1773 for (FileSet fileSet : projectScan) {
1774 getLog().debug("Scanning fileSet: " + fileSet.getDirectory());
1775 final String[] includedFiles = fileSetManager.getIncludedFiles(fileSet);
1776 for (String include : includedFiles) {
1777 final File includeFile = new File(fileSet.getDirectory(), include).getAbsoluteFile();
1778 if (includeFile.exists()) {
1779 engine.scan(includeFile, project.getName());
1780 }
1781 }
1782 }
1783 return exCol;
1784 }
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797 private boolean addReactorDependency(Engine engine, Artifact artifact, final MavenProject depender) {
1798 return addVirtualDependencyFromReactor(engine, artifact, depender, "Unable to resolve %s as it has not been built yet "
1799 + "- creating a virtual dependency instead.");
1800 }
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816 private boolean addVirtualDependencyFromReactor(Engine engine, Artifact artifact,
1817 final MavenProject depender, String infoLogTemplate) {
1818
1819 getLog().debug(String.format("Checking the reactor projects (%d) for %s:%s:%s",
1820 reactorProjects.size(),
1821 artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()));
1822
1823 for (MavenProject prj : reactorProjects) {
1824
1825 getLog().debug(String.format("Comparing %s:%s:%s to %s:%s:%s",
1826 artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(),
1827 prj.getGroupId(), prj.getArtifactId(), prj.getVersion()));
1828
1829 if (prj.getArtifactId().equals(artifact.getArtifactId())
1830 && prj.getGroupId().equals(artifact.getGroupId())
1831 && prj.getVersion().equals(artifact.getBaseVersion())) {
1832
1833 final String displayName = String.format("%s:%s:%s",
1834 prj.getGroupId(), prj.getArtifactId(), prj.getVersion());
1835 getLog().info(String.format(infoLogTemplate,
1836 displayName));
1837 final Dependency d = newDependency(prj);
1838 final String key = String.format("%s:%s:%s", prj.getGroupId(), prj.getArtifactId(), prj.getVersion());
1839 d.setSha1sum(Checksum.getSHA1Checksum(key));
1840 d.setSha256sum(Checksum.getSHA256Checksum(key));
1841 d.setMd5sum(Checksum.getMD5Checksum(key));
1842 d.setEcosystem(JarAnalyzer.DEPENDENCY_ECOSYSTEM);
1843 d.setDisplayFileName(displayName);
1844 d.addProjectReference(depender.getName());
1845 final String includedby = buildReference(
1846 depender.getGroupId(),
1847 depender.getArtifactId(),
1848 depender.getVersion());
1849 d.addIncludedBy(includedby);
1850 d.addEvidence(EvidenceType.PRODUCT, "project", "artifactid", prj.getArtifactId(), Confidence.HIGHEST);
1851 d.addEvidence(EvidenceType.VENDOR, "project", "artifactid", prj.getArtifactId(), Confidence.LOW);
1852
1853 d.addEvidence(EvidenceType.VENDOR, "project", "groupid", prj.getGroupId(), Confidence.HIGHEST);
1854 d.addEvidence(EvidenceType.PRODUCT, "project", "groupid", prj.getGroupId(), Confidence.LOW);
1855 d.setEcosystem(JarAnalyzer.DEPENDENCY_ECOSYSTEM);
1856 Identifier id;
1857 try {
1858 id = new PurlIdentifier(StandardTypes.MAVEN, artifact.getGroupId(),
1859 artifact.getArtifactId(), artifact.getVersion(), Confidence.HIGHEST);
1860 } catch (MalformedPackageURLException ex) {
1861 getLog().debug("Unable to create PackageURL object:" + key);
1862 id = new GenericIdentifier("maven:" + key, Confidence.HIGHEST);
1863 }
1864 d.addSoftwareIdentifier(id);
1865
1866 d.setName(String.format("%s:%s", prj.getGroupId(), prj.getArtifactId()));
1867 d.setVersion(prj.getVersion());
1868 d.setPackagePath(displayName);
1869 if (prj.getDescription() != null) {
1870 JarAnalyzer.addDescription(d, prj.getDescription(), "project", "description");
1871 }
1872 for (License l : prj.getLicenses()) {
1873 final StringBuilder license = new StringBuilder();
1874 if (l.getName() != null) {
1875 license.append(l.getName());
1876 }
1877 if (l.getUrl() != null) {
1878 license.append(" ").append(l.getUrl());
1879 }
1880 if (d.getLicense() == null) {
1881 d.setLicense(license.toString());
1882 } else if (!d.getLicense().contains(license)) {
1883 d.setLicense(String.format("%s%n%s", d.getLicense(), license));
1884 }
1885 }
1886 engine.addDependency(d);
1887 return true;
1888 }
1889 }
1890 return false;
1891 }
1892
1893 Dependency newDependency(MavenProject prj) {
1894 final File pom = new File(prj.getBasedir(), "pom.xml");
1895
1896 if (pom.isFile()) {
1897 getLog().debug("Adding virtual dependency from pom.xml");
1898 return new Dependency(pom, true);
1899 } else if (prj.getFile().isFile()) {
1900 getLog().debug("Adding virtual dependency from file");
1901 return new Dependency(prj.getFile(), true);
1902 } else {
1903 return new Dependency(true);
1904 }
1905 }
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918 private boolean addSnapshotReactorDependency(Engine engine, Artifact artifact, final MavenProject depender) {
1919 if (!artifact.isSnapshot()) {
1920 return false;
1921 }
1922 return addVirtualDependencyFromReactor(engine, artifact, depender, "Found snapshot reactor project in aggregate for %s - "
1923 + "creating a virtual dependency as the snapshot found in the repository may contain outdated dependencies.");
1924 }
1925
1926
1927
1928
1929
1930
1931
1932
1933 public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest(MavenProject project, List<ArtifactRepository> repos) {
1934 final ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
1935 buildingRequest.setRemoteRepositories(repos);
1936 buildingRequest.setProject(project);
1937 return buildingRequest;
1938 }
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948 protected void runCheck() throws MojoExecutionException, MojoFailureException {
1949 muteNoisyLoggers();
1950 try (Engine engine = initializeEngine()) {
1951 ExceptionCollection exCol = null;
1952 if (scanDependencies) {
1953 exCol = scanDependencies(engine);
1954 }
1955 if (scanPlugins) {
1956 exCol = scanPlugins(engine, exCol);
1957 }
1958 try {
1959 engine.analyzeDependencies();
1960 } catch (ExceptionCollection ex) {
1961 exCol = handleAnalysisExceptions(exCol, ex);
1962 }
1963 if (exCol == null || !exCol.isFatal()) {
1964
1965 File outputDir = getCorrectOutputDirectory(this.getProject());
1966 if (outputDir == null) {
1967
1968
1969 outputDir = new File(this.getProject().getBuild().getDirectory());
1970 }
1971 try {
1972 final MavenProject p = this.getProject();
1973 for (String f : getFormats()) {
1974 engine.writeReports(p.getName(), p.getGroupId(), p.getArtifactId(), p.getVersion(), outputDir, f, exCol);
1975 }
1976 } catch (ReportException ex) {
1977 if (exCol == null) {
1978 exCol = new ExceptionCollection(ex);
1979 } else {
1980 exCol.addException(ex);
1981 }
1982 if (this.isFailOnError()) {
1983 throw new MojoExecutionException("One or more exceptions occurred during dependency-check analysis", exCol);
1984 } else {
1985 getLog().debug("Error writing the report", ex);
1986 }
1987 }
1988 showSummary(this.getProject(), engine.getDependencies());
1989 checkForFailure(engine.getDependencies());
1990 if (exCol != null && this.isFailOnError()) {
1991 throw new MojoExecutionException("One or more exceptions occurred during dependency-check analysis", exCol);
1992 }
1993 }
1994 } catch (DatabaseException ex) {
1995 if (getLog().isDebugEnabled()) {
1996 getLog().debug("Database connection error", ex);
1997 }
1998 final String msg = "An exception occurred connecting to the local database. Please see the log file for more details.";
1999 if (this.isFailOnError()) {
2000 throw new MojoExecutionException(msg, ex);
2001 }
2002 getLog().error(msg, ex);
2003 } finally {
2004 getSettings().cleanup();
2005 }
2006 }
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018 private ExceptionCollection handleAnalysisExceptions(ExceptionCollection currentEx, ExceptionCollection newEx) throws MojoExecutionException {
2019 ExceptionCollection returnEx = currentEx;
2020 if (returnEx == null) {
2021 returnEx = newEx;
2022 } else {
2023 returnEx.getExceptions().addAll(newEx.getExceptions());
2024 if (newEx.isFatal()) {
2025 returnEx.setFatal(true);
2026 }
2027 }
2028 if (returnEx.isFatal()) {
2029 final String msg = String.format("Fatal exception(s) analyzing %s", getProject().getName());
2030 if (this.isFailOnError()) {
2031 throw new MojoExecutionException(msg, returnEx);
2032 }
2033 getLog().error(msg);
2034 if (getLog().isDebugEnabled()) {
2035 getLog().debug(returnEx);
2036 }
2037 } else {
2038 final String msg = String.format("Exception(s) analyzing %s", getProject().getName());
2039 if (getLog().isDebugEnabled()) {
2040 getLog().debug(msg, returnEx);
2041 }
2042 }
2043 return returnEx;
2044 }
2045
2046
2047
2048
2049
2050
2051
2052
2053 protected abstract ExceptionCollection scanDependencies(Engine engine) throws MojoExecutionException;
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064 protected abstract ExceptionCollection scanPlugins(Engine engine, ExceptionCollection exCol) throws MojoExecutionException;
2065
2066
2067
2068
2069
2070
2071 @Override
2072 public File getReportOutputDirectory() {
2073 return reportOutputDirectory;
2074 }
2075
2076
2077
2078
2079
2080
2081 @Override
2082 public void setReportOutputDirectory(File directory) {
2083 reportOutputDirectory = directory;
2084 }
2085
2086
2087
2088
2089
2090
2091 public File getOutputDirectory() {
2092 return outputDirectory;
2093 }
2094
2095
2096
2097
2098
2099
2100
2101 @Override
2102 public final boolean isExternalReport() {
2103 return true;
2104 }
2105
2106
2107
2108
2109
2110
2111 @Override
2112 public String getOutputName() {
2113 final Set<String> selectedFormats = getFormats();
2114 if (selectedFormats.contains("HTML") || selectedFormats.contains("ALL") || selectedFormats.size() > 1) {
2115 return "dependency-check-report";
2116 } else if (selectedFormats.contains("JENKINS")) {
2117 return "dependency-check-jenkins.html";
2118 } else if (selectedFormats.contains("XML")) {
2119 return "dependency-check-report.xml";
2120 } else if (selectedFormats.contains("JUNIT")) {
2121 return "dependency-check-junit.xml";
2122 } else if (selectedFormats.contains("JSON")) {
2123 return "dependency-check-report.json";
2124 } else if (selectedFormats.contains("SARIF")) {
2125 return "dependency-check-report.sarif";
2126 } else if (selectedFormats.contains("CSV")) {
2127 return "dependency-check-report.csv";
2128 } else {
2129 getLog().warn("Unknown report format used during site generation.");
2130 return "dependency-check-report";
2131 }
2132 }
2133
2134
2135
2136
2137
2138
2139 @Override
2140 public String getCategoryName() {
2141 return MavenReport.CATEGORY_PROJECT_REPORTS;
2142 }
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155 protected Engine initializeEngine() throws DatabaseException, MojoExecutionException, MojoFailureException {
2156 populateSettings();
2157 try {
2158 Downloader.getInstance().configure(settings);
2159 } catch (InvalidSettingException e) {
2160 if (this.failOnError) {
2161 throw new MojoFailureException(e.getMessage(), e);
2162 } else {
2163 throw new MojoExecutionException(e.getMessage(), e);
2164 }
2165 }
2166 return new Engine(settings);
2167 }
2168
2169
2170
2171
2172
2173
2174
2175 protected void populateSettings() {
2176 settings = new Settings();
2177 InputStream mojoProperties = null;
2178 try {
2179 mojoProperties = this.getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
2180 settings.mergeProperties(mojoProperties);
2181 } catch (IOException ex) {
2182 getLog().warn("Unable to load the dependency-check maven mojo.properties file.");
2183 if (getLog().isDebugEnabled()) {
2184 getLog().debug("", ex);
2185 }
2186 } finally {
2187 if (mojoProperties != null) {
2188 try {
2189 mojoProperties.close();
2190 } catch (IOException ex) {
2191 if (getLog().isDebugEnabled()) {
2192 getLog().debug("", ex);
2193 }
2194 }
2195 }
2196 }
2197 settings.setStringIfNotEmpty(Settings.KEYS.MAVEN_LOCAL_REPO, mavenSettings.getLocalRepository());
2198 settings.setBooleanIfNotNull(Settings.KEYS.AUTO_UPDATE, autoUpdate);
2199 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_EXPERIMENTAL_ENABLED, enableExperimental);
2200 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_RETIRED_ENABLED, enableRetired);
2201 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_GOLANG_DEP_ENABLED, golangDepEnabled);
2202 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_GOLANG_MOD_ENABLED, golangModEnabled);
2203 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_DART_ENABLED, dartAnalyzerEnabled);
2204 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_GOLANG_PATH, pathToGo);
2205 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_YARN_PATH, pathToYarn);
2206 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_PNPM_PATH, pathToPnpm);
2207
2208
2209 final Proxy mavenProxy = getMavenProxy();
2210 if (mavenProxy != null) {
2211 final String existing = System.getProperty("https.proxyHost");
2212 if (existing == null && mavenProxy.getHost() != null && !mavenProxy.getHost().isEmpty()) {
2213 System.setProperty("https.proxyHost", mavenProxy.getHost());
2214 if (mavenProxy.getPort() > 0) {
2215 System.setProperty("https.proxyPort", String.valueOf(mavenProxy.getPort()));
2216 }
2217 if (mavenProxy.getUsername() != null && !mavenProxy.getUsername().isEmpty()) {
2218 System.setProperty("https.proxyUser", mavenProxy.getUsername());
2219 }
2220 String password = mavenProxy.getPassword();
2221 if (password != null && !password.isEmpty()) {
2222 try {
2223 password = decryptPasswordFromSettings(password);
2224 } catch (SecDispatcherException ex) {
2225 password = handleSecDispatcherException("proxy", mavenProxy.getId(), password, ex);
2226 }
2227 System.setProperty("https.proxyPassword", password);
2228 }
2229 if (mavenProxy.getNonProxyHosts() != null && !mavenProxy.getNonProxyHosts().isEmpty()) {
2230 System.setProperty("http.nonProxyHosts", mavenProxy.getNonProxyHosts());
2231 }
2232 }
2233 } else if (this.proxy != null && this.proxy.getHost() != null) {
2234
2235 settings.setString(Settings.KEYS.PROXY_SERVER, this.proxy.getHost());
2236 settings.setString(Settings.KEYS.PROXY_PORT, Integer.toString(this.proxy.getPort()));
2237
2238 configureServerCredentials(this.proxy.getServerId(), Settings.KEYS.PROXY_USERNAME, Settings.KEYS.PROXY_PASSWORD);
2239 }
2240
2241 final String[] suppressions = determineSuppressions();
2242 settings.setArrayIfNotEmpty(Settings.KEYS.SUPPRESSION_FILE, suppressions);
2243 settings.setBooleanIfNotNull(Settings.KEYS.UPDATE_VERSION_CHECK_ENABLED, versionCheckEnabled);
2244 settings.setStringIfNotEmpty(Settings.KEYS.CONNECTION_TIMEOUT, connectionTimeout);
2245 settings.setStringIfNotEmpty(Settings.KEYS.CONNECTION_READ_TIMEOUT, readTimeout);
2246 settings.setStringIfNotEmpty(Settings.KEYS.HINTS_FILE, hintsFile);
2247 settings.setFloat(Settings.KEYS.JUNIT_FAIL_ON_CVSS, junitFailOnCVSS);
2248 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_JAR_ENABLED, jarAnalyzerEnabled);
2249 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NUSPEC_ENABLED, nuspecAnalyzerEnabled);
2250 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NUGETCONF_ENABLED, nugetconfAnalyzerEnabled);
2251 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_LIBMAN_ENABLED, libmanAnalyzerEnabled);
2252 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_CENTRAL_ENABLED, centralAnalyzerEnabled);
2253 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_CENTRAL_USE_CACHE, centralAnalyzerUseCache);
2254 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_ENABLED, artifactoryAnalyzerEnabled);
2255 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NEXUS_ENABLED, nexusAnalyzerEnabled);
2256 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_ASSEMBLY_ENABLED, assemblyAnalyzerEnabled);
2257 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_MSBUILD_PROJECT_ENABLED, msbuildAnalyzerEnabled);
2258 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_ARCHIVE_ENABLED, archiveAnalyzerEnabled);
2259 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_KNOWN_EXPLOITED_ENABLED, knownExploitedEnabled);
2260 settings.setStringIfNotEmpty(Settings.KEYS.KEV_URL, knownExploitedUrl);
2261 settings.setStringIfNotEmpty(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS, zipExtensions);
2262 settings.setStringIfNotEmpty(Settings.KEYS.ANALYZER_ASSEMBLY_DOTNET_PATH, pathToCore);
2263 settings.setStringIfNotEmpty(Settings.KEYS.ANALYZER_NEXUS_URL, nexusUrl);
2264 configureServerCredentials(nexusServerId, Settings.KEYS.ANALYZER_NEXUS_USER, Settings.KEYS.ANALYZER_NEXUS_PASSWORD);
2265 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NEXUS_USES_PROXY, nexusUsesProxy);
2266 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_URL, artifactoryAnalyzerUrl);
2267 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_USES_PROXY, artifactoryAnalyzerUseProxy);
2268 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_PARALLEL_ANALYSIS, artifactoryAnalyzerParallelAnalysis);
2269 if (Boolean.TRUE.equals(artifactoryAnalyzerEnabled)) {
2270 if (artifactoryAnalyzerServerId != null) {
2271 configureServerCredentials(artifactoryAnalyzerServerId, Settings.KEYS.ANALYZER_ARTIFACTORY_API_USERNAME,
2272 Settings.KEYS.ANALYZER_ARTIFACTORY_API_TOKEN);
2273 } else {
2274 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_API_USERNAME, artifactoryAnalyzerUsername);
2275 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_API_TOKEN, artifactoryAnalyzerApiToken);
2276 }
2277 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_ARTIFACTORY_BEARER_TOKEN, artifactoryAnalyzerBearerToken);
2278 }
2279 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_PYTHON_DISTRIBUTION_ENABLED, pyDistributionAnalyzerEnabled);
2280 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_PYTHON_PACKAGE_ENABLED, pyPackageAnalyzerEnabled);
2281 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_RUBY_GEMSPEC_ENABLED, rubygemsAnalyzerEnabled);
2282 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_OPENSSL_ENABLED, opensslAnalyzerEnabled);
2283 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_CMAKE_ENABLED, cmakeAnalyzerEnabled);
2284 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_AUTOCONF_ENABLED, autoconfAnalyzerEnabled);
2285 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_MAVEN_INSTALL_ENABLED, mavenInstallAnalyzerEnabled);
2286 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_PIP_ENABLED, pipAnalyzerEnabled);
2287 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_PIPFILE_ENABLED, pipfileAnalyzerEnabled);
2288 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_POETRY_ENABLED, poetryAnalyzerEnabled);
2289 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_COMPOSER_LOCK_ENABLED, composerAnalyzerEnabled);
2290 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_COMPOSER_LOCK_SKIP_DEV, composerAnalyzerSkipDev);
2291 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_CPANFILE_ENABLED, cpanfileAnalyzerEnabled);
2292 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED, nodeAnalyzerEnabled);
2293 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NODE_AUDIT_ENABLED, nodeAuditAnalyzerEnabled);
2294 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_NODE_AUDIT_URL, nodeAuditAnalyzerUrl);
2295 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NODE_AUDIT_USE_CACHE, nodeAuditAnalyzerUseCache);
2296 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NODE_PACKAGE_SKIPDEV, nodePackageSkipDevDependencies);
2297 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_NODE_AUDIT_SKIPDEV, nodeAuditSkipDevDependencies);
2298 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_YARN_AUDIT_ENABLED, yarnAuditAnalyzerEnabled);
2299 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_PNPM_AUDIT_ENABLED, pnpmAuditAnalyzerEnabled);
2300 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_RETIREJS_ENABLED, retireJsAnalyzerEnabled);
2301 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_RETIREJS_REPO_JS_URL, retireJsUrl);
2302 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_RETIREJS_FORCEUPDATE, retireJsForceUpdate);
2303 if (retireJsUser == null && retireJsPassword == null && retireJsUrlServerId != null) {
2304 configureServerCredentials(retireJsUrlServerId, Settings.KEYS.ANALYZER_RETIREJS_REPO_JS_USER, Settings.KEYS.ANALYZER_RETIREJS_REPO_JS_PASSWORD);
2305 } else {
2306 settings.setStringIfNotEmpty(Settings.KEYS.ANALYZER_RETIREJS_REPO_JS_USER, retireJsUser);
2307 settings.setStringIfNotEmpty(Settings.KEYS.ANALYZER_RETIREJS_REPO_JS_PASSWORD, retireJsPassword);
2308 }
2309 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_MIX_AUDIT_ENABLED, mixAuditAnalyzerEnabled);
2310 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_MIX_AUDIT_PATH, mixAuditPath);
2311 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_BUNDLE_AUDIT_ENABLED, bundleAuditAnalyzerEnabled);
2312 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_BUNDLE_AUDIT_PATH, bundleAuditPath);
2313 settings.setStringIfNotNull(Settings.KEYS.ANALYZER_BUNDLE_AUDIT_WORKING_DIRECTORY, bundleAuditWorkingDirectory);
2314 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_COCOAPODS_ENABLED, cocoapodsAnalyzerEnabled);
2315 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_CARTHAGE_ENABLED, carthageAnalyzerEnabled);
2316 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_SWIFT_PACKAGE_MANAGER_ENABLED, swiftPackageManagerAnalyzerEnabled);
2317 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_SWIFT_PACKAGE_RESOLVED_ENABLED, swiftPackageResolvedAnalyzerEnabled);
2318 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_OSSINDEX_ENABLED, ossindexAnalyzerEnabled);
2319 settings.setStringIfNotEmpty(Settings.KEYS.ANALYZER_OSSINDEX_URL, ossindexAnalyzerUrl);
2320 configureServerCredentials(ossIndexServerId, Settings.KEYS.ANALYZER_OSSINDEX_USER, Settings.KEYS.ANALYZER_OSSINDEX_PASSWORD);
2321 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_OSSINDEX_USE_CACHE, ossindexAnalyzerUseCache);
2322 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_OSSINDEX_WARN_ONLY_ON_REMOTE_ERRORS, ossIndexWarnOnlyOnRemoteErrors);
2323 if (retirejs != null) {
2324 settings.setBooleanIfNotNull(Settings.KEYS.ANALYZER_RETIREJS_FILTER_NON_VULNERABLE, retirejs.getFilterNonVulnerable());
2325 settings.setArrayIfNotEmpty(Settings.KEYS.ANALYZER_RETIREJS_FILTERS, retirejs.getFilters());
2326 }
2327
2328 settings.setStringIfNotEmpty(Settings.KEYS.DB_DRIVER_NAME, databaseDriverName);
2329 settings.setStringIfNotEmpty(Settings.KEYS.DB_DRIVER_PATH, databaseDriverPath);
2330 settings.setStringIfNotEmpty(Settings.KEYS.DB_CONNECTION_STRING, connectionString);
2331 if (databaseUser == null && databasePassword == null && serverId != null) {
2332 configureServerCredentials(serverId, Settings.KEYS.DB_USER, Settings.KEYS.DB_PASSWORD);
2333 } else {
2334 settings.setStringIfNotEmpty(Settings.KEYS.DB_USER, databaseUser);
2335 settings.setStringIfNotEmpty(Settings.KEYS.DB_PASSWORD, databasePassword);
2336 }
2337 settings.setStringIfNotEmpty(Settings.KEYS.DATA_DIRECTORY, dataDirectory);
2338 settings.setStringIfNotEmpty(Settings.KEYS.DB_FILE_NAME, dbFilename);
2339 settings.setStringIfNotNull(Settings.KEYS.NVD_API_ENDPOINT, nvdApiEndpoint);
2340 settings.setIntIfNotNull(Settings.KEYS.NVD_API_DELAY, nvdApiDelay);
2341 settings.setIntIfNotNull(Settings.KEYS.NVD_API_RESULTS_PER_PAGE, nvdApiResultsPerPage);
2342 settings.setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_URL, nvdDatafeedUrl);
2343 settings.setIntIfNotNull(Settings.KEYS.NVD_API_VALID_FOR_HOURS, nvdValidForHours);
2344 settings.setIntIfNotNull(Settings.KEYS.NVD_API_MAX_RETRY_COUNT, nvdMaxRetryCount);
2345 if (nvdApiKey == null) {
2346 if (nvdApiKeyEnvironmentVariable != null) {
2347 settings.setStringIfNotEmpty(Settings.KEYS.NVD_API_KEY, System.getenv(nvdApiKeyEnvironmentVariable));
2348 getLog().debug("Using NVD API key from environment variable " + nvdApiKeyEnvironmentVariable);
2349 } else if (nvdApiServerId != null) {
2350 configureServerCredentialsApiKey(nvdApiServerId, Settings.KEYS.NVD_API_KEY);
2351 getLog().debug("Using NVD API key from server's password with id " + nvdApiServerId + " in settings.xml");
2352 }
2353 } else {
2354 settings.setStringIfNotEmpty(Settings.KEYS.NVD_API_KEY, nvdApiKey);
2355 }
2356 if (nvdUser == null && nvdPassword == null && nvdDatafeedServerId != null) {
2357 configureServerCredentials(nvdDatafeedServerId, Settings.KEYS.NVD_API_DATAFEED_USER, Settings.KEYS.NVD_API_DATAFEED_PASSWORD);
2358 } else {
2359 settings.setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_USER, nvdUser);
2360 settings.setStringIfNotEmpty(Settings.KEYS.NVD_API_DATAFEED_PASSWORD, nvdPassword);
2361 }
2362 settings.setBooleanIfNotNull(Settings.KEYS.PRETTY_PRINT, prettyPrint);
2363 artifactScopeExcluded = new ArtifactScopeExcluded(skipTestScope, skipProvidedScope, skipSystemScope, skipRuntimeScope);
2364 artifactTypeExcluded = new ArtifactTypeExcluded(skipArtifactType);
2365 if (suppressionFileUser == null && suppressionFilePassword == null && suppressionFileServerId != null) {
2366 configureServerCredentials(suppressionFileServerId, Settings.KEYS.SUPPRESSION_FILE_USER, Settings.KEYS.SUPPRESSION_FILE_PASSWORD);
2367 } else {
2368 settings.setStringIfNotEmpty(Settings.KEYS.SUPPRESSION_FILE_USER, suppressionFileUser);
2369 settings.setStringIfNotEmpty(Settings.KEYS.SUPPRESSION_FILE_PASSWORD, suppressionFilePassword);
2370 }
2371 settings.setIntIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_VALID_FOR_HOURS, hostedSuppressionsValidForHours);
2372 settings.setStringIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_URL, hostedSuppressionsUrl);
2373 settings.setBooleanIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_FORCEUPDATE, hostedSuppressionsForceUpdate);
2374 settings.setBooleanIfNotNull(Settings.KEYS.HOSTED_SUPPRESSIONS_ENABLED, hostedSuppressionsEnabled);
2375 }
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387 private void configureServerCredentials(String serverId, String userSettingKey, String passwordSettingKey) {
2388 if (serverId != null) {
2389 final Server server = settingsXml.getServer(serverId);
2390 if (server != null) {
2391 final String username = server.getUsername();
2392 String password = null;
2393 try {
2394 password = decryptPasswordFromSettings(server.getPassword());
2395 } catch (SecDispatcherException ex) {
2396 password = handleSecDispatcherException("server", serverId, server.getPassword(), ex);
2397 }
2398 settings.setStringIfNotEmpty(userSettingKey, username);
2399 settings.setStringIfNotEmpty(passwordSettingKey, password);
2400 } else {
2401 getLog().error(String.format("Server '%s' not found in the settings.xml file", serverId));
2402 }
2403 }
2404 }
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414 private void configureServerCredentialsApiKey(String serverId, String apiKeySetting) {
2415 if (serverId != null) {
2416 final Server server = settingsXml.getServer(serverId);
2417 if (server != null) {
2418 String password = null;
2419 try {
2420 password = decryptPasswordFromSettings(server.getPassword());
2421 } catch (SecDispatcherException ex) {
2422 password = handleSecDispatcherException("server", serverId, server.getPassword(), ex);
2423 }
2424 settings.setStringIfNotEmpty(apiKeySetting, password);
2425 } else {
2426 getLog().error(String.format("Server '%s' not found in the settings.xml file", serverId));
2427 }
2428 }
2429 }
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440 private String decryptPasswordFromSettings(String password) throws SecDispatcherException {
2441
2442
2443
2444
2445
2446
2447
2448 if (securityDispatcher instanceof DefaultSecDispatcher) {
2449 ((DefaultSecDispatcher) securityDispatcher).setConfigurationFile("~/.m2/settings-security.xml");
2450 }
2451
2452 return securityDispatcher.decrypt(password);
2453 }
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468 private String handleSecDispatcherException(String settingsElementName, String settingsElementId, String passwordValueFromSettings,
2469 SecDispatcherException ex) {
2470 String password = passwordValueFromSettings;
2471 if (ex.getCause() instanceof FileNotFoundException
2472 || (ex.getCause() != null && ex.getCause().getCause() instanceof FileNotFoundException)) {
2473
2474 final String tmp = passwordValueFromSettings;
2475 if (tmp.startsWith("{") && tmp.endsWith("}")) {
2476 getLog().error(String.format(
2477 "Unable to decrypt the %s password for %s id '%s' in settings.xml%n\tCause: %s",
2478 settingsElementName, settingsElementName, settingsElementId, ex.getMessage()));
2479 } else {
2480 password = tmp;
2481 }
2482 } else {
2483 getLog().error(String.format(
2484 "Unable to decrypt the %s password for %s id '%s' in settings.xml%n\tCause: %s",
2485 settingsElementName, settingsElementName, settingsElementId, ex.getMessage()));
2486 }
2487 return password;
2488 }
2489
2490
2491
2492
2493
2494
2495
2496 private String[] determineSuppressions() {
2497 String[] suppressions = suppressionFiles;
2498 if (suppressionFile != null) {
2499 if (suppressions == null) {
2500 suppressions = new String[]{suppressionFile};
2501 } else {
2502 suppressions = Arrays.copyOf(suppressions, suppressions.length + 1);
2503 suppressions[suppressions.length - 1] = suppressionFile;
2504 }
2505 }
2506 return suppressions;
2507 }
2508
2509
2510
2511
2512 private void muteNoisyLoggers() {
2513 System.setProperty("jcs.logSystem", "slf4j");
2514 if (!getLog().isDebugEnabled()) {
2515 Slf4jAdapter.muteLogging(true);
2516 }
2517
2518 final String[] noisyLoggers = {
2519 "org.apache.hc"
2520 };
2521 for (String loggerName : noisyLoggers) {
2522 System.setProperty("org.slf4j.simpleLogger.log." + loggerName, "error");
2523 }
2524 }
2525
2526
2527
2528
2529
2530
2531 private Proxy getMavenProxy() {
2532 if (mavenSettings != null) {
2533 final List<Proxy> proxies = mavenSettings.getProxies();
2534 if (proxies != null && !proxies.isEmpty()) {
2535 if (mavenSettingsProxyId != null) {
2536 for (Proxy proxy : proxies) {
2537 if (mavenSettingsProxyId.equalsIgnoreCase(proxy.getId())) {
2538 return proxy;
2539 }
2540 }
2541 } else {
2542 for (Proxy aProxy : proxies) {
2543 if (aProxy.isActive()) {
2544 return aProxy;
2545 }
2546 }
2547 }
2548 }
2549 }
2550 return null;
2551 }
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563 protected MavenProject getProject() {
2564 return project;
2565 }
2566
2567
2568
2569
2570
2571
2572 protected List<MavenProject> getReactorProjects() {
2573 return reactorProjects;
2574 }
2575
2576
2577
2578
2579
2580
2581 private Set<String> getFormats() {
2582 final Set<String> invalid = new HashSet<>();
2583 final Set<String> selectedFormats = formats == null || formats.length == 0 ? new HashSet<>() : new HashSet<>(Arrays.asList(formats));
2584 selectedFormats.forEach((s) -> {
2585 try {
2586 ReportGenerator.Format.valueOf(s.toUpperCase());
2587 } catch (IllegalArgumentException ex) {
2588 invalid.add(s);
2589 }
2590 });
2591 invalid.forEach((s) -> getLog().warn("Invalid report format specified: " + s));
2592 if (selectedFormats.contains("true")) {
2593 selectedFormats.remove("true");
2594 }
2595 if (format != null && selectedFormats.isEmpty()) {
2596 selectedFormats.add(format);
2597 }
2598 return selectedFormats;
2599 }
2600
2601
2602
2603
2604
2605
2606
2607 public List<String> getExcludes() {
2608 if (excludes == null) {
2609 excludes = new ArrayList<>();
2610 }
2611 return excludes;
2612 }
2613
2614
2615
2616
2617
2618
2619 protected Filter<String> getArtifactScopeExcluded() {
2620 return artifactScopeExcluded;
2621 }
2622
2623
2624
2625
2626
2627
2628 protected Settings getSettings() {
2629 return settings;
2630 }
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641 protected void checkForFailure(Dependency[] dependencies) throws MojoFailureException {
2642 final StringBuilder ids = new StringBuilder();
2643 for (Dependency d : dependencies) {
2644 boolean addName = true;
2645 for (Vulnerability v : d.getVulnerabilities()) {
2646 final Double cvssV2 = v.getCvssV2() != null && v.getCvssV2().getCvssData() != null && v.getCvssV2().getCvssData().getBaseScore() != null ? v.getCvssV2().getCvssData().getBaseScore() : -1;
2647 final Double cvssV3 = v.getCvssV3() != null && v.getCvssV3().getCvssData() != null && v.getCvssV3().getCvssData().getBaseScore() != null ? v.getCvssV3().getCvssData().getBaseScore() : -1;
2648 final Double unscoredCvss = v.getUnscoredSeverity() != null ? SeverityUtil.estimateCvssV2(v.getUnscoredSeverity()) : -1;
2649
2650 if (failBuildOnAnyVulnerability || cvssV2 >= failBuildOnCVSS
2651 || cvssV3 >= failBuildOnCVSS
2652 || unscoredCvss >= failBuildOnCVSS
2653
2654 || (failBuildOnCVSS <= 0.0)) {
2655 String name = v.getName();
2656 if (cvssV3 >= 0.0) {
2657 name += "(" + cvssV3 + ")";
2658 } else if (cvssV2 >= 0.0) {
2659 name += "(" + cvssV2 + ")";
2660 } else if (unscoredCvss >= 0.0) {
2661 name += "(" + unscoredCvss + ")";
2662 }
2663 if (addName) {
2664 addName = false;
2665 ids.append(NEW_LINE).append(d.getFileName()).append(": ");
2666 ids.append(name);
2667 } else {
2668 ids.append(", ").append(name);
2669 }
2670 }
2671 }
2672 }
2673 if (ids.length() > 0) {
2674 final String msg;
2675 if (showSummary) {
2676 if (failBuildOnAnyVulnerability) {
2677 msg = String.format("%n%nOne or more dependencies were identified with vulnerabilities: %n%s%n%n"
2678 + "See the dependency-check report for more details.%n%n", ids);
2679 } else {
2680 msg = String.format("%n%nOne or more dependencies were identified with vulnerabilities that have a CVSS score greater than or "
2681 + "equal to '%.1f': %n%s%n%nSee the dependency-check report for more details.%n%n", failBuildOnCVSS, ids);
2682 }
2683 } else {
2684 msg = String.format("%n%nOne or more dependencies were identified with vulnerabilities.%n%n"
2685 + "See the dependency-check report for more details.%n%n");
2686 }
2687 throw new MojoFailureException(msg);
2688 }
2689 }
2690
2691
2692
2693
2694
2695
2696
2697
2698 protected void showSummary(MavenProject mp, Dependency[] dependencies) {
2699 if (showSummary) {
2700 DependencyCheckScanAgent.showSummary(mp.getName(), dependencies);
2701 }
2702 }
2703
2704
2705
2706 private ExceptionCollection scanDependencyNode(DependencyNode dependencyNode, DependencyNode root,
2707 Engine engine, MavenProject project, List<ArtifactResult> allResolvedDeps,
2708 ProjectBuildingRequest buildingRequest, boolean aggregate, ExceptionCollection exceptionCollection) {
2709 ExceptionCollection exCol = exceptionCollection;
2710 if (artifactScopeExcluded.passes(dependencyNode.getArtifact().getScope())
2711 || artifactTypeExcluded.passes(dependencyNode.getArtifact().getType())) {
2712 return exCol;
2713 }
2714
2715 boolean isResolved = false;
2716 File artifactFile = null;
2717 String artifactId = null;
2718 String groupId = null;
2719 String version = null;
2720 List<ArtifactVersion> availableVersions = null;
2721 if (org.apache.maven.artifact.Artifact.SCOPE_SYSTEM.equals(dependencyNode.getArtifact().getScope())) {
2722 final Artifact a = dependencyNode.getArtifact();
2723 if (a.isResolved() && a.getFile().isFile()) {
2724 artifactFile = a.getFile();
2725 isResolved = artifactFile.isFile();
2726 groupId = a.getGroupId();
2727 artifactId = a.getArtifactId();
2728 version = a.getVersion();
2729 availableVersions = a.getAvailableVersions();
2730 } else {
2731 for (org.apache.maven.model.Dependency d : project.getDependencies()) {
2732 if (d.getSystemPath() != null && artifactsMatch(d, a)) {
2733 artifactFile = new File(d.getSystemPath());
2734 isResolved = artifactFile.isFile();
2735 groupId = a.getGroupId();
2736 artifactId = a.getArtifactId();
2737 version = a.getVersion();
2738 availableVersions = a.getAvailableVersions();
2739 break;
2740 }
2741 }
2742 }
2743 Throwable ignored = null;
2744 if (!isResolved) {
2745
2746
2747 try {
2748 tryResolutionOnce(project, allResolvedDeps, buildingRequest);
2749 final Artifact result = findInAllDeps(allResolvedDeps, dependencyNode.getArtifact(), project);
2750 isResolved = result.isResolved();
2751 artifactFile = result.getFile();
2752 groupId = result.getGroupId();
2753 artifactId = result.getArtifactId();
2754 version = result.getVersion();
2755 availableVersions = result.getAvailableVersions();
2756 } catch (DependencyNotFoundException | DependencyResolverException e) {
2757 getLog().warn("Error performing last-resort System-scoped dependency resolution: " + e.getMessage());
2758 ignored = e;
2759 }
2760 }
2761 if (!isResolved) {
2762 final StringBuilder message = new StringBuilder("Unable to resolve system scoped dependency: ");
2763 if (artifactFile != null) {
2764 message.append(dependencyNode.toNodeString()).append(" at path ").append(artifactFile);
2765 } else {
2766 message.append(dependencyNode.toNodeString()).append(" at path ").append(a.getFile());
2767 }
2768 getLog().error(message);
2769 if (exCol == null) {
2770 exCol = new ExceptionCollection();
2771 }
2772 final Exception thrown = new DependencyNotFoundException(message.toString());
2773 if (ignored != null) {
2774 thrown.addSuppressed(ignored);
2775 }
2776 exCol.addException(thrown);
2777 }
2778 } else {
2779 final Artifact dependencyArtifact = dependencyNode.getArtifact();
2780 final Artifact result;
2781 if (dependencyArtifact.isResolved()) {
2782
2783
2784
2785 getLog().debug(String.format("Skipping artifact %s, already resolved", dependencyArtifact.getArtifactId()));
2786 result = dependencyArtifact;
2787 } else {
2788 try {
2789 tryResolutionOnce(project, allResolvedDeps, buildingRequest);
2790 result = findInAllDeps(allResolvedDeps, dependencyNode.getArtifact(), project);
2791 } catch (DependencyNotFoundException | DependencyResolverException ex) {
2792 getLog().debug(String.format("Aggregate : %s", aggregate));
2793 boolean addException = true;
2794
2795 if (!aggregate) {
2796
2797 } else if (addReactorDependency(engine, dependencyNode.getArtifact(), project)) {
2798
2799 addException = false;
2800 }
2801 if (addException) {
2802 if (exCol == null) {
2803 exCol = new ExceptionCollection();
2804 }
2805 exCol.addException(ex);
2806 }
2807 return exCol;
2808 }
2809 }
2810 if (aggregate && virtualSnapshotsFromReactor
2811 && dependencyNode.getArtifact().isSnapshot()
2812 && addSnapshotReactorDependency(engine, dependencyNode.getArtifact(), project)) {
2813 return exCol;
2814 }
2815 isResolved = result.isResolved();
2816 artifactFile = result.getFile();
2817 groupId = result.getGroupId();
2818 artifactId = result.getArtifactId();
2819 version = result.getVersion();
2820 availableVersions = result.getAvailableVersions();
2821 }
2822 if (isResolved && artifactFile != null) {
2823 final List<Dependency> deps = engine.scan(artifactFile.getAbsoluteFile(),
2824 createProjectReferenceName(project, dependencyNode));
2825 if (deps != null) {
2826 processResolvedArtifact(artifactFile, deps, groupId, artifactId, version, root, project, availableVersions, dependencyNode);
2827 } else if ("import".equals(dependencyNode.getArtifact().getScope())) {
2828 final String msg = String.format("Skipping '%s:%s' in project %s as it uses an `import` scope",
2829 dependencyNode.getArtifact().getId(), dependencyNode.getArtifact().getScope(), project.getName());
2830 getLog().debug(msg);
2831 } else if ("pom".equals(dependencyNode.getArtifact().getType())) {
2832 exCol = processPomArtifact(artifactFile, root, project, engine, exCol);
2833 } else {
2834 if (!scannedFiles.contains(artifactFile)) {
2835 final String msg = String.format("No analyzer could be found or the artifact has been scanned twice for '%s:%s' in project %s",
2836 dependencyNode.getArtifact().getId(), dependencyNode.getArtifact().getScope(), project.getName());
2837 getLog().warn(msg);
2838 }
2839 }
2840 } else {
2841 final String msg = String.format("Unable to resolve '%s' in project %s",
2842 dependencyNode.getArtifact().getId(), project.getName());
2843 getLog().debug(msg);
2844 if (exCol == null) {
2845 exCol = new ExceptionCollection();
2846 }
2847 }
2848 return exCol;
2849 }
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870 private void tryResolutionOnce(MavenProject project, List<ArtifactResult> allResolvedDeps, ProjectBuildingRequest buildingRequest) throws DependencyResolverException {
2871 if (allResolvedDeps.isEmpty()) {
2872 try {
2873 final List<org.apache.maven.model.Dependency> dependencies = project.getDependencies();
2874 final List<org.apache.maven.model.Dependency> managedDependencies = project
2875 .getDependencyManagement() == null ? null : project.getDependencyManagement().getDependencies();
2876 final Iterable<ArtifactResult> allDeps = dependencyResolver
2877 .resolveDependencies(buildingRequest, dependencies, managedDependencies, null);
2878 allDeps.forEach(allResolvedDeps::add);
2879 } catch (DependencyResolverException dre) {
2880 if (dre.getCause() instanceof org.eclipse.aether.resolution.DependencyResolutionException) {
2881 final List<ArtifactResult> successResults = Mshared998Util
2882 .getResolutionResults((org.eclipse.aether.resolution.DependencyResolutionException) dre.getCause());
2883 allResolvedDeps.addAll(successResults);
2884 } else {
2885 throw dre;
2886 }
2887 }
2888 }
2889 }
2890
2891
2892
2893 private void processResolvedArtifact(File artifactFile, final List<Dependency> deps,
2894 String groupId, String artifactId, String version, DependencyNode root,
2895 MavenProject project1, List<ArtifactVersion> availableVersions,
2896 DependencyNode dependencyNode) {
2897 scannedFiles.add(artifactFile);
2898 Dependency d = null;
2899 if (deps.size() == 1) {
2900 d = deps.get(0);
2901
2902 } else {
2903 for (Dependency possible : deps) {
2904 if (artifactFile.getAbsoluteFile().equals(possible.getActualFile())) {
2905 d = possible;
2906 break;
2907 }
2908 }
2909 for (Dependency dep : deps) {
2910 if (d != null && d != dep) {
2911 final String includedBy = buildReference(groupId, artifactId, version);
2912 dep.addIncludedBy(includedBy);
2913 }
2914 }
2915 }
2916 if (d != null) {
2917 final MavenArtifact ma = new MavenArtifact(groupId, artifactId, version);
2918 d.addAsEvidence("pom", ma, Confidence.HIGHEST);
2919 if (root != null) {
2920 final String includedby = buildReference(
2921 root.getArtifact().getGroupId(),
2922 root.getArtifact().getArtifactId(),
2923 root.getArtifact().getVersion());
2924 d.addIncludedBy(includedby);
2925 } else {
2926 final String includedby = buildReference(project1.getGroupId(), project1.getArtifactId(), project1.getVersion());
2927 d.addIncludedBy(includedby);
2928 }
2929 if (availableVersions != null) {
2930 for (ArtifactVersion av : availableVersions) {
2931 d.addAvailableVersion(av.toString());
2932 }
2933 }
2934 getLog().debug(String.format("Adding project reference %s on dependency %s", project1.getName(), d.getDisplayFileName()));
2935 } else if (getLog().isDebugEnabled()) {
2936 final String msg = String.format("More than 1 dependency was identified in first pass scan of '%s' in project %s", dependencyNode.getArtifact().getId(), project1.getName());
2937 getLog().debug(msg);
2938 }
2939 }
2940
2941
2942 private ExceptionCollection processPomArtifact(File artifactFile, DependencyNode root,
2943 MavenProject project1, Engine engine, ExceptionCollection exCollection) {
2944 ExceptionCollection exCol = exCollection;
2945 try {
2946 final Dependency d = new Dependency(artifactFile.getAbsoluteFile());
2947 final Model pom = PomUtils.readPom(artifactFile.getAbsoluteFile());
2948 JarAnalyzer.setPomEvidence(d, pom, null, true);
2949 if (root != null) {
2950 final String includedby = buildReference(
2951 root.getArtifact().getGroupId(),
2952 root.getArtifact().getArtifactId(),
2953 root.getArtifact().getVersion());
2954 d.addIncludedBy(includedby);
2955 } else {
2956 final String includedby = buildReference(project1.getGroupId(), project1.getArtifactId(), project1.getVersion());
2957 d.addIncludedBy(includedby);
2958 }
2959 engine.addDependency(d);
2960 } catch (AnalysisException ex) {
2961 if (exCol == null) {
2962 exCol = new ExceptionCollection();
2963 }
2964 exCol.addException(ex);
2965 getLog().debug("Error reading pom " + artifactFile.getAbsoluteFile(), ex);
2966 }
2967 return exCol;
2968 }
2969
2970 }
2971