1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.analyzer;
19
20 import java.io.IOException;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.concurrent.ThreadSafe;
23 import org.apache.lucene.index.CorruptIndexException;
24 import org.apache.lucene.queryparser.classic.ParseException;
25 import org.owasp.dependencycheck.Engine;
26 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
27 import org.owasp.dependencycheck.data.cpe.IndexException;
28 import org.owasp.dependencycheck.data.cpe.NpmCpeMemoryIndex;
29 import org.owasp.dependencycheck.data.nvdcve.CveDB;
30 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
31 import org.owasp.dependencycheck.dependency.Dependency;
32 import org.owasp.dependencycheck.utils.Settings;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37
38
39
40
41
42
43
44
45 @ThreadSafe
46 @Experimental
47 public class NpmCPEAnalyzer extends CPEAnalyzer {
48
49
50
51
52 private static final Logger LOGGER = LoggerFactory.getLogger(NpmCPEAnalyzer.class);
53
54
55
56
57
58
59 @Override
60 public AnalysisPhase getAnalysisPhase() {
61
62
63
64
65 return AnalysisPhase.PRE_IDENTIFIER_ANALYSIS;
66 }
67
68
69
70
71
72
73 @Override
74 public String getName() {
75 return "NPM CPE Analyzer";
76 }
77
78
79
80
81
82
83
84 @Override
85 protected String getAnalyzerEnabledSettingKey() {
86 return Settings.KEYS.ANALYZER_NPM_CPE_ENABLED;
87 }
88
89
90
91
92
93
94
95
96
97
98 @Override
99 public void open(CveDB cve) throws IOException, DatabaseException {
100 setCveDB(cve);
101 setMemoryIndex(NpmCpeMemoryIndex.getInstance());
102 try {
103 final long creationStart = System.currentTimeMillis();
104 getMemoryIndex().open(cve.getVendorProductListForNode(), this.getSettings());
105 final long creationSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - creationStart);
106 LOGGER.info("Created CPE Index ({} seconds)", creationSeconds);
107 } catch (IndexException ex) {
108 LOGGER.debug("IndexException", ex);
109 throw new DatabaseException(ex);
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122 @Override
123 protected void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
124 if (!"npm".equals(dependency.getEcosystem())) {
125 return;
126 }
127 try {
128 determineCPE(dependency);
129 } catch (CorruptIndexException ex) {
130 throw new AnalysisException("CPE Index is corrupt.", ex);
131 } catch (IOException ex) {
132 throw new AnalysisException("Failure opening the CPE Index.", ex);
133 } catch (ParseException ex) {
134 throw new AnalysisException("Unable to parse the generated Lucene query for this dependency.", ex);
135 }
136 }
137 }