Skip to content

Commit

Permalink
Merge pull request #3 from snyk-partners/fix/Fix-unique-issues-id-col…
Browse files Browse the repository at this point in the history
…lisions

fix: Fix unique issue id collisions
  • Loading branch information
aarlaud authored Dec 7, 2020
2 parents 17ff3e5 + 9c3e082 commit dd715e6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.snyk.integrations.fortify</groupId>
<artifactId>parser</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public enum CustomAttribute implements com.fortify.plugin.spi.VulnerabilityAttri
IS_UPGRADABLE("isUpgradable", AttrType.STRING),
IS_PATCHABLE("isPatchable", AttrType.STRING),
TARGET_FILE("targetFile", AttrType.STRING),

PROJECT_NAME("projectName", AttrType.STRING),
ISSUE_URL("issueUrl", AttrType.STRING);
;

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/snyk/integrations/fortify/parser/Scan.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
public class Scan {
public Date scanDate;

public String targetFile;
public String displayTargetFile;
public String projectName;

public Issue[] vulnerabilities;

Expand All @@ -24,4 +26,4 @@ public class Issue {
public boolean isUpgradable;
public boolean isPatchable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ public void parseVulnerabilities(final ScanData scanData, final VulnerabilityHan
for (Scan scan : scans) {
for (Scan.Issue issue : scan.vulnerabilities) {
try {
String uniqueId = scan.displayTargetFile + ":" + hashJsonObject(issue);
String targetFile = scan.targetFile != null ? scan.targetFile : scan.displayTargetFile;
String uniqueId = hashStringObject(targetFile) + ":" + hashJsonObject(issue);
StaticVulnerabilityBuilder vulnerabilityBuilder = vulnerabilityHandler
.startStaticVulnerability(uniqueId);

buildVulnerability(vulnerabilityBuilder, issue, scan.displayTargetFile);
buildVulnerability(vulnerabilityBuilder, issue, targetFile, scan.projectName);

vulnerabilityBuilder.completeVulnerability();
} catch (NullPointerException e) {
Expand All @@ -91,7 +92,7 @@ public void parseVulnerabilities(final ScanData scanData, final VulnerabilityHan
}
}

private void buildVulnerability(final StaticVulnerabilityBuilder vulnerabilityBuilder, final Scan.Issue issue, final String targetFile) {
private void buildVulnerability(final StaticVulnerabilityBuilder vulnerabilityBuilder, final Scan.Issue issue, final String targetFile, final String projectName) {
// mandatory by SSC
vulnerabilityBuilder.setAccuracy(5f);
vulnerabilityBuilder.setAnalyzer("snyk");
Expand Down Expand Up @@ -135,6 +136,7 @@ private void buildVulnerability(final StaticVulnerabilityBuilder vulnerabilityBu
vulnerabilityBuilder.setStringCustomAttributeValue(CustomAttribute.IS_PATCHABLE,
(issue.isPatchable ? "Yes" : "No"));
vulnerabilityBuilder.setStringCustomAttributeValue(CustomAttribute.TARGET_FILE, targetFile);
vulnerabilityBuilder.setStringCustomAttributeValue(CustomAttribute.PROJECT_NAME, projectName);
vulnerabilityBuilder.setStringCustomAttributeValue(CustomAttribute.ISSUE_URL,
"https://snyk.io/vuln/" + issue.id);
}
Expand Down Expand Up @@ -164,4 +166,14 @@ private String hashJsonObject(final Object obj) {
return ""; // should never reach here
}
}

private String hashStringObject(final String obj) {
try {
byte[] stringBytes = obj.getBytes();
byte[] digest = MessageDigest.getInstance("SHA-256").digest(stringBytes);
return UUID.nameUUIDFromBytes(digest).toString();
} catch (NoSuchAlgorithmException e) {
return ""; // should never reach here
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
id="io.snyk.integrations.fortify.parser" api-version="1.0">
<plugin-info>
<name>Snyk Parser Plugin</name>
<version><!--VERSION-->0.0.3<!--/VERSION--></version>
<version><!--VERSION-->0.0.4<!--/VERSION--></version>
<data-version>1</data-version>
<vendor name="Snyk" url="https://snyk.io"/>
<description>Snyk automates finding and fixing known vulnerabilities in your open source dependencies. This plugin parses "snyk test" results and uploads them to Fortify Software Security Center.
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/resources/en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ from=Through
isUpgradable=Is Upgradable
isPatchable=Is Patchable
targetFile=Target File
projectName=Project Name
issueUrl=More Info on snyk.io
6 changes: 6 additions & 0 deletions src/main/resources/viewtemplate/ViewTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
"templateId": "SIMPLE",
"dataType": "string"
},
{
"type": "template",
"key": "customAttributes.projectName",
"templateId": "SIMPLE",
"dataType": "string"
},
{
"type": "template",
"key": "customAttributes.packageManager",
Expand Down

0 comments on commit dd715e6

Please sign in to comment.