-
Notifications
You must be signed in to change notification settings - Fork 145
Reduce number of CVE collisions dependency-check/dependency-check-son… #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jordannstrong
wants to merge
2
commits into
dependency-check:master
Choose a base branch
from
jordannstrong:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
@@ -36,15 +37,17 @@ | |
| import org.sonar.dependencycheck.parser.element.Confidence; | ||
| import org.sonar.dependencycheck.parser.element.Dependency; | ||
| import org.sonar.dependencycheck.parser.element.IncludedBy; | ||
| import org.sonar.dependencycheck.parser.element.Vulnerability; | ||
| import org.sonar.dependencycheck.reason.maven.MavenDependency; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
|
|
||
| public class GradleDependencyReason extends DependencyReason { | ||
|
|
||
| private final InputFile buildGradle; | ||
| private String content; | ||
| private final Map<Dependency, TextRangeConfidence> dependencyMap; | ||
| private final Map<Map<Dependency, Vulnerability>, TextRangeConfidence> dependencyMap; | ||
|
|
||
| private static final Logger LOGGER = Loggers.get(GradleDependencyReason.class); | ||
|
|
||
|
|
@@ -62,45 +65,45 @@ public GradleDependencyReason(@NonNull InputFile buildGradle) { | |
|
|
||
| @Override | ||
| @NonNull | ||
| public TextRangeConfidence getBestTextRange(@NonNull Dependency dependency) { | ||
| public TextRangeConfidence getBestTextRange(@NonNull Dependency dependency, @Nullable Vulnerability vulnerability) { | ||
| if (dependencyMap.containsKey(dependency)) { | ||
| return dependencyMap.get(dependency); | ||
| } else { | ||
| Optional<MavenDependency> mavenDependency = DependencyCheckUtils.getMavenDependency(dependency); | ||
| if (mavenDependency.isPresent()) { | ||
| fillArtifactMatch(dependency, mavenDependency.get()); | ||
| fillArtifactMatch(dependency, vulnerability, mavenDependency.get()); | ||
| } else { | ||
| LOGGER.debug("No artifactId found for Dependency {}", dependency.getFileName()); | ||
| } | ||
| Optional<Collection<IncludedBy>> includedBys = dependency.getIncludedBy(); | ||
| if (includedBys.isPresent()) { | ||
| workOnIncludedBy(dependency, includedBys.get()); | ||
| workOnIncludedBy(dependency, vulnerability, includedBys.get()); | ||
| } | ||
| dependencyMap.computeIfAbsent(dependency, k -> addDependencyToFirstLine(k, buildGradle)); | ||
| dependencyMap.computeIfAbsent(Collections.singletonMap(dependency, vulnerability), k -> addDependencyToFirstLine(k, buildGradle)); | ||
| } | ||
| return dependencyMap.get(dependency); | ||
| return dependencyMap.get(Collections.singletonMap(dependency, vulnerability)); | ||
| } | ||
|
|
||
| private void workOnIncludedBy(@NonNull Dependency dependency, Collection<IncludedBy> includedBys) { | ||
| private void workOnIncludedBy(@NonNull Dependency dependency, @Nullable Vulnerability vulnerability, Collection<IncludedBy> includedBys) { | ||
| for (IncludedBy includedBy : includedBys) { | ||
| String reference = includedBy.getReference(); | ||
| if (StringUtils.isNotBlank(reference)) { | ||
| Optional<SoftwareDependency> softwareDependency = DependencyCheckUtils.convertToSoftwareDependency(reference); | ||
| if (softwareDependency.isPresent() && DependencyCheckUtils.isMavenDependency(softwareDependency.get())) { | ||
| fillArtifactMatch(dependency, (MavenDependency) softwareDependency.get()); | ||
| fillArtifactMatch(dependency, vulnerability, (MavenDependency) softwareDependency.get()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void putDependencyMap(@NonNull Dependency dependency, TextRangeConfidence newTextRange) { | ||
| private void putDependencyMap(@NonNull Dependency dependency, @Nullable Vulnerability vulnerability, TextRangeConfidence newTextRange) { | ||
| if (dependencyMap.containsKey(dependency)) { | ||
| TextRangeConfidence oldTextRange = dependencyMap.get(dependency); | ||
| if (oldTextRange.getConfidence().compareTo(newTextRange.getConfidence()) > 0) { | ||
| dependencyMap.put(dependency, newTextRange); | ||
| dependencyMap.put(Collections.singletonMap(dependency, vulnerability), newTextRange); | ||
| } | ||
| } else { | ||
| dependencyMap.put(dependency, newTextRange); | ||
| dependencyMap.put(Collections.singletonMap(dependency, vulnerability), newTextRange); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -111,7 +114,7 @@ private void putDependencyMap(@NonNull Dependency dependency, TextRangeConfidenc | |
| * @param mavenDependency Identifier for gradle | ||
| * @return TextRange if found in gradle, else null | ||
| */ | ||
| private void fillArtifactMatch(@NonNull Dependency dependency, MavenDependency mavenDependency) { | ||
| private void fillArtifactMatch(@NonNull Dependency dependency, @Nullable Vulnerability vulnerability, MavenDependency mavenDependency) { | ||
| try (final Scanner scanner = new Scanner(content)) { | ||
| int linenumber = 0; | ||
| while (scanner.hasNextLine()) { | ||
|
|
@@ -123,18 +126,18 @@ private void fillArtifactMatch(@NonNull Dependency dependency, MavenDependency m | |
| if (depVersion.isPresent() && | ||
| lineFromFile.contains(depVersion.get())) { | ||
| LOGGER.debug("Found a artifactId, groupId and version match in {}", buildGradle); | ||
| putDependencyMap(dependency, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.HIGHEST)); | ||
| putDependencyMap(dependency, vulnerability, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.HIGHEST)); | ||
| return; | ||
| } else { | ||
|
||
| LOGGER.debug("Found a artifactId and groupId match in {} on line {}", buildGradle, linenumber); | ||
| putDependencyMap(dependency, vulnerability, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.HIGH)); | ||
| } | ||
| LOGGER.debug("Found a artifactId and groupId match in {} on line {}", buildGradle, linenumber); | ||
| putDependencyMap(dependency, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.HIGH)); | ||
| } | ||
| if (lineFromFile.contains(mavenDependency.getArtifactId())) { | ||
| } else if (lineFromFile.contains(mavenDependency.getArtifactId())) { | ||
| LOGGER.debug("Found a artifactId match in {} for {}", buildGradle, mavenDependency.getArtifactId()); | ||
| putDependencyMap(dependency, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.MEDIUM)); | ||
| } | ||
| if (lineFromFile.contains(mavenDependency.getGroupId())) { | ||
| putDependencyMap(dependency, vulnerability, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.MEDIUM)); | ||
| } else if (lineFromFile.contains(mavenDependency.getGroupId())) { | ||
| LOGGER.debug("Found a groupId match in {} for {}", buildGradle, mavenDependency.getGroupId()); | ||
| putDependencyMap(dependency, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.MEDIUM)); | ||
| putDependencyMap(dependency, vulnerability, new TextRangeConfidence(buildGradle.selectLine(linenumber), Confidence.MEDIUM)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.