Coverage Report - org.owasp.dependencycheck.analyzer.AssemblyAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
AssemblyAnalyzer
66%
82/123
41%
26/62
6.875
 
 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) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.analyzer;
 19  
 
 20  
 import java.io.BufferedReader;
 21  
 import java.io.File;
 22  
 import java.io.FileOutputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.io.InputStreamReader;
 26  
 import java.util.ArrayList;
 27  
 import java.util.List;
 28  
 import java.util.Set;
 29  
 import java.util.logging.Level;
 30  
 import java.util.logging.Logger;
 31  
 import javax.xml.parsers.DocumentBuilder;
 32  
 import javax.xml.parsers.DocumentBuilderFactory;
 33  
 import javax.xml.xpath.XPath;
 34  
 import javax.xml.xpath.XPathExpressionException;
 35  
 import javax.xml.xpath.XPathFactory;
 36  
 import org.owasp.dependencycheck.Engine;
 37  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 38  
 import org.owasp.dependencycheck.dependency.Confidence;
 39  
 import org.owasp.dependencycheck.dependency.Dependency;
 40  
 import org.owasp.dependencycheck.dependency.Evidence;
 41  
 import org.owasp.dependencycheck.utils.Settings;
 42  
 import org.w3c.dom.Document;
 43  
 import org.xml.sax.SAXException;
 44  
 
 45  
 /**
 46  
  * Analyzer for getting company, product, and version information from a .NET assembly.
 47  
  *
 48  
  * @author colezlaw
 49  
  *
 50  
  */
 51  
 public class AssemblyAnalyzer extends AbstractFileTypeAnalyzer {
 52  
 
 53  
     /**
 54  
      * The analyzer name
 55  
      */
 56  
     private static final String ANALYZER_NAME = "Assembly Analyzer";
 57  
     /**
 58  
      * The analysis phase
 59  
      */
 60  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 61  
     /**
 62  
      * The list of supported extensions
 63  
      */
 64  1
     private static final Set<String> SUPPORTED_EXTENSIONS = newHashSet("dll", "exe");
 65  
     /**
 66  
      * The temp value for GrokAssembly.exe
 67  
      */
 68  
     private File grokAssemblyExe = null;
 69  
     /**
 70  
      * The DocumentBuilder for parsing the XML
 71  
      */
 72  
     private DocumentBuilder builder;
 73  
     /**
 74  
      * Logger
 75  
      */
 76  1
     private static final Logger LOGGER = Logger.getLogger(AssemblyAnalyzer.class.getName(), "dependencycheck-resources");
 77  
 
 78  
     /**
 79  
      * Builds the beginnings of a List for ProcessBuilder
 80  
      *
 81  
      * @return the list of arguments to begin populating the ProcessBuilder
 82  
      */
 83  
     private List<String> buildArgumentList() {
 84  
         // Use file.separator as a wild guess as to whether this is Windows
 85  8
         final List<String> args = new ArrayList<String>();
 86  8
         if (!"\\".equals(System.getProperty("file.separator"))) {
 87  0
             if (Settings.getString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH) != null) {
 88  0
                 args.add(Settings.getString(Settings.KEYS.ANALYZER_ASSEMBLY_MONO_PATH));
 89  
             } else {
 90  0
                 args.add("mono");
 91  
             }
 92  
         }
 93  8
         args.add(grokAssemblyExe.getPath());
 94  
 
 95  8
         return args;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Performs the analysis on a single Dependency.
 100  
      *
 101  
      * @param dependency the dependency to analyze
 102  
      * @param engine the engine to perform the analysis under
 103  
      * @throws AnalysisException if anything goes sideways
 104  
      */
 105  
     @Override
 106  
     public void analyzeFileType(Dependency dependency, Engine engine)
 107  
             throws AnalysisException {
 108  3
         if (grokAssemblyExe == null) {
 109  0
             LOGGER.warning("analyzer.AssemblyAnalyzer.notdeployed");
 110  0
             return;
 111  
         }
 112  
 
 113  3
         final List<String> args = buildArgumentList();
 114  3
         args.add(dependency.getActualFilePath());
 115  3
         final ProcessBuilder pb = new ProcessBuilder(args);
 116  3
         BufferedReader rdr = null;
 117  3
         Document doc = null;
 118  
         try {
 119  3
             final Process proc = pb.start();
 120  
             // Try evacuating the error stream
 121  3
             rdr = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "UTF-8"));
 122  3
             String line = null;
 123  3
             while (rdr.ready() && (line = rdr.readLine()) != null) {
 124  0
                 LOGGER.log(Level.WARNING, "analyzer.AssemblyAnalyzer.grokassembly.stderr", line);
 125  
             }
 126  3
             int rc = 0;
 127  3
             doc = builder.parse(proc.getInputStream());
 128  
 
 129  
             try {
 130  3
                 rc = proc.waitFor();
 131  0
             } catch (InterruptedException ie) {
 132  
                 return;
 133  3
             }
 134  3
             if (rc == 3) {
 135  0
                 LOGGER.log(Level.FINE, "analyzer.AssemblyAnalyzer.notassembly", dependency.getActualFilePath());
 136  
                 return;
 137  3
             } else if (rc != 0) {
 138  1
                 LOGGER.log(Level.WARNING, "analyzer.AssemblyAnalyzer.grokassembly.rc", rc);
 139  
             }
 140  
 
 141  3
             final XPath xpath = XPathFactory.newInstance().newXPath();
 142  
 
 143  
             // First, see if there was an error
 144  3
             final String error = xpath.evaluate("/assembly/error", doc);
 145  3
             if (error != null && !"".equals(error)) {
 146  1
                 throw new AnalysisException(error);
 147  
             }
 148  
 
 149  2
             final String version = xpath.evaluate("/assembly/version", doc);
 150  2
             if (version != null) {
 151  2
                 dependency.getVersionEvidence().addEvidence(new Evidence("grokassembly", "version",
 152  
                         version, Confidence.HIGHEST));
 153  
             }
 154  
 
 155  2
             final String vendor = xpath.evaluate("/assembly/company", doc);
 156  2
             if (vendor != null) {
 157  2
                 dependency.getVendorEvidence().addEvidence(new Evidence("grokassembly", "vendor",
 158  
                         vendor, Confidence.HIGH));
 159  
             }
 160  
 
 161  2
             final String product = xpath.evaluate("/assembly/product", doc);
 162  2
             if (product != null) {
 163  2
                 dependency.getProductEvidence().addEvidence(new Evidence("grokassembly", "product",
 164  
                         product, Confidence.HIGH));
 165  
             }
 166  
 
 167  0
         } catch (IOException ioe) {
 168  0
             throw new AnalysisException(ioe);
 169  0
         } catch (SAXException saxe) {
 170  0
             throw new AnalysisException("Couldn't parse GrokAssembly result", saxe);
 171  0
         } catch (XPathExpressionException xpe) {
 172  
             // This shouldn't happen
 173  0
             throw new AnalysisException(xpe);
 174  
         } finally {
 175  3
             if (rdr != null) {
 176  
                 try {
 177  3
                     rdr.close();
 178  0
                 } catch (IOException ex) {
 179  0
                     LOGGER.log(Level.FINEST, "ignore", ex);
 180  4
                 }
 181  
             }
 182  
         }
 183  2
     }
 184  
 
 185  
     /**
 186  
      * Initialize the analyzer. In this case, extract GrokAssembly.exe to a temporary location.
 187  
      *
 188  
      * @throws Exception if anything goes wrong
 189  
      */
 190  
     @Override
 191  
     public void initializeFileTypeAnalyzer() throws Exception {
 192  5
         final File tempFile = File.createTempFile("GKA", ".exe", Settings.getTempDirectory());
 193  5
         FileOutputStream fos = null;
 194  5
         InputStream is = null;
 195  
         try {
 196  5
             fos = new FileOutputStream(tempFile);
 197  5
             is = AssemblyAnalyzer.class.getClassLoader().getResourceAsStream("GrokAssembly.exe");
 198  5
             final byte[] buff = new byte[4096];
 199  5
             int bread = -1;
 200  15
             while ((bread = is.read(buff)) >= 0) {
 201  10
                 fos.write(buff, 0, bread);
 202  
             }
 203  5
             grokAssemblyExe = tempFile;
 204  
             // Set the temp file to get deleted when we're done
 205  5
             grokAssemblyExe.deleteOnExit();
 206  5
             LOGGER.log(Level.FINE, "analyzer.AssemblyAnalyzer.grokassembly.deployed", grokAssemblyExe.getPath());
 207  0
         } catch (IOException ioe) {
 208  0
             this.setEnabled(false);
 209  0
             LOGGER.log(Level.WARNING, "analyzer.AssemblyAnalyzer.grokassembly.notdeployed", ioe.getMessage());
 210  0
             throw new AnalysisException("Could not extract GrokAssembly.exe", ioe);
 211  
         } finally {
 212  5
             if (fos != null) {
 213  
                 try {
 214  5
                     fos.close();
 215  0
                 } catch (Throwable e) {
 216  0
                     LOGGER.fine("Error closing output stream");
 217  5
                 }
 218  
             }
 219  5
             if (is != null) {
 220  
                 try {
 221  5
                     is.close();
 222  0
                 } catch (Throwable e) {
 223  0
                     LOGGER.fine("Error closing input stream");
 224  5
                 }
 225  
             }
 226  
         }
 227  
 
 228  
         // Now, need to see if GrokAssembly actually runs from this location.
 229  5
         final List<String> args = buildArgumentList();
 230  5
         BufferedReader rdr = null;
 231  
         try {
 232  5
             final ProcessBuilder pb = new ProcessBuilder(args);
 233  5
             final Process p = pb.start();
 234  
             // Try evacuating the error stream
 235  5
             rdr = new BufferedReader(new InputStreamReader(p.getErrorStream(), "UTF-8"));
 236  5
             while (rdr.ready() && rdr.readLine() != null) {
 237  
                 // We expect this to complain
 238  
             }
 239  5
             final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(p.getInputStream());
 240  5
             final XPath xpath = XPathFactory.newInstance().newXPath();
 241  5
             final String error = xpath.evaluate("/assembly/error", doc);
 242  5
             if (p.waitFor() != 1 || error == null || "".equals(error)) {
 243  0
                 LOGGER.warning("An error occurred with the .NET AssemblyAnalyzer, please see the log for more details.");
 244  0
                 LOGGER.fine("GrokAssembly.exe is not working properly");
 245  0
                 grokAssemblyExe = null;
 246  0
                 this.setEnabled(false);
 247  0
                 throw new AnalysisException("Could not execute .NET AssemblyAnalyzer");
 248  
             }
 249  0
         } catch (Throwable e) {
 250  0
             if (e instanceof AnalysisException) {
 251  0
                 throw (AnalysisException) e;
 252  
             } else {
 253  0
                 LOGGER.warning("analyzer.AssemblyAnalyzer.grokassembly.initialization.failed");
 254  0
                 LOGGER.log(Level.FINE, "analyzer.AssemblyAnalyzer.grokassembly.initialization.message", e.getMessage());
 255  0
                 this.setEnabled(false);
 256  0
                 throw new AnalysisException("An error occured with the .NET AssemblyAnalyzer", e);
 257  
             }
 258  
         } finally {
 259  5
             if (rdr != null) {
 260  
                 try {
 261  5
                     rdr.close();
 262  0
                 } catch (IOException ex) {
 263  0
                     LOGGER.log(Level.FINEST, "ignore", ex);
 264  5
                 }
 265  
             }
 266  
         }
 267  5
         builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 268  5
     }
 269  
 
 270  
     @Override
 271  
     public void close() throws Exception {
 272  6
         super.close();
 273  
         try {
 274  6
             if (grokAssemblyExe != null && !grokAssemblyExe.delete()) {
 275  0
                 grokAssemblyExe.deleteOnExit();
 276  
             }
 277  0
         } catch (SecurityException se) {
 278  0
             LOGGER.fine("analyzer.AssemblyAnalyzer.grokassembly.notdeleted");
 279  6
         }
 280  6
     }
 281  
 
 282  
     /**
 283  
      * Gets the set of extensions supported by this analyzer.
 284  
      *
 285  
      * @return the list of supported extensions
 286  
      */
 287  
     @Override
 288  
     public Set<String> getSupportedExtensions() {
 289  853
         return SUPPORTED_EXTENSIONS;
 290  
     }
 291  
 
 292  
     /**
 293  
      * Gets this analyzer's name.
 294  
      *
 295  
      * @return the analyzer name
 296  
      */
 297  
     @Override
 298  
     public String getName() {
 299  5
         return ANALYZER_NAME;
 300  
     }
 301  
 
 302  
     /**
 303  
      * Returns the phase this analyzer runs under.
 304  
      *
 305  
      * @return the phase this runs under
 306  
      */
 307  
     @Override
 308  
     public AnalysisPhase getAnalysisPhase() {
 309  1
         return ANALYSIS_PHASE;
 310  
     }
 311  
 
 312  
     /**
 313  
      * Returns the key used in the properties file to reference the analyzer's enabled property.
 314  
      *
 315  
      * @return the analyzer's enabled property setting key
 316  
      */
 317  
     @Override
 318  
     protected String getAnalyzerEnabledSettingKey() {
 319  7
         return Settings.KEYS.ANALYZER_ASSEMBLY_ENABLED;
 320  
     }
 321  
 }