| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck; |
| 20 | |
|
| 21 | |
import java.util.EnumMap; |
| 22 | |
import java.io.File; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.HashSet; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.Set; |
| 28 | |
import java.util.logging.Level; |
| 29 | |
import java.util.logging.Logger; |
| 30 | |
import org.owasp.dependencycheck.analyzer.AnalysisException; |
| 31 | |
import org.owasp.dependencycheck.analyzer.AnalysisPhase; |
| 32 | |
import org.owasp.dependencycheck.analyzer.Analyzer; |
| 33 | |
import org.owasp.dependencycheck.analyzer.AnalyzerService; |
| 34 | |
import org.owasp.dependencycheck.data.CachedWebDataSource; |
| 35 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 36 | |
import org.owasp.dependencycheck.data.UpdateService; |
| 37 | |
import org.owasp.dependencycheck.dependency.Dependency; |
| 38 | |
import org.owasp.dependencycheck.utils.FileUtils; |
| 39 | |
import org.owasp.dependencycheck.utils.InvalidSettingException; |
| 40 | |
import org.owasp.dependencycheck.utils.Settings; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class Engine { |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 3 | private final List<Dependency> dependencies = new ArrayList<Dependency>(); |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 3 | private final EnumMap<AnalysisPhase, List<Analyzer>> analyzers = |
| 60 | |
new EnumMap<AnalysisPhase, List<Analyzer>>(AnalysisPhase.class); |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 3 | private final Set<String> extensions = new HashSet<String>(); |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 3 | public Engine() { |
| 70 | 3 | boolean autoUpdate = true; |
| 71 | |
try { |
| 72 | 3 | autoUpdate = Settings.getBoolean(Settings.KEYS.AUTO_UPDATE); |
| 73 | 0 | } catch (InvalidSettingException ex) { |
| 74 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINE, "Invalid setting for auto-update; using true."); |
| 75 | 3 | } |
| 76 | 3 | if (autoUpdate) { |
| 77 | 0 | doUpdates(); |
| 78 | |
} |
| 79 | 3 | loadAnalyzers(); |
| 80 | 3 | } |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
@Deprecated |
| 92 | 0 | public Engine(boolean autoUpdate) { |
| 93 | 0 | if (autoUpdate) { |
| 94 | 0 | doUpdates(); |
| 95 | |
} |
| 96 | 0 | loadAnalyzers(); |
| 97 | 0 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private void loadAnalyzers() { |
| 104 | |
|
| 105 | 30 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 106 | 27 | analyzers.put(phase, new ArrayList<Analyzer>()); |
| 107 | |
} |
| 108 | |
|
| 109 | 3 | final AnalyzerService service = AnalyzerService.getInstance(); |
| 110 | 3 | final Iterator<Analyzer> iterator = service.getAnalyzers(); |
| 111 | 27 | while (iterator.hasNext()) { |
| 112 | 24 | final Analyzer a = iterator.next(); |
| 113 | 24 | analyzers.get(a.getAnalysisPhase()).add(a); |
| 114 | 24 | if (a.getSupportedExtensions() != null) { |
| 115 | 6 | extensions.addAll(a.getSupportedExtensions()); |
| 116 | |
} |
| 117 | 24 | } |
| 118 | 3 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
public List<Analyzer> getAnalyzers(AnalysisPhase phase) { |
| 127 | 0 | return analyzers.get(phase); |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public List<Dependency> getDependencies() { |
| 136 | 17 | return dependencies; |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public void scan(String[] paths) { |
| 149 | 0 | for (String path : paths) { |
| 150 | 0 | final File file = new File(path); |
| 151 | 0 | scan(file); |
| 152 | |
} |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public void scan(String path) { |
| 163 | 0 | final File file = new File(path); |
| 164 | 0 | scan(file); |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
public void scan(File[] files) { |
| 177 | 0 | for (File file : files) { |
| 178 | 0 | scan(file); |
| 179 | |
} |
| 180 | 0 | } |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public void scan(Set<File> files) { |
| 192 | 0 | for (File file : files) { |
| 193 | 0 | scan(file); |
| 194 | |
} |
| 195 | 0 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
public void scan(List<File> files) { |
| 207 | 0 | for (File file : files) { |
| 208 | 0 | scan(file); |
| 209 | |
} |
| 210 | 0 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
public void scan(File file) { |
| 222 | 6 | if (file.exists()) { |
| 223 | 6 | if (file.isDirectory()) { |
| 224 | 3 | scanDirectory(file); |
| 225 | |
} else { |
| 226 | 3 | scanFile(file); |
| 227 | |
} |
| 228 | |
} |
| 229 | 6 | } |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
protected void scanDirectory(File dir) { |
| 238 | 28 | final File[] files = dir.listFiles(); |
| 239 | 28 | if (files != null) { |
| 240 | 58 | for (File f : files) { |
| 241 | 30 | if (f.isDirectory()) { |
| 242 | 25 | scanDirectory(f); |
| 243 | |
} else { |
| 244 | 5 | scanFile(f); |
| 245 | |
} |
| 246 | |
} |
| 247 | |
} |
| 248 | 28 | } |
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
protected void scanFile(File file) { |
| 257 | 8 | if (!file.isFile()) { |
| 258 | 0 | final String msg = String.format("Path passed to scanFile(File) is not a file: %s. Skipping the file.", file.toString()); |
| 259 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINE, msg); |
| 260 | 0 | return; |
| 261 | |
} |
| 262 | 8 | final String fileName = file.getName(); |
| 263 | 8 | final String extension = FileUtils.getFileExtension(fileName); |
| 264 | 8 | if (extension != null) { |
| 265 | 8 | if (extensions.contains(extension)) { |
| 266 | 8 | final Dependency dependency = new Dependency(file); |
| 267 | 8 | dependencies.add(dependency); |
| 268 | 8 | } |
| 269 | |
} else { |
| 270 | 0 | final String msg = String.format("No file extension found on file '%s'. The file was not analyzed.", |
| 271 | |
file.toString()); |
| 272 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINEST, msg); |
| 273 | |
} |
| 274 | 8 | } |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
public void analyzeDependencies() { |
| 280 | |
|
| 281 | 10 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 282 | 9 | final List<Analyzer> analyzerList = analyzers.get(phase); |
| 283 | 9 | for (Analyzer a : analyzerList) { |
| 284 | |
try { |
| 285 | 8 | a.initialize(); |
| 286 | 0 | } catch (Exception ex) { |
| 287 | 0 | final String msg = String.format("\"Exception occurred initializing \"%s\".\"", a.getName()); |
| 288 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, msg); |
| 289 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.INFO, msg, ex); |
| 290 | |
try { |
| 291 | 0 | a.close(); |
| 292 | 0 | } catch (Exception ex1) { |
| 293 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex1); |
| 294 | 0 | } |
| 295 | 16 | } |
| 296 | |
} |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | 10 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 301 | 9 | final List<Analyzer> analyzerList = analyzers.get(phase); |
| 302 | |
|
| 303 | 9 | for (Analyzer a : analyzerList) { |
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | 8 | final Set<Dependency> dependencySet = new HashSet<Dependency>(); |
| 309 | 8 | dependencySet.addAll(dependencies); |
| 310 | 8 | for (Dependency d : dependencySet) { |
| 311 | 24 | if (a.supportsExtension(d.getFileExtension())) { |
| 312 | |
try { |
| 313 | 21 | a.analyze(d, this); |
| 314 | 0 | } catch (AnalysisException ex) { |
| 315 | 0 | d.addAnalysisException(ex); |
| 316 | 45 | } |
| 317 | |
} |
| 318 | |
} |
| 319 | 8 | } |
| 320 | |
} |
| 321 | |
|
| 322 | |
|
| 323 | 10 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 324 | 9 | final List<Analyzer> analyzerList = analyzers.get(phase); |
| 325 | 9 | for (Analyzer a : analyzerList) { |
| 326 | |
try { |
| 327 | 8 | a.close(); |
| 328 | 0 | } catch (Exception ex) { |
| 329 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINEST, null, ex); |
| 330 | 16 | } |
| 331 | |
} |
| 332 | |
} |
| 333 | 1 | } |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
private void doUpdates() { |
| 340 | 0 | final UpdateService service = UpdateService.getInstance(); |
| 341 | 0 | final Iterator<CachedWebDataSource> iterator = service.getDataSources(); |
| 342 | 0 | while (iterator.hasNext()) { |
| 343 | 0 | final CachedWebDataSource source = iterator.next(); |
| 344 | |
try { |
| 345 | 0 | source.update(); |
| 346 | 0 | } catch (UpdateException ex) { |
| 347 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.WARNING, |
| 348 | |
"Unable to update Cached Web DataSource, using local data instead. Results may not include recent vulnerabilities."); |
| 349 | 0 | Logger.getLogger(Engine.class.getName()).log(Level.FINE, |
| 350 | |
String.format("Unable to update details for %s", source.getClass().getName()), ex); |
| 351 | 0 | } |
| 352 | 0 | } |
| 353 | 0 | } |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
public List<Analyzer> getAnalyzers() { |
| 362 | 1 | final List<Analyzer> ret = new ArrayList<Analyzer>(); |
| 363 | 10 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 364 | 9 | final List<Analyzer> analyzerList = analyzers.get(phase); |
| 365 | 9 | ret.addAll(analyzerList); |
| 366 | |
} |
| 367 | 1 | return ret; |
| 368 | |
} |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
public boolean supportsExtension(String ext) { |
| 378 | 138 | if (ext == null) { |
| 379 | 8 | return false; |
| 380 | |
} |
| 381 | 1259 | for (AnalysisPhase phase : AnalysisPhase.values()) { |
| 382 | 1134 | final List<Analyzer> analyzerList = analyzers.get(phase); |
| 383 | 1134 | for (Analyzer a : analyzerList) { |
| 384 | 1009 | if (a.getSupportedExtensions() != null && a.supportsExtension(ext)) { |
| 385 | 5 | return true; |
| 386 | |
} |
| 387 | |
} |
| 388 | |
} |
| 389 | 125 | return false; |
| 390 | |
} |
| 391 | |
} |