| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JavaScriptAnalyzer |
|
| 2.2;2.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 | ||
| 33 | /** | |
| 34 | * | |
| 35 | * Used to analyze a JavaScript file to gather information to aid in identification of a CPE identifier. | |
| 36 | * | |
| 37 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 38 | */ | |
| 39 | public class JavaScriptAnalyzer extends AbstractAnalyzer implements Analyzer { | |
| 40 | ||
| 41 | //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer"> | |
| 42 | /** | |
| 43 | * The name of the analyzer. | |
| 44 | */ | |
| 45 | private static final String ANALYZER_NAME = "JavaScript Analyzer"; | |
| 46 | /** | |
| 47 | * The phase that this analyzer is intended to run in. | |
| 48 | */ | |
| 49 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION; |
| 50 | /** | |
| 51 | * The set of file extensions supported by this analyzer. | |
| 52 | */ | |
| 53 | 1 | private static final Set<String> EXTENSIONS = newHashSet("js"); |
| 54 | ||
| 55 | /** | |
| 56 | * Returns a list of file EXTENSIONS supported by this analyzer. | |
| 57 | * | |
| 58 | * @return a list of file EXTENSIONS supported by this analyzer. | |
| 59 | */ | |
| 60 | @Override | |
| 61 | public Set<String> getSupportedExtensions() { | |
| 62 | 1 | return EXTENSIONS; |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Returns the name of the analyzer. | |
| 67 | * | |
| 68 | * @return the name of the analyzer. | |
| 69 | */ | |
| 70 | @Override | |
| 71 | public String getName() { | |
| 72 | 1 | return ANALYZER_NAME; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Returns whether or not this analyzer can process the given extension. | |
| 77 | * | |
| 78 | * @param extension the file extension to test for support. | |
| 79 | * @return whether or not the specified file extension is supported by this analyzer. | |
| 80 | */ | |
| 81 | @Override | |
| 82 | public boolean supportsExtension(String extension) { | |
| 83 | 1 | return EXTENSIONS.contains(extension); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * Returns the phase that the analyzer is intended to run in. | |
| 88 | * | |
| 89 | * @return the phase that the analyzer is intended to run in. | |
| 90 | */ | |
| 91 | @Override | |
| 92 | public AnalysisPhase getAnalysisPhase() { | |
| 93 | 1 | return ANALYSIS_PHASE; |
| 94 | } | |
| 95 | //</editor-fold> | |
| 96 | ||
| 97 | /** | |
| 98 | * Loads a specified JavaScript file and collects information from the copyright information contained within. | |
| 99 | * | |
| 100 | * @param dependency the dependency to analyze. | |
| 101 | * @param engine the engine that is scanning the dependencies | |
| 102 | * @throws AnalysisException is thrown if there is an error reading the JavaScript file. | |
| 103 | */ | |
| 104 | @Override | |
| 105 | public void analyze(Dependency dependency, Engine engine) throws AnalysisException { | |
| 106 | 0 | BufferedReader fin = null;; |
| 107 | try { | |
| 108 | // /\*([^\*][^/]|[\r\n\f])+?\*/ | |
| 109 | 0 | final Pattern extractComments = Pattern.compile("(/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)", Pattern.MULTILINE); |
| 110 | 0 | File file = dependency.getActualFile(); |
| 111 | 0 | fin = new BufferedReader(new FileReader(file)); |
| 112 | 0 | StringBuilder sb = new StringBuilder(2000); |
| 113 | String text; | |
| 114 | 0 | while ((text = fin.readLine()) != null) { |
| 115 | 0 | sb.append(text); |
| 116 | } | |
| 117 | 0 | } catch (FileNotFoundException ex) { |
| 118 | 0 | final String msg = String.format("Dependency file not found: '%s'", dependency.getActualFilePath()); |
| 119 | 0 | throw new AnalysisException(msg, ex); |
| 120 | 0 | } catch (IOException ex) { |
| 121 | 0 | Logger.getLogger(JavaScriptAnalyzer.class.getName()).log(Level.SEVERE, null, ex); |
| 122 | } finally { | |
| 123 | 0 | if (fin != null) { |
| 124 | try { | |
| 125 | 0 | fin.close(); |
| 126 | 0 | } catch (IOException ex) { |
| 127 | 0 | Logger.getLogger(JavaScriptAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 128 | 0 | } |
| 129 | } | |
| 130 | } | |
| 131 | 0 | } |
| 132 | } |