Coverage Report - org.owasp.dependencycheck.analyzer.ComposerLockAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
ComposerLockAnalyzer
83%
31/37
75%
3/4
1.833
 
 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) 2015 The OWASP Foundation. 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.composer.ComposerDependency;
 23  
 import org.owasp.dependencycheck.data.composer.ComposerException;
 24  
 import org.owasp.dependencycheck.data.composer.ComposerLockParser;
 25  
 import org.owasp.dependencycheck.dependency.Confidence;
 26  
 import org.owasp.dependencycheck.dependency.Dependency;
 27  
 import org.owasp.dependencycheck.utils.Checksum;
 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.nio.charset.Charset;
 37  
 import java.security.MessageDigest;
 38  
 
 39  
 /**
 40  
  * Used to analyze a composer.lock file for a composer PHP app.
 41  
  *
 42  
  * @author colezlaw
 43  
  */
 44  
 @Experimental
 45  18
 public class ComposerLockAnalyzer extends AbstractFileTypeAnalyzer {
 46  
 
 47  
     /**
 48  
      * The logger.
 49  
      */
 50  2
     private static final Logger LOGGER = LoggerFactory.getLogger(ComposerLockAnalyzer.class);
 51  
 
 52  
     /**
 53  
      * The analyzer name.
 54  
      */
 55  
     private static final String ANALYZER_NAME = "Composer.lock analyzer";
 56  
 
 57  
     /**
 58  
      * composer.json.
 59  
      */
 60  
     private static final String COMPOSER_LOCK = "composer.lock";
 61  
 
 62  
     /**
 63  
      * The FileFilter.
 64  
      */
 65  2
     private static final FileFilter FILE_FILTER = FileFilterBuilder.newInstance().addFilenames(COMPOSER_LOCK).build();
 66  
 
 67  
     /**
 68  
      * Returns the FileFilter.
 69  
      *
 70  
      * @return the FileFilter
 71  
      */
 72  
     @Override
 73  
     protected FileFilter getFileFilter() {
 74  1720
         return FILE_FILTER;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Initializes the analyzer.
 79  
      *
 80  
      * @throws Exception thrown if an exception occurs getting an instance of SHA1
 81  
      */
 82  
     @Override
 83  
     protected void initializeFileTypeAnalyzer() throws Exception {
 84  6
         sha1 = MessageDigest.getInstance("SHA1");
 85  6
     }
 86  
 
 87  
     /**
 88  
      * The MessageDigest for calculating a new digest for the new dependencies added.
 89  
      */
 90  18
     private MessageDigest sha1 = null;
 91  
 
 92  
     /**
 93  
      * Entry point for the analyzer.
 94  
      *
 95  
      * @param dependency the dependency to analyze
 96  
      * @param engine the engine scanning
 97  
      * @throws AnalysisException if there's a failure during analysis
 98  
      */
 99  
     @Override
 100  
     protected void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException {
 101  2
         FileInputStream fis = null;
 102  
         try {
 103  2
             fis = new FileInputStream(dependency.getActualFile());
 104  2
             final ComposerLockParser clp = new ComposerLockParser(fis);
 105  2
             LOGGER.info("Checking composer.lock file {}", dependency.getActualFilePath());
 106  2
             clp.process();
 107  2
             for (ComposerDependency dep : clp.getDependencies()) {
 108  60
                 final Dependency d = new Dependency(dependency.getActualFile());
 109  60
                 d.setDisplayFileName(String.format("%s:%s/%s", dependency.getDisplayFileName(), dep.getGroup(), dep.getProject()));
 110  60
                 final String filePath = String.format("%s:%s/%s", dependency.getFilePath(), dep.getGroup(), dep.getProject());
 111  60
                 d.setFilePath(filePath);
 112  60
                 d.setSha1sum(Checksum.getHex(sha1.digest(filePath.getBytes(Charset.defaultCharset()))));
 113  60
                 d.getVendorEvidence().addEvidence(COMPOSER_LOCK, "vendor", dep.getGroup(), Confidence.HIGHEST);
 114  60
                 d.getProductEvidence().addEvidence(COMPOSER_LOCK, "product", dep.getProject(), Confidence.HIGHEST);
 115  60
                 d.getVersionEvidence().addEvidence(COMPOSER_LOCK, "version", dep.getVersion(), Confidence.HIGHEST);
 116  60
                 LOGGER.info("Adding dependency {}", d);
 117  60
                 engine.getDependencies().add(d);
 118  60
             }
 119  0
         } catch (FileNotFoundException fnfe) {
 120  0
             LOGGER.warn("Error opening dependency {}", dependency.getActualFilePath());
 121  0
         } catch (ComposerException ce) {
 122  0
             LOGGER.warn("Error parsing composer.json {}", dependency.getActualFilePath(), ce);
 123  
         } finally {
 124  2
             if (fis != null) {
 125  
                 try {
 126  2
                     fis.close();
 127  0
                 } catch (Exception e) {
 128  0
                     LOGGER.debug("Unable to close file", e);
 129  2
                 }
 130  
             }
 131  
         }
 132  2
     }
 133  
 
 134  
     /**
 135  
      * Gets the key to determine whether the analyzer is enabled.
 136  
      *
 137  
      * @return the key specifying whether the analyzer is enabled
 138  
      */
 139  
     @Override
 140  
     protected String getAnalyzerEnabledSettingKey() {
 141  18
         return Settings.KEYS.ANALYZER_COMPOSER_LOCK_ENABLED;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Returns the analyzer's name.
 146  
      *
 147  
      * @return the analyzer's name
 148  
      */
 149  
     @Override
 150  
     public String getName() {
 151  30
         return ANALYZER_NAME;
 152  
     }
 153  
 
 154  
     /**
 155  
      * Returns the phase this analyzer should run under.
 156  
      *
 157  
      * @return the analysis phase
 158  
      */
 159  
     @Override
 160  
     public AnalysisPhase getAnalysisPhase() {
 161  8
         return AnalysisPhase.INFORMATION_COLLECTION;
 162  
     }
 163  
 }