| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.analyzer; |
| 20 | |
|
| 21 | |
import java.io.BufferedInputStream; |
| 22 | |
import java.io.BufferedOutputStream; |
| 23 | |
import java.io.File; |
| 24 | |
import java.io.FileInputStream; |
| 25 | |
import java.io.FileNotFoundException; |
| 26 | |
import java.io.FileOutputStream; |
| 27 | |
import java.io.IOException; |
| 28 | |
import java.util.ArrayList; |
| 29 | |
import java.util.Collections; |
| 30 | |
import java.util.HashSet; |
| 31 | |
import java.util.List; |
| 32 | |
import java.util.Set; |
| 33 | |
import java.util.logging.Level; |
| 34 | |
import java.util.logging.Logger; |
| 35 | |
import java.util.zip.ZipEntry; |
| 36 | |
import java.util.zip.ZipInputStream; |
| 37 | |
import org.h2.store.fs.FileUtils; |
| 38 | |
import org.owasp.dependencycheck.Engine; |
| 39 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 40 | |
import org.owasp.dependencycheck.utils.Settings; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 21 | public class ArchiveAnalyzer extends AbstractAnalyzer implements Analyzer { |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private static final int BUFFER_SIZE = 4096; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 3 | private static int dirCount = 0; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 21 | private File tempFileLocation = null; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 3 | private static final int MAX_SCAN_DEPTH = Settings.getInt("archive.scan.depth", 3); |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 21 | private int scanDepth = 0; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
private static final String ANALYZER_NAME = "Archive Analyzer"; |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | 3 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INITIAL; |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | 3 | private static final Set<String> EXTENSIONS = newHashSet("zip", "ear", "war"); |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public Set<String> getSupportedExtensions() { |
| 102 | 17472 | return EXTENSIONS; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public String getName() { |
| 111 | 3 | return ANALYZER_NAME; |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public boolean supportsExtension(String extension) { |
| 122 | 17649 | return EXTENSIONS.contains(extension); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
public AnalysisPhase getAnalysisPhase() { |
| 131 | 6 | return ANALYSIS_PHASE; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
@Override |
| 142 | |
public void initialize() throws Exception { |
| 143 | 6 | final String tmpDir = Settings.getString(Settings.KEYS.TEMP_DIRECTORY, System.getProperty("java.io.tmpdir")); |
| 144 | 6 | final File baseDir = new File(tmpDir); |
| 145 | 6 | tempFileLocation = File.createTempFile("check", "tmp", baseDir); |
| 146 | 6 | if (!tempFileLocation.delete()) { |
| 147 | 0 | throw new AnalysisException("Unable to delete temporary file '" + tempFileLocation.getAbsolutePath() + "'."); |
| 148 | |
} |
| 149 | 6 | if (!tempFileLocation.mkdirs()) { |
| 150 | 0 | throw new AnalysisException("Unable to create directory '" + tempFileLocation.getAbsolutePath() + "'."); |
| 151 | |
} |
| 152 | 6 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
@Override |
| 161 | |
public void close() throws Exception { |
| 162 | 6 | if (tempFileLocation != null && tempFileLocation.exists()) { |
| 163 | 6 | FileUtils.deleteRecursive(tempFileLocation.getAbsolutePath(), true); |
| 164 | |
} |
| 165 | 6 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
@Override |
| 177 | |
public void analyze(Dependency dependency, Engine engine) throws AnalysisException { |
| 178 | 3 | final File f = new File(dependency.getActualFilePath()); |
| 179 | 3 | final File tmpDir = getNextTempDirectory(); |
| 180 | 3 | extractFiles(f, tmpDir, engine); |
| 181 | |
|
| 182 | |
|
| 183 | 3 | final List<Dependency> dependencies = new ArrayList<Dependency>(engine.getDependencies()); |
| 184 | 3 | engine.scan(tmpDir); |
| 185 | 3 | final List<Dependency> newDependencies = engine.getDependencies(); |
| 186 | 3 | if (dependencies.size() != newDependencies.size()) { |
| 187 | |
|
| 188 | 3 | final Set<Dependency> dependencySet = new HashSet<Dependency>(); |
| 189 | 3 | dependencySet.addAll(newDependencies); |
| 190 | 3 | dependencySet.removeAll(dependencies); |
| 191 | |
|
| 192 | 3 | for (Dependency d : dependencySet) { |
| 193 | |
|
| 194 | 177 | final String displayPath = String.format("%s%s", |
| 195 | |
dependency.getFilePath(), |
| 196 | |
d.getActualFilePath().substring(tmpDir.getAbsolutePath().length())); |
| 197 | 177 | final String displayName = String.format("%s%s%s", |
| 198 | |
dependency.getFileName(), |
| 199 | |
File.separator, |
| 200 | |
d.getFileName()); |
| 201 | 177 | d.setFilePath(displayPath); |
| 202 | 177 | d.setFileName(displayName); |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | 177 | if (this.supportsExtension(d.getFileExtension()) && scanDepth < MAX_SCAN_DEPTH) { |
| 208 | 0 | scanDepth += 1; |
| 209 | 0 | analyze(d, engine); |
| 210 | 0 | scanDepth -= 1; |
| 211 | |
} |
| 212 | 177 | } |
| 213 | |
} |
| 214 | 3 | Collections.sort(engine.getDependencies()); |
| 215 | 3 | } |
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
private File getNextTempDirectory() throws AnalysisException { |
| 224 | 3 | dirCount += 1; |
| 225 | 3 | final File directory = new File(tempFileLocation, String.valueOf(dirCount)); |
| 226 | 3 | if (!directory.mkdirs()) { |
| 227 | 0 | throw new AnalysisException("Unable to create temp directory '" + directory.getAbsolutePath() + "'."); |
| 228 | |
} |
| 229 | 3 | return directory; |
| 230 | |
} |
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
private void extractFiles(File archive, File extractTo, Engine engine) throws AnalysisException { |
| 241 | 3 | if (archive == null || extractTo == null) { |
| 242 | 0 | return; |
| 243 | |
} |
| 244 | |
|
| 245 | 3 | FileInputStream fis = null; |
| 246 | 3 | ZipInputStream zis = null; |
| 247 | |
|
| 248 | |
try { |
| 249 | 3 | fis = new FileInputStream(archive); |
| 250 | 0 | } catch (FileNotFoundException ex) { |
| 251 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.INFO, null, ex); |
| 252 | 0 | throw new AnalysisException("Archive file was not found.", ex); |
| 253 | 3 | } |
| 254 | 3 | zis = new ZipInputStream(new BufferedInputStream(fis)); |
| 255 | |
ZipEntry entry; |
| 256 | |
try { |
| 257 | 18567 | while ((entry = zis.getNextEntry()) != null) { |
| 258 | 18564 | if (entry.isDirectory()) { |
| 259 | 873 | final File d = new File(extractTo, entry.getName()); |
| 260 | 873 | if (!d.mkdirs()) { |
| 261 | 0 | throw new AnalysisException("Unable to create '" + d.getAbsolutePath() + "'."); |
| 262 | |
} |
| 263 | 873 | } else { |
| 264 | 17691 | final File file = new File(extractTo, entry.getName()); |
| 265 | 17691 | final String ext = org.owasp.dependencycheck.utils.FileUtils.getFileExtension(file.getName()); |
| 266 | 17691 | if (engine.supportsExtension(ext)) { |
| 267 | 177 | BufferedOutputStream bos = null; |
| 268 | |
FileOutputStream fos; |
| 269 | |
try { |
| 270 | 177 | fos = new FileOutputStream(file); |
| 271 | 177 | bos = new BufferedOutputStream(fos, BUFFER_SIZE); |
| 272 | |
int count; |
| 273 | 177 | final byte data[] = new byte[BUFFER_SIZE]; |
| 274 | 354765 | while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) { |
| 275 | 354588 | bos.write(data, 0, count); |
| 276 | |
} |
| 277 | 177 | bos.flush(); |
| 278 | 0 | } catch (FileNotFoundException ex) { |
| 279 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 280 | 0 | throw new AnalysisException("Unable to find file '" + file.getName() + "'.", ex); |
| 281 | 0 | } catch (IOException ex) { |
| 282 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 283 | 0 | throw new AnalysisException("IO Exception while parsing file '" + file.getName() + "'.", ex); |
| 284 | |
} finally { |
| 285 | 177 | if (bos != null) { |
| 286 | |
try { |
| 287 | 177 | bos.close(); |
| 288 | 0 | } catch (IOException ex) { |
| 289 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 290 | 177 | } |
| 291 | |
} |
| 292 | |
} |
| 293 | |
} |
| 294 | 17691 | } |
| 295 | |
} |
| 296 | 0 | } catch (IOException ex) { |
| 297 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 298 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, msg, ex); |
| 299 | 0 | throw new AnalysisException(msg, ex); |
| 300 | |
} finally { |
| 301 | 0 | try { |
| 302 | 3 | zis.close(); |
| 303 | 0 | } catch (IOException ex) { |
| 304 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 305 | 3 | } |
| 306 | 0 | } |
| 307 | 3 | } |
| 308 | |
} |