Coverage Report - org.owasp.dependencycheck.analyzer.NuspecAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
NuspecAnalyzer
22%
8/35
0%
0/6
2.667
 
 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 java.io.FileInputStream;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import java.util.Set;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import org.owasp.dependencycheck.Engine;
 27  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 28  
 import org.owasp.dependencycheck.data.nuget.NugetPackage;
 29  
 import org.owasp.dependencycheck.data.nuget.NuspecParseException;
 30  
 import org.owasp.dependencycheck.data.nuget.NuspecParser;
 31  
 import org.owasp.dependencycheck.data.nuget.XPathNuspecParser;
 32  
 import org.owasp.dependencycheck.dependency.Confidence;
 33  
 import org.owasp.dependencycheck.dependency.Dependency;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 
 36  
 /**
 37  
  * Analyzer which will parse a Nuspec file to gather module information.
 38  
  *
 39  
  * @author colezlaw
 40  
  */
 41  6
 public class NuspecAnalyzer extends AbstractFileTypeAnalyzer {
 42  
 
 43  
     /**
 44  
      * The logger.
 45  
      */
 46  1
     private static final Logger LOGGER = Logger.getLogger(NuspecAnalyzer.class.getName());
 47  
 
 48  
     /**
 49  
      * The name of the analyzer.
 50  
      */
 51  
     private static final String ANALYZER_NAME = "Nuspec Analyzer";
 52  
 
 53  
     /**
 54  
      * The phase in which the analyzer runs.
 55  
      */
 56  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 57  
 
 58  
     /**
 59  
      * The types of files on which this will work.
 60  
      */
 61  1
     private static final Set<String> SUPPORTED_EXTENSIONS = newHashSet("nuspec");
 62  
 
 63  
     /**
 64  
      * Initializes the analyzer once before any analysis is performed.
 65  
      *
 66  
      * @throws Exception if there's an error during initialization
 67  
      */
 68  
     @Override
 69  
     public void initializeFileTypeAnalyzer() throws Exception {
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Returns the analyzer's name.
 74  
      *
 75  
      * @return the name of the analyzer
 76  
      */
 77  
     @Override
 78  
     public String getName() {
 79  5
         return ANALYZER_NAME;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Returns the key used in the properties file to reference the analyzer's enabled property.
 84  
      *
 85  
      * @return the analyzer's enabled property setting key
 86  
      */
 87  
     @Override
 88  
     protected String getAnalyzerEnabledSettingKey() {
 89  6
         return Settings.KEYS.ANALYZER_NUSPEC_ENABLED;
 90  
     }
 91  
 
 92  
     /**
 93  
      * Returns the analysis phase under which the analyzer runs.
 94  
      *
 95  
      * @return the phase under which this analyzer runs
 96  
      */
 97  
     @Override
 98  
     public AnalysisPhase getAnalysisPhase() {
 99  2
         return ANALYSIS_PHASE;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Returns the extensions for which this Analyzer runs.
 104  
      *
 105  
      * @return the extensions for which this Analyzer runs
 106  
      */
 107  
     @Override
 108  
     public Set<String> getSupportedExtensions() {
 109  852
         return SUPPORTED_EXTENSIONS;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Performs the analysis.
 114  
      *
 115  
      * @param dependency the dependency to analyze
 116  
      * @param engine the engine
 117  
      * @throws AnalysisException when there's an exception during analysis
 118  
      */
 119  
     @Override
 120  
     public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException {
 121  0
         LOGGER.log(Level.FINE, "Checking Nuspec file {0}", dependency.toString());
 122  
         try {
 123  0
             final NuspecParser parser = new XPathNuspecParser();
 124  0
             NugetPackage np = null;
 125  0
             FileInputStream fis = null;
 126  
             try {
 127  0
                 fis = new FileInputStream(dependency.getActualFilePath());
 128  0
                 np = parser.parse(fis);
 129  0
             } catch (NuspecParseException ex) {
 130  0
                 throw new AnalysisException(ex);
 131  0
             } catch (FileNotFoundException ex) {
 132  0
                 throw new AnalysisException(ex);
 133  
             } finally {
 134  0
                 if (fis != null) {
 135  
                     try {
 136  0
                         fis.close();
 137  0
                     } catch (IOException e) {
 138  0
                         LOGGER.fine("Error closing input stream");
 139  0
                     }
 140  
                 }
 141  
             }
 142  
 
 143  0
             if (np.getOwners() != null) {
 144  0
                 dependency.getVendorEvidence().addEvidence("nuspec", "owners", np.getOwners(), Confidence.HIGHEST);
 145  
             }
 146  0
             dependency.getVendorEvidence().addEvidence("nuspec", "authors", np.getAuthors(), Confidence.HIGH);
 147  0
             dependency.getVersionEvidence().addEvidence("nuspec", "version", np.getVersion(), Confidence.HIGHEST);
 148  0
             dependency.getProductEvidence().addEvidence("nuspec", "id", np.getId(), Confidence.HIGHEST);
 149  0
             if (np.getTitle() != null) {
 150  0
                 dependency.getProductEvidence().addEvidence("nuspec", "title", np.getTitle(), Confidence.MEDIUM);
 151  
             }
 152  0
         } catch (Throwable e) {
 153  0
             throw new AnalysisException(e);
 154  0
         }
 155  0
     }
 156  
 }