View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.analyzer;
19  
20  import org.owasp.dependencycheck.Engine;
21  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
22  import org.owasp.dependencycheck.data.nuget.NugetPackage;
23  import org.owasp.dependencycheck.data.nuget.NuspecParseException;
24  import org.owasp.dependencycheck.data.nuget.NuspecParser;
25  import org.owasp.dependencycheck.data.nuget.XPathNuspecParser;
26  import org.owasp.dependencycheck.dependency.Confidence;
27  import org.owasp.dependencycheck.dependency.Dependency;
28  import org.owasp.dependencycheck.utils.FileFilterBuilder;
29  import org.owasp.dependencycheck.utils.Settings;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.io.FileFilter;
34  import java.io.FileInputStream;
35  import java.io.FileNotFoundException;
36  import java.io.IOException;
37  import org.owasp.dependencycheck.exception.InitializationException;
38  
39  /**
40   * Analyzer which will parse a Nuspec file to gather module information.
41   *
42   * @author colezlaw
43   */
44  public class NuspecAnalyzer extends AbstractFileTypeAnalyzer {
45  
46      /**
47       * The logger.
48       */
49      private static final Logger LOGGER = LoggerFactory.getLogger(NuspecAnalyzer.class);
50  
51      /**
52       * The name of the analyzer.
53       */
54      private static final String ANALYZER_NAME = "Nuspec Analyzer";
55  
56      /**
57       * The phase in which the analyzer runs.
58       */
59      private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
60  
61      /**
62       * The types of files on which this will work.
63       */
64      private static final String SUPPORTED_EXTENSIONS = "nuspec";
65  
66      /**
67       * Initializes the analyzer once before any analysis is performed.
68       *
69       * @throws InitializationException if there's an error during initialization
70       */
71      @Override
72      public void initializeFileTypeAnalyzer() throws InitializationException {
73      }
74  
75      /**
76       * Returns the analyzer's name.
77       *
78       * @return the name of the analyzer
79       */
80      @Override
81      public String getName() {
82          return ANALYZER_NAME;
83      }
84  
85      /**
86       * Returns the key used in the properties file to reference the analyzer's
87       * enabled property.
88       *
89       * @return the analyzer's enabled property setting key
90       */
91      @Override
92      protected String getAnalyzerEnabledSettingKey() {
93          return Settings.KEYS.ANALYZER_NUSPEC_ENABLED;
94      }
95  
96      /**
97       * Returns the analysis phase under which the analyzer runs.
98       *
99       * @return the phase under which this analyzer runs
100      */
101     @Override
102     public AnalysisPhase getAnalysisPhase() {
103         return ANALYSIS_PHASE;
104     }
105 
106     /**
107      * The file filter used to determine which files this analyzer supports.
108      */
109     private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(
110             SUPPORTED_EXTENSIONS).build();
111 
112     /**
113      * Returns the FileFilter
114      *
115      * @return the FileFilter
116      */
117     @Override
118     protected FileFilter getFileFilter() {
119         return FILTER;
120     }
121 
122     /**
123      * Performs the analysis.
124      *
125      * @param dependency the dependency to analyze
126      * @param engine the engine
127      * @throws AnalysisException when there's an exception during analysis
128      */
129     @Override
130     public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
131         LOGGER.debug("Checking Nuspec file {}", dependency);
132         try {
133             final NuspecParser parser = new XPathNuspecParser();
134             NugetPackage np = null;
135             FileInputStream fis = null;
136             try {
137                 fis = new FileInputStream(dependency.getActualFilePath());
138                 np = parser.parse(fis);
139             } catch (NuspecParseException ex) {
140                 throw new AnalysisException(ex);
141             } catch (FileNotFoundException ex) {
142                 throw new AnalysisException(ex);
143             } finally {
144                 if (fis != null) {
145                     try {
146                         fis.close();
147                     } catch (IOException e) {
148                         LOGGER.debug("Error closing input stream");
149                     }
150                 }
151             }
152 
153             if (np.getOwners() != null) {
154                 dependency.getVendorEvidence().addEvidence("nuspec", "owners", np.getOwners(), Confidence.HIGHEST);
155             }
156             dependency.getVendorEvidence().addEvidence("nuspec", "authors", np.getAuthors(), Confidence.HIGH);
157             dependency.getVersionEvidence().addEvidence("nuspec", "version", np.getVersion(), Confidence.HIGHEST);
158             dependency.getProductEvidence().addEvidence("nuspec", "id", np.getId(), Confidence.HIGHEST);
159             if (np.getTitle() != null) {
160                 dependency.getProductEvidence().addEvidence("nuspec", "title", np.getTitle(), Confidence.MEDIUM);
161             }
162         } catch (Throwable e) {
163             throw new AnalysisException(e);
164         }
165     }
166 }