Class AbstractNpmAnalyzer
java.lang.Object
org.owasp.dependencycheck.analyzer.AbstractAnalyzer
org.owasp.dependencycheck.analyzer.AbstractFileTypeAnalyzer
org.owasp.dependencycheck.analyzer.AbstractNpmAnalyzer
- All Implemented Interfaces:
FileFilter, Analyzer, FileTypeAnalyzer
- Direct Known Subclasses:
NodeAuditAnalyzer, NodePackageAnalyzer, PnpmAuditAnalyzer, YarnAuditAnalyzer
An abstract NPM analyzer that contains common methods for concrete
implementations.
- Author:
- Steve Springett
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringA descriptor for the type of dependencies processed or added by this analyzer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanDetermines if the file can be analyzed by the analyzer.protected DependencycreateDependency(Dependency dependency, String name, String version, String scope) Construct a dependency object.static StringdetermineVersionFromMap(String versionRange, Collection<String> availableVersions) 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.protected DependencyfindDependency(Engine engine, String name, String version) Locates the dependency from the list of dependencies that have been scanned by the engine.voidgatherEvidence(jakarta.json.JsonObject json, Dependency dependency) Collects evidence from the given JSON for the associated dependency.protected NodeAuditSearchReturns the node audit search utility.protected voidprepareFileTypeAnalyzer(Engine engine) Initializes the analyzer once before any analysis is performed.protected voidprocessPackage(Engine engine, Dependency dependency, jakarta.json.JsonArray jsonArray, String depType) Processes a part of package.json (as defined by JsonArray) and update the specified dependency with relevant info.protected voidprocessPackage(Engine engine, Dependency dependency, jakarta.json.JsonObject jsonObject, String depType) Processes a part of package.json (as defined by JsonObject) and update the specified dependency with relevant info.protected voidprocessResults(List<Advisory> advisories, Engine engine, Dependency dependency, org.apache.commons.collections4.MultiValuedMap<String, String> dependencyMap) Processes the advisories creating the appropriate dependency objects and adding the resulting vulnerabilities.protected voidreplaceOrAddVulnerability(Dependency dependency, Vulnerability vuln) Evaluates if the vulnerability is already present; if it is the vulnerability is not added.static booleanshouldProcess(File pathname) Determines if the path contains "/node_modules/" or "/bower_components/" (i.e. it is a child module).Methods inherited from class AbstractFileTypeAnalyzer
getFileFilter, getFilesMatched, newHashSet, prepareAnalyzer, setFilesMatchedMethods inherited from class AbstractAnalyzer
analyze, analyzeDependency, close, closeAnalyzer, getAnalyzerEnabledSettingKey, getSettings, initialize, isEnabled, prepare, setEnabled, supportsParallelProcessingMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Analyzer
analyze, close, getAnalysisPhase, getName, initialize, isEnabled, prepare, supportsParallelProcessing
-
Field Details
-
NPM_DEPENDENCY_ECOSYSTEM
A descriptor for the type of dependencies processed or added by this analyzer.- See Also:
-
-
Constructor Details
-
AbstractNpmAnalyzer
public AbstractNpmAnalyzer()
-
-
Method Details
-
accept
Determines if the file can be analyzed by the analyzer.- Specified by:
acceptin interfaceFileFilter- Overrides:
acceptin classAbstractFileTypeAnalyzer- Parameters:
pathname- the path to the file- Returns:
- true if the file can be analyzed by the given analyzer; otherwise false
-
shouldProcess
Determines if the path contains "/node_modules/" or "/bower_components/" (i.e. it is a child module). This analyzer does not scan child modules.- Parameters:
pathname- the path to test- Returns:
trueif the path does not contain "/node_modules/" or "/bower_components/"- Throws:
AnalysisException- thrown if the canonical path cannot be obtained from the given file
-
createDependency
protected Dependency createDependency(Dependency dependency, String name, String version, String scope) Construct a dependency object.- Parameters:
dependency- the parent dependencyname- the name of the dependency to createversion- the version of the dependency to createscope- the scope of the dependency being created- Returns:
- the generated dependency
-
processPackage
protected void processPackage(Engine engine, Dependency dependency, jakarta.json.JsonArray jsonArray, String depType) Processes a part of package.json (as defined by JsonArray) and update the specified dependency with relevant info.- Parameters:
engine- the dependency-check enginedependency- the Dependency to updatejsonArray- the jsonArray to parsedepType- the dependency type
-
processPackage
protected void processPackage(Engine engine, Dependency dependency, jakarta.json.JsonObject jsonObject, String depType) Processes a part of package.json (as defined by JsonObject) and update the specified dependency with relevant info.- Parameters:
engine- the dependency-check enginedependency- the Dependency to updatejsonObject- the jsonObject to parsedepType- the dependency type
-
findDependency
Locates the dependency from the list of dependencies that have been scanned by the engine.- Parameters:
engine- the dependency-check enginename- the name of the dependency to findversion- the version of the dependency to find- Returns:
- the identified dependency; otherwise null
-
gatherEvidence
Collects evidence from the given JSON for the associated dependency.- Parameters:
json- the JSON that contains the evidence to collectdependency- the dependency to add the evidence too
-
prepareFileTypeAnalyzer
Initializes the analyzer once before any analysis is performed.- Specified by:
prepareFileTypeAnalyzerin classAbstractFileTypeAnalyzer- Parameters:
engine- a reference to the dependency-check engine- Throws:
InitializationException- if there's an error during initialization
-
processResults
protected void processResults(List<Advisory> advisories, Engine engine, Dependency dependency, org.apache.commons.collections4.MultiValuedMap<String, String> dependencyMap) throws us.springett.parsers.cpe.exceptions.CpeValidationExceptionProcesses the advisories creating the appropriate dependency objects and adding the resulting vulnerabilities.- Parameters:
advisories- a collection of advisories from npmengine- a reference to the analysis enginedependency- a reference to the package-lock.json dependencydependencyMap- a collection of module/version pairs obtained from the package-lock file - used in case the advisories do not include a version number- Throws:
us.springett.parsers.cpe.exceptions.CpeValidationException- thrown when a CPE cannot be created
-
replaceOrAddVulnerability
Evaluates if the vulnerability is already present; if it is the vulnerability is not added.- Parameters:
dependency- a reference to the dependency being analyzedvuln- the vulnerability to add
-
getSearcher
Returns the node audit search utility.- Returns:
- the node audit search utility
-
determineVersionFromMap
public static String determineVersionFromMap(String versionRange, Collection<String> availableVersions) 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.- Parameters:
versionRange- the version range to evaluateavailableVersions- the collection of possible versions to select- Returns:
- the selected range from the versionRange
-