| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.owasp.dependencycheck.analyzer; |
| 19 | |
|
| 20 | |
import java.io.BufferedInputStream; |
| 21 | |
import java.io.BufferedOutputStream; |
| 22 | |
import java.io.File; |
| 23 | |
import java.io.FileInputStream; |
| 24 | |
import java.io.FileNotFoundException; |
| 25 | |
import java.io.FileOutputStream; |
| 26 | |
import java.io.IOException; |
| 27 | |
import java.util.ArrayList; |
| 28 | |
import java.util.Collections; |
| 29 | |
import java.util.HashSet; |
| 30 | |
import java.util.List; |
| 31 | |
import java.util.Set; |
| 32 | |
import java.util.logging.Level; |
| 33 | |
import java.util.logging.Logger; |
| 34 | |
import org.apache.commons.compress.archivers.ArchiveEntry; |
| 35 | |
import org.apache.commons.compress.archivers.ArchiveInputStream; |
| 36 | |
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 37 | |
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; |
| 38 | |
import org.apache.commons.compress.compressors.CompressorInputStream; |
| 39 | |
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 40 | |
import org.apache.commons.compress.compressors.gzip.GzipUtils; |
| 41 | |
import org.h2.store.fs.FileUtils; |
| 42 | |
import org.owasp.dependencycheck.Engine; |
| 43 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 44 | |
import org.owasp.dependencycheck.utils.Settings; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 11 | public class ArchiveAnalyzer extends AbstractAnalyzer implements Analyzer { |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
private static final int BUFFER_SIZE = 4096; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 1 | private static int dirCount = 0; |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 11 | private File tempFileLocation = null; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 1 | private static final int MAX_SCAN_DEPTH = Settings.getInt("archive.scan.depth", 3); |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | 11 | private int scanDepth = 0; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
private static final String ANALYZER_NAME = "Archive Analyzer"; |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | 1 | private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INITIAL; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 1 | private static final Set<String> ZIPPABLES = newHashSet("zip", "ear", "war", "nupkg"); |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | 1 | private static final Set<String> EXTENSIONS = newHashSet("tar", "gz", "tgz"); |
| 92 | |
static { |
| 93 | 1 | EXTENSIONS.addAll(ZIPPABLES); |
| 94 | 1 | } |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public Set<String> getSupportedExtensions() { |
| 102 | 158 | return EXTENSIONS; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public String getName() { |
| 111 | 10 | return ANALYZER_NAME; |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public boolean supportsExtension(String extension) { |
| 121 | 173 | return EXTENSIONS.contains(extension); |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public AnalysisPhase getAnalysisPhase() { |
| 130 | 7 | return ANALYSIS_PHASE; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
@Override |
| 140 | |
public void initialize() throws Exception { |
| 141 | 9 | final File baseDir = Settings.getTempDirectory(); |
| 142 | 9 | if (!baseDir.exists()) { |
| 143 | 0 | if (!baseDir.mkdirs()) { |
| 144 | 0 | final String msg = String.format("Unable to make a temporary folder '%s'", baseDir.getPath()); |
| 145 | 0 | throw new AnalysisException(msg); |
| 146 | |
} |
| 147 | |
} |
| 148 | 9 | tempFileLocation = File.createTempFile("check", "tmp", baseDir); |
| 149 | 9 | if (!tempFileLocation.delete()) { |
| 150 | 0 | final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath()); |
| 151 | 0 | throw new AnalysisException(msg); |
| 152 | |
} |
| 153 | 9 | if (!tempFileLocation.mkdirs()) { |
| 154 | 0 | final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath()); |
| 155 | 0 | throw new AnalysisException(msg); |
| 156 | |
} |
| 157 | 9 | } |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
@Override |
| 165 | |
public void close() throws Exception { |
| 166 | 9 | if (tempFileLocation != null && tempFileLocation.exists()) { |
| 167 | 9 | FileUtils.deleteRecursive(tempFileLocation.getAbsolutePath(), true); |
| 168 | |
} |
| 169 | 9 | } |
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public void analyze(Dependency dependency, Engine engine) throws AnalysisException { |
| 181 | 8 | final File f = new File(dependency.getActualFilePath()); |
| 182 | 8 | final File tmpDir = getNextTempDirectory(); |
| 183 | 8 | extractFiles(f, tmpDir, engine); |
| 184 | |
|
| 185 | |
|
| 186 | 8 | final List<Dependency> dependencies = new ArrayList<Dependency>(engine.getDependencies()); |
| 187 | 8 | engine.scan(tmpDir); |
| 188 | 8 | final List<Dependency> newDependencies = engine.getDependencies(); |
| 189 | 8 | if (dependencies.size() != newDependencies.size()) { |
| 190 | |
|
| 191 | 6 | final Set<Dependency> dependencySet = new HashSet<Dependency>(); |
| 192 | 6 | dependencySet.addAll(newDependencies); |
| 193 | 6 | dependencySet.removeAll(dependencies); |
| 194 | |
|
| 195 | 6 | for (Dependency d : dependencySet) { |
| 196 | |
|
| 197 | 19 | final String displayPath = String.format("%s%s", |
| 198 | |
dependency.getFilePath(), |
| 199 | |
d.getActualFilePath().substring(tmpDir.getAbsolutePath().length())); |
| 200 | 19 | final String displayName = String.format("%s%s%s", |
| 201 | |
dependency.getFileName(), |
| 202 | |
File.separator, |
| 203 | |
d.getFileName()); |
| 204 | 19 | d.setFilePath(displayPath); |
| 205 | 19 | d.setFileName(displayName); |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | 19 | if (this.supportsExtension(d.getFileExtension()) && scanDepth < MAX_SCAN_DEPTH) { |
| 210 | 3 | scanDepth += 1; |
| 211 | 3 | analyze(d, engine); |
| 212 | 3 | scanDepth -= 1; |
| 213 | |
} |
| 214 | 19 | } |
| 215 | |
} |
| 216 | 8 | Collections.sort(engine.getDependencies()); |
| 217 | 8 | } |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
private File getNextTempDirectory() throws AnalysisException { |
| 226 | 8 | dirCount += 1; |
| 227 | 8 | final File directory = new File(tempFileLocation, String.valueOf(dirCount)); |
| 228 | |
|
| 229 | 8 | if (directory.exists()) { |
| 230 | 0 | return getNextTempDirectory(); |
| 231 | |
} |
| 232 | 8 | if (!directory.mkdirs()) { |
| 233 | 0 | final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath()); |
| 234 | 0 | throw new AnalysisException(msg); |
| 235 | |
} |
| 236 | 8 | return directory; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
private void extractFiles(File archive, File destination, Engine engine) throws AnalysisException { |
| 248 | 8 | if (archive == null || destination == null) { |
| 249 | 0 | return; |
| 250 | |
} |
| 251 | |
|
| 252 | 8 | FileInputStream fis = null; |
| 253 | |
try { |
| 254 | 8 | fis = new FileInputStream(archive); |
| 255 | 0 | } catch (FileNotFoundException ex) { |
| 256 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.INFO, null, ex); |
| 257 | 0 | throw new AnalysisException("Archive file was not found.", ex); |
| 258 | 8 | } |
| 259 | 8 | final String archiveExt = org.owasp.dependencycheck.utils.FileUtils.getFileExtension(archive.getName()).toLowerCase(); |
| 260 | |
try { |
| 261 | 8 | if (ZIPPABLES.contains(archiveExt)) { |
| 262 | 3 | extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, engine); |
| 263 | 5 | } else if ("tar".equals(archiveExt)) { |
| 264 | 3 | extractArchive(new TarArchiveInputStream(new BufferedInputStream(fis)), destination, engine); |
| 265 | 2 | } else if ("gz".equals(archiveExt) || "tgz".equals(archiveExt)) { |
| 266 | 2 | final String uncompressedName = GzipUtils.getUncompressedFilename(archive.getName()); |
| 267 | 2 | final String uncompressedExt = org.owasp.dependencycheck.utils.FileUtils.getFileExtension(uncompressedName).toLowerCase(); |
| 268 | 2 | if (engine.supportsExtension(uncompressedExt)) { |
| 269 | 2 | decompressFile(new GzipCompressorInputStream(new BufferedInputStream(fis)), new File(destination, uncompressedName)); |
| 270 | |
} |
| 271 | |
} |
| 272 | 0 | } catch (ArchiveExtractionException ex) { |
| 273 | 0 | final String msg = String.format("Exception extracting archive '%s'.", archive.getName()); |
| 274 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.WARNING, msg); |
| 275 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 276 | 0 | } catch (IOException ex) { |
| 277 | 0 | final String msg = String.format("Exception reading archive '%s'.", archive.getName()); |
| 278 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.WARNING, msg); |
| 279 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 280 | |
} finally { |
| 281 | 0 | try { |
| 282 | 8 | fis.close(); |
| 283 | 0 | } catch (IOException ex) { |
| 284 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 285 | 8 | } |
| 286 | 0 | } |
| 287 | 8 | } |
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
private void extractArchive(ArchiveInputStream input, File destination, Engine engine) throws ArchiveExtractionException { |
| 298 | |
ArchiveEntry entry; |
| 299 | |
try { |
| 300 | 181 | while ((entry = input.getNextEntry()) != null) { |
| 301 | 175 | if (entry.isDirectory()) { |
| 302 | 25 | final File d = new File(destination, entry.getName()); |
| 303 | 25 | if (!d.exists()) { |
| 304 | 25 | if (!d.mkdirs()) { |
| 305 | 0 | final String msg = String.format("Unable to create directory '%s'.", d.getAbsolutePath()); |
| 306 | 0 | throw new AnalysisException(msg); |
| 307 | |
} |
| 308 | |
} |
| 309 | 25 | } else { |
| 310 | 150 | final File file = new File(destination, entry.getName()); |
| 311 | 150 | final String ext = org.owasp.dependencycheck.utils.FileUtils.getFileExtension(file.getName()); |
| 312 | 150 | if (engine.supportsExtension(ext)) { |
| 313 | 17 | BufferedOutputStream bos = null; |
| 314 | |
FileOutputStream fos; |
| 315 | |
try { |
| 316 | 17 | final File parent = file.getParentFile(); |
| 317 | 17 | if (!parent.isDirectory()) { |
| 318 | 1 | if (!parent.mkdirs()) { |
| 319 | 0 | final String msg = String.format("Unable to build directory '%s'.", parent.getAbsolutePath()); |
| 320 | 0 | throw new AnalysisException(msg); |
| 321 | |
} |
| 322 | |
} |
| 323 | 17 | fos = new FileOutputStream(file); |
| 324 | 17 | bos = new BufferedOutputStream(fos, BUFFER_SIZE); |
| 325 | |
int count; |
| 326 | 17 | final byte data[] = new byte[BUFFER_SIZE]; |
| 327 | 3367 | while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) { |
| 328 | 3350 | bos.write(data, 0, count); |
| 329 | |
} |
| 330 | 17 | bos.flush(); |
| 331 | 0 | } catch (FileNotFoundException ex) { |
| 332 | 0 | Logger.getLogger(ArchiveAnalyzer.class |
| 333 | |
.getName()).log(Level.FINE, null, ex); |
| 334 | 0 | final String msg = String.format("Unable to find file '%s'.", file.getName()); |
| 335 | 0 | throw new AnalysisException(msg, ex); |
| 336 | 0 | } catch (IOException ex) { |
| 337 | 0 | Logger.getLogger(ArchiveAnalyzer.class |
| 338 | |
.getName()).log(Level.FINE, null, ex); |
| 339 | 0 | final String msg = String.format("IO Exception while parsing file '%s'.", file.getName()); |
| 340 | 0 | throw new AnalysisException(msg, ex); |
| 341 | |
} finally { |
| 342 | 17 | if (bos != null) { |
| 343 | |
try { |
| 344 | 17 | bos.close(); |
| 345 | 0 | } catch (IOException ex) { |
| 346 | 0 | Logger.getLogger(ArchiveAnalyzer.class |
| 347 | |
.getName()).log(Level.FINEST, null, ex); |
| 348 | 17 | } |
| 349 | |
} |
| 350 | |
} |
| 351 | |
} |
| 352 | 150 | } |
| 353 | |
} |
| 354 | 0 | } catch (IOException ex) { |
| 355 | 0 | throw new ArchiveExtractionException(ex); |
| 356 | 0 | } catch (Throwable ex) { |
| 357 | 0 | throw new ArchiveExtractionException(ex); |
| 358 | |
} finally { |
| 359 | 6 | if (input != null) { |
| 360 | |
try { |
| 361 | 6 | input.close(); |
| 362 | 0 | } catch (IOException ex) { |
| 363 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 364 | 6 | } |
| 365 | |
} |
| 366 | |
} |
| 367 | 6 | } |
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
private void decompressFile(CompressorInputStream inputStream, File outputFile) throws ArchiveExtractionException { |
| 377 | 2 | FileOutputStream out = null; |
| 378 | |
try { |
| 379 | 2 | out = new FileOutputStream(outputFile); |
| 380 | 2 | final byte[] buffer = new byte[BUFFER_SIZE]; |
| 381 | 2 | int n = 0; |
| 382 | 272 | while (-1 != (n = inputStream.read(buffer))) { |
| 383 | 270 | out.write(buffer, 0, n); |
| 384 | |
} |
| 385 | 0 | } catch (FileNotFoundException ex) { |
| 386 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 387 | 0 | throw new ArchiveExtractionException(ex); |
| 388 | 0 | } catch (IOException ex) { |
| 389 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINE, null, ex); |
| 390 | 0 | throw new ArchiveExtractionException(ex); |
| 391 | |
} finally { |
| 392 | 2 | if (out != null) { |
| 393 | |
try { |
| 394 | 2 | out.close(); |
| 395 | 0 | } catch (IOException ex) { |
| 396 | 0 | Logger.getLogger(ArchiveAnalyzer.class.getName()).log(Level.FINEST, null, ex); |
| 397 | 2 | } |
| 398 | |
} |
| 399 | |
} |
| 400 | 2 | } |
| 401 | |
} |