| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JavaScriptAnalyzer |
|
| 2.0;2 |
| 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.BufferedReader; | |
| 21 | import java.io.File; | |
| 22 | import java.io.FileNotFoundException; | |
| 23 | import java.io.FileReader; | |
| 24 | import java.io.IOException; | |
| 25 | import java.util.Set; | |
| 26 | import java.util.logging.Level; | |
| 27 | import java.util.logging.Logger; | |
| 28 | import java.util.regex.Pattern; | |
| 29 | import org.owasp.dependencycheck.Engine; | |
| 30 | import org.owasp.dependencycheck.analyzer.exception.AnalysisException; | |
| 31 | import org.owasp.dependencycheck.dependency.Dependency; | |
| 32 | import org.owasp.dependencycheck.utils.Settings; | |
| 33 | ||
| 34 | /** | |
| 35 | * | |
| 36 | * Used to analyze a JavaScript file to gather information to aid in identification of a CPE identifier. | |
| 37 | * | |
| 38 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 39 | */ | |
| 40 | public class JavaScriptAnalyzer extends AbstractFileTypeAnalyzer { | |
| 41 | ||
| 42 | /** | |
| 43 | * The logger. | |
| 44 | */ | |
| 45 | 4 | private static final Logger LOGGER = Logger.getLogger(JavaScriptAnalyzer.class.getName()); |
| 46 | ||
| 47 | //<editor-fold defaultstate="collapsed" desc="All standard implementation details of Analyzer"> | |
| 48 | /** | |
| 49 | * The name of the analyzer. | |
| 50 | */ | |
| 51 | private static final String ANALYZER_NAME = "JavaScript Analyzer"; | |
| 52 | /** | |
| 53 | * The phase that this analyzer is intended to run in. | |
| 54 | */ | |
| 55 | 4 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION; |
| 56 | /** | |
| 57 | * The set of file extensions supported by this analyzer. | |
| 58 | */ | |
| 59 | 4 | private static final Set<String> EXTENSIONS = newHashSet("js"); |
| 60 | ||
| 61 | /** | |
| 62 | * Returns a list of file EXTENSIONS supported by this analyzer. | |
| 63 | * | |
| 64 | * @return a list of file EXTENSIONS supported by this analyzer. | |
| 65 | */ | |
| 66 | @Override | |
| 67 | public Set<String> getSupportedExtensions() { | |
| 68 | 8 | return EXTENSIONS; |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Returns the name of the analyzer. | |
| 73 | * | |
| 74 | * @return the name of the analyzer. | |
| 75 | */ | |
| 76 | @Override | |
| 77 | public String getName() { | |
| 78 | 4 | return ANALYZER_NAME; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Returns the phase that the analyzer is intended to run in. | |
| 83 | * | |
| 84 | * @return the phase that the analyzer is intended to run in. | |
| 85 | */ | |
| 86 | @Override | |
| 87 | public AnalysisPhase getAnalysisPhase() { | |
| 88 | 4 | return ANALYSIS_PHASE; |
| 89 | } | |
| 90 | //</editor-fold> | |
| 91 | /** | |
| 92 | * Returns the key used in the properties file to reference the analyzer's enabled property. | |
| 93 | * | |
| 94 | * @return the analyzer's enabled property setting key | |
| 95 | */ | |
| 96 | @Override | |
| 97 | protected String getAnalyzerEnabledSettingKey() { | |
| 98 | 20 | return Settings.KEYS.ANALYZER_JAVASCRIPT_ENABLED; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Loads a specified JavaScript file and collects information from the copyright information contained within. | |
| 103 | * | |
| 104 | * @param dependency the dependency to analyze. | |
| 105 | * @param engine the engine that is scanning the dependencies | |
| 106 | * @throws AnalysisException is thrown if there is an error reading the JavaScript file. | |
| 107 | */ | |
| 108 | @Override | |
| 109 | public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException { | |
| 110 | 0 | BufferedReader fin = null; |
| 111 | try { | |
| 112 | // /\*([^\*][^/]|[\r\n\f])+?\*/ | |
| 113 | 0 | final Pattern extractComments = Pattern.compile("(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)", Pattern.MULTILINE); |
| 114 | 0 | File file = dependency.getActualFile(); |
| 115 | 0 | fin = new BufferedReader(new FileReader(file)); |
| 116 | 0 | StringBuilder sb = new StringBuilder(2000); |
| 117 | String text; | |
| 118 | 0 | while ((text = fin.readLine()) != null) { |
| 119 | 0 | sb.append(text); |
| 120 | } | |
| 121 | 0 | } catch (FileNotFoundException ex) { |
| 122 | 0 | final String msg = String.format("Dependency file not found: '%s'", dependency.getActualFilePath()); |
| 123 | 0 | throw new AnalysisException(msg, ex); |
| 124 | 0 | } catch (IOException ex) { |
| 125 | 0 | LOGGER.log(Level.SEVERE, null, ex); |
| 126 | } finally { | |
| 127 | 0 | if (fin != null) { |
| 128 | try { | |
| 129 | 0 | fin.close(); |
| 130 | 0 | } catch (IOException ex) { |
| 131 | 0 | LOGGER.log(Level.FINEST, null, ex); |
| 132 | 0 | } |
| 133 | } | |
| 134 | } | |
| 135 | 0 | } |
| 136 | ||
| 137 | @Override | |
| 138 | protected void initializeFileTypeAnalyzer() throws Exception { | |
| 139 | ||
| 140 | 0 | } |
| 141 | } |