-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
AbstractNpmAnalyzer.java
542 lines (520 loc) · 24.3 KB
/
AbstractNpmAnalyzer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* This file is part of dependency-check-core.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2017 Steve Springett. All Rights Reserved.
*/
package org.owasp.dependencycheck.analyzer;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.github.packageurl.PackageURL.StandardTypes;
import com.github.packageurl.PackageURLBuilder;
import org.semver4j.Semver;
import org.semver4j.SemverException;
import org.owasp.dependencycheck.Engine;
import org.owasp.dependencycheck.data.nodeaudit.Advisory;
import org.owasp.dependencycheck.data.nodeaudit.NodeAuditSearch;
import org.owasp.dependencycheck.dependency.Confidence;
import org.owasp.dependencycheck.dependency.Dependency;
import org.owasp.dependencycheck.dependency.Reference;
import org.owasp.dependencycheck.dependency.Vulnerability;
import org.owasp.dependencycheck.dependency.VulnerableSoftware;
import org.owasp.dependencycheck.dependency.VulnerableSoftwareBuilder;
import org.owasp.dependencycheck.exception.InitializationException;
import org.owasp.dependencycheck.utils.InvalidSettingException;
import org.owasp.dependencycheck.utils.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;
import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.lang3.StringUtils;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.analyzer.exception.UnexpectedAnalysisException;
import org.owasp.dependencycheck.data.nvd.ecosystem.Ecosystem;
import org.owasp.dependencycheck.dependency.EvidenceType;
import org.owasp.dependencycheck.dependency.naming.GenericIdentifier;
import org.owasp.dependencycheck.dependency.naming.Identifier;
import org.owasp.dependencycheck.dependency.naming.PurlIdentifier;
import org.owasp.dependencycheck.utils.Checksum;
import us.springett.parsers.cpe.exceptions.CpeValidationException;
import us.springett.parsers.cpe.values.Part;
/**
* An abstract NPM analyzer that contains common methods for concrete
* implementations.
*
* @author Steve Springett
*/
@ThreadSafe
public abstract class AbstractNpmAnalyzer extends AbstractFileTypeAnalyzer {
/**
* The logger.
*/
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNpmAnalyzer.class);
/**
* A descriptor for the type of dependencies processed or added by this
* analyzer.
*/
public static final String NPM_DEPENDENCY_ECOSYSTEM = Ecosystem.NODEJS;
/**
* The file name to scan.
*/
private static final String PACKAGE_JSON = "package.json";
/**
* The Node Audit Searcher.
*/
private NodeAuditSearch searcher;
/**
* Determines if the file can be analyzed by the analyzer.
*
* @param pathname the path to the file
* @return true if the file can be analyzed by the given analyzer; otherwise
* false
*/
@Override
public boolean accept(File pathname) {
boolean accept = super.accept(pathname);
if (accept) {
try {
accept = shouldProcess(pathname);
} catch (AnalysisException ex) {
throw new UnexpectedAnalysisException(ex.getMessage(), ex.getCause());
}
}
return accept;
}
/**
* Determines if the path contains "/node_modules/" or "/bower_components/"
* (i.e. it is a child module). This analyzer does not scan child modules.
*
* @param pathname the path to test
* @return <code>true</code> if the path does not contain "/node_modules/"
* or "/bower_components/"
* @throws AnalysisException thrown if the canonical path cannot be obtained
* from the given file
*/
public static boolean shouldProcess(File pathname) throws AnalysisException {
try {
// Do not scan the node_modules (or bower_components) directory
final String canonicalPath = pathname.getCanonicalPath();
if (canonicalPath.contains(File.separator + "node_modules" + File.separator)
|| canonicalPath.contains(File.separator + "bower_components" + File.separator)) {
LOGGER.debug("Skipping analysis of node/bower module: {}", canonicalPath);
return false;
}
} catch (IOException ex) {
throw new AnalysisException("Unable to process dependency", ex);
}
return true;
}
/**
* Construct a dependency object.
*
* @param dependency the parent dependency
* @param name the name of the dependency to create
* @param version the version of the dependency to create
* @param scope the scope of the dependency being created
* @return the generated dependency
*/
protected Dependency createDependency(Dependency dependency, String name, String version, String scope) {
final Dependency nodeModule = new Dependency(new File(dependency.getActualFile() + "?" + name), true);
nodeModule.setEcosystem(NPM_DEPENDENCY_ECOSYSTEM);
//this is virtual - the sha1 is purely for the hyperlink in the final html report
nodeModule.setSha1sum(Checksum.getSHA1Checksum(String.format("%s:%s", name, version)));
nodeModule.setSha256sum(Checksum.getSHA256Checksum(String.format("%s:%s", name, version)));
nodeModule.setMd5sum(Checksum.getMD5Checksum(String.format("%s:%s", name, version)));
nodeModule.addEvidence(EvidenceType.PRODUCT, "package.json", "name", name, Confidence.HIGHEST);
nodeModule.addEvidence(EvidenceType.VENDOR, "package.json", "name", name, Confidence.HIGH);
if (!StringUtils.isBlank(version)) {
nodeModule.addEvidence(EvidenceType.VERSION, "package.json", "version", version, Confidence.HIGHEST);
nodeModule.setVersion(version);
}
if (dependency.getName() != null) {
nodeModule.addProjectReference(dependency.getName() + ": " + scope);
} else {
nodeModule.addProjectReference(dependency.getDisplayFileName() + ": " + scope);
}
nodeModule.setName(name);
//TODO - we can likely create a valid CPE as a low confidence guess using cpe:2.3:a:[name]_project:[name]:[version]
//(and add a targetSw of npm/node)
Identifier id;
try {
final PackageURL purl = PackageURLBuilder.aPackageURL().withType(StandardTypes.NPM)
.withName(name).withVersion(version).build();
id = new PurlIdentifier(purl, Confidence.HIGHEST);
} catch (MalformedPackageURLException ex) {
LOGGER.debug("Unable to generate Purl - using a generic identifier instead " + ex.getMessage());
id = new GenericIdentifier(String.format("npm:%s@%s", dependency.getName(), version), Confidence.HIGHEST);
}
nodeModule.addSoftwareIdentifier(id);
return nodeModule;
}
/**
* Processes a part of package.json (as defined by JsonArray) and update the
* specified dependency with relevant info.
*
* @param engine the dependency-check engine
* @param dependency the Dependency to update
* @param jsonArray the jsonArray to parse
* @param depType the dependency type
*/
protected void processPackage(Engine engine, Dependency dependency, JsonArray jsonArray, String depType) {
final JsonObjectBuilder builder = Json.createObjectBuilder();
jsonArray.getValuesAs(JsonString.class).forEach((str) -> builder.add(str.toString(), ""));
final JsonObject jsonObject = builder.build();
processPackage(engine, dependency, jsonObject, depType);
}
/**
* Processes a part of package.json (as defined by JsonObject) and update
* the specified dependency with relevant info.
*
* @param engine the dependency-check engine
* @param dependency the Dependency to update
* @param jsonObject the jsonObject to parse
* @param depType the dependency type
*/
protected void processPackage(Engine engine, Dependency dependency, JsonObject jsonObject, String depType) {
for (int i = 0; i < jsonObject.size(); i++) {
jsonObject.forEach((name, value) -> {
String version = "";
if (value != null && value.getValueType() == ValueType.STRING) {
version = ((JsonString) value).getString();
}
final Dependency existing = findDependency(engine, name, version);
if (existing == null) {
final Dependency nodeModule = createDependency(dependency, name, version, depType);
engine.addDependency(nodeModule);
} else {
existing.addProjectReference(dependency.getName() + ": " + depType);
}
});
}
}
/**
* Adds information to an evidence collection from the node json
* configuration.
*
* @param dep the dependency to add the evidence
* @param t the type of evidence to add
* @param json information from node.js
* @return the actual string set into evidence
* @param key the key to obtain the data from the json information
*/
private static String addToEvidence(Dependency dep, EvidenceType t, JsonObject json, String key) {
String evidenceStr = null;
if (json.containsKey(key)) {
final JsonValue value = json.get(key);
if (value instanceof JsonString) {
evidenceStr = ((JsonString) value).getString();
dep.addEvidence(t, PACKAGE_JSON, key, evidenceStr, Confidence.HIGHEST);
} else if (value instanceof JsonObject) {
final JsonObject jsonObject = (JsonObject) value;
for (final Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
final String property = entry.getKey();
final JsonValue subValue = entry.getValue();
if (subValue instanceof JsonString) {
evidenceStr = ((JsonString) subValue).getString();
dep.addEvidence(t, PACKAGE_JSON,
String.format("%s.%s", key, property),
evidenceStr,
Confidence.HIGHEST);
} else {
LOGGER.warn("JSON sub-value not string as expected: {}", subValue);
}
}
} else if (value instanceof JsonArray) {
final JsonArray jsonArray = (JsonArray) value;
jsonArray.forEach(entry -> {
if (entry instanceof JsonObject) {
((JsonObject) entry).keySet().forEach(item -> {
final JsonValue v = ((JsonObject) entry).get(item);
if (v instanceof JsonString) {
final String eStr = ((JsonString) v).getString();
dep.addEvidence(t, PACKAGE_JSON,
String.format("%s.%s", key, item),
eStr,
Confidence.HIGHEST);
}
});
}
});
} else {
LOGGER.warn("JSON value not string or JSON object as expected: {}", value);
}
}
return evidenceStr;
}
/**
* Locates the dependency from the list of dependencies that have been
* scanned by the engine.
*
* @param engine the dependency-check engine
* @param name the name of the dependency to find
* @param version the version of the dependency to find
* @return the identified dependency; otherwise null
*/
protected Dependency findDependency(Engine engine, String name, String version) {
for (Dependency d : engine.getDependencies()) {
if (NPM_DEPENDENCY_ECOSYSTEM.equals(d.getEcosystem()) && name.equals(d.getName()) && version != null && d.getVersion() != null) {
final String dependencyVersion = d.getVersion();
if (DependencyBundlingAnalyzer.npmVersionsMatch(version, dependencyVersion)) {
return d;
}
}
}
return null;
}
/**
* Collects evidence from the given JSON for the associated dependency.
*
* @param json the JSON that contains the evidence to collect
* @param dependency the dependency to add the evidence too
*/
public void gatherEvidence(final JsonObject json, Dependency dependency) {
String displayName = null;
if (json.containsKey("name")) {
final Object value = json.get("name");
if (value instanceof JsonString) {
final String valueString = ((JsonString) value).getString();
displayName = valueString;
dependency.setName(valueString);
dependency.setPackagePath(valueString);
dependency.addEvidence(EvidenceType.PRODUCT, PACKAGE_JSON, "name", valueString, Confidence.HIGHEST);
dependency.addEvidence(EvidenceType.VENDOR, PACKAGE_JSON, "name", valueString, Confidence.HIGHEST);
dependency.addEvidence(EvidenceType.VENDOR, PACKAGE_JSON, "name", valueString + "_project", Confidence.HIGHEST);
} else {
LOGGER.warn("JSON value not string as expected: {}", value);
}
}
//TODO - if we start doing CPE analysis on node - we need to exclude description as it creates too many FP
final String desc = addToEvidence(dependency, EvidenceType.VENDOR, json, "description");
dependency.setDescription(desc);
String vendor = addToEvidence(dependency, EvidenceType.VENDOR, json, "author");
if (vendor == null) {
vendor = addToEvidence(dependency, EvidenceType.VENDOR, json, "maintainers");
} else {
addToEvidence(dependency, EvidenceType.VENDOR, json, "maintainers");
}
addToEvidence(dependency, EvidenceType.VENDOR, json, "homepage");
addToEvidence(dependency, EvidenceType.VENDOR, json, "bugs");
final String version = addToEvidence(dependency, EvidenceType.VERSION, json, "version");
if (version != null) {
displayName = String.format("%s:%s", displayName, version);
dependency.setVersion(version);
dependency.setPackagePath(displayName);
Identifier id;
try {
final PackageURL purl = PackageURLBuilder.aPackageURL()
.withType(StandardTypes.NPM).withName(dependency.getName()).withVersion(version).build();
id = new PurlIdentifier(purl, Confidence.HIGHEST);
} catch (MalformedPackageURLException ex) {
LOGGER.debug("Unable to generate Purl - using a generic identifier instead " + ex.getMessage());
id = new GenericIdentifier(String.format("npm:%s:%s", dependency.getName(), version), Confidence.HIGHEST);
}
dependency.addSoftwareIdentifier(id);
}
if (displayName != null) {
dependency.setDisplayFileName(displayName);
dependency.setPackagePath(displayName);
} else {
LOGGER.warn("Unable to determine package name or version for {}", dependency.getActualFilePath());
if (vendor != null && !vendor.isEmpty()) {
dependency.setDisplayFileName(String.format("%s package.json", vendor));
}
}
// Adds the license if defined in package.json
if (json.containsKey("license")) {
final Object value = json.get("license");
if (value instanceof JsonString) {
dependency.setLicense(json.getString("license"));
} else if (value instanceof JsonArray) {
final JsonArray array = (JsonArray) value;
final StringBuilder sb = new StringBuilder();
boolean addComma = false;
for (int x = 0; x < array.size(); x++) {
if (!array.isNull(x)) {
if (addComma) {
sb.append(", ");
} else {
addComma = true;
}
if (ValueType.STRING == array.get(x).getValueType()) {
sb.append(array.getString(x));
} else {
final JsonObject lo = array.getJsonObject(x);
if (lo.containsKey("type") && !lo.isNull("type")
&& lo.containsKey("url") && !lo.isNull("url")) {
final String license = String.format("%s (%s)", lo.getString("type"), lo.getString("url"));
sb.append(license);
} else if (lo.containsKey("type") && !lo.isNull("type")) {
sb.append(lo.getString("type"));
} else if (lo.containsKey("url") && !lo.isNull("url")) {
sb.append(lo.getString("url"));
}
}
}
}
dependency.setLicense(sb.toString());
} else {
dependency.setLicense(json.getJsonObject("license").getString("type"));
}
}
}
/**
* Initializes the analyzer once before any analysis is performed.
*
* @param engine a reference to the dependency-check engine
* @throws InitializationException if there's an error during initialization
*/
@Override
protected void prepareFileTypeAnalyzer(Engine engine) throws InitializationException {
if (!isEnabled() || !getFilesMatched()) {
this.setEnabled(false);
return;
}
if (searcher == null) {
LOGGER.debug("Initializing {}", getName());
try {
searcher = new NodeAuditSearch(getSettings());
} catch (MalformedURLException ex) {
setEnabled(false);
throw new InitializationException("The configured URL to NPM Audit API is malformed", ex);
}
try {
final Settings settings = engine.getSettings();
final boolean nodeEnabled = settings.getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED);
if (!nodeEnabled) {
LOGGER.warn("The Node Package Analyzer has been disabled; the resulting report will only "
+ "contain the known vulnerable dependency - not a bill of materials for the node project.");
}
} catch (InvalidSettingException ex) {
throw new InitializationException("Unable to read configuration settings", ex);
}
}
}
/**
* Processes the advisories creating the appropriate dependency objects and
* adding the resulting vulnerabilities.
*
* @param advisories a collection of advisories from npm
* @param engine a reference to the analysis engine
* @param dependency a reference to the package-lock.json dependency
* @param dependencyMap a collection of module/version pairs obtained from
* the package-lock file - used in case the advisories do not include a
* version number
* @throws CpeValidationException thrown when a CPE cannot be created
*/
protected void processResults(final List<Advisory> advisories, Engine engine,
Dependency dependency, MultiValuedMap<String, String> dependencyMap)
throws CpeValidationException {
for (Advisory advisory : advisories) {
//Create a new vulnerability out of the advisory returned by nsp.
final Vulnerability vuln = new Vulnerability();
vuln.setDescription(advisory.getOverview());
vuln.setName(String.valueOf(advisory.getId()));
vuln.setUnscoredSeverity(advisory.getSeverity());
vuln.setSource(Vulnerability.Source.NPM);
vuln.addReference(
"Advisory " + advisory.getId() + ": " + advisory.getTitle(),
advisory.getReferences(),
null
);
//Create a single vulnerable software object - these do not use CPEs unlike the NVD.
final VulnerableSoftwareBuilder builder = new VulnerableSoftwareBuilder();
builder.part(Part.APPLICATION).product(advisory.getModuleName().replace(" ", "_"))
.version(advisory.getVulnerableVersions().replace(" ", ""));
final VulnerableSoftware vs = builder.build();
vuln.addVulnerableSoftware(vs);
String version = advisory.getVersion();
if (version == null && dependencyMap.containsKey(advisory.getModuleName())) {
version = determineVersionFromMap(advisory.getVulnerableVersions(), dependencyMap.get(advisory.getModuleName()));
}
final Dependency existing = findDependency(engine, advisory.getModuleName(), version);
if (existing == null) {
final Dependency nodeModule = createDependency(dependency, advisory.getModuleName(), version, "transitive");
nodeModule.addVulnerability(vuln);
engine.addDependency(nodeModule);
} else {
replaceOrAddVulnerability(existing, vuln);
}
}
}
/**
* Evaluates if the vulnerability is already present; if it is the
* vulnerability is not added.
*
* @param dependency a reference to the dependency being analyzed
* @param vuln the vulnerability to add
*/
protected void replaceOrAddVulnerability(Dependency dependency, Vulnerability vuln) {
boolean found = false;
for (Vulnerability existing : dependency.getVulnerabilities()) {
for (Reference ref : existing.getReferences()) {
if (ref.getName() != null
&& vuln.getSource().toString().equals("NPM")
&& ref.getName().equals("https://nodesecurity.io/advisories/" + vuln.getName())) {
found = true;
}
}
}
if (!found) {
dependency.addVulnerability(vuln);
}
}
/**
* Returns the node audit search utility.
*
* @return the node audit search utility
*/
protected NodeAuditSearch getSearcher() {
return searcher;
}
/**
* Give an NPM version range and a collection of versions, this method
* attempts to select a specific version from the collection that is in the
* range.
*
* @param versionRange the version range to evaluate
* @param availableVersions the collection of possible versions to select
* @return the selected range from the versionRange
*/
public static String determineVersionFromMap(String versionRange, Collection<String> availableVersions) {
if (availableVersions.size() == 1) {
return availableVersions.iterator().next();
}
for (String v : availableVersions) {
try {
final Semver version = new Semver(v);
if (version.satisfies(versionRange)) {
return v;
}
} catch (SemverException ex) {
LOGGER.debug("invalid semver: " + v);
}
}
return availableVersions.iterator().next();
}
}