Skip to content
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

Filter fixable #94

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ buildNumber.properties
**/out/
**/.idea_modules/
*.iml


src/it/.DS_Store
47 changes: 47 additions & 0 deletions src/it/non-fixable-issues-module/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.snyk.example</groupId>
<artifactId>non-fixable-issues-module</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>non fixable issues module</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>2.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.snyk</groupId>
<artifactId>snyk-maven-plugin</artifactId>
<version>${snyk.maven.plugin.version}</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<apiToken>${env.SNYK_API_TOKEN}</apiToken>
<endpoint>${env.SNYK_API_ENDPOINT}</endpoint>
<failOnSeverity>low</failOnSeverity>
<onlyFailFixable>true</onlyFailFixable>
</configuration>
</plugin>
</plugins>
</build>
</project>
14 changes: 14 additions & 0 deletions src/it/non-fixable-issues-module/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* verifying that the non-fixable-issues-module integration test runs as expected
*/

import java.io.*;
import org.codehaus.plexus.util.*;

String log = FileUtils.fileRead( new File( basedir, "build.log" ) );

if(!log.contains("/SNYK-JAVA-COMMONSHTTPCLIENT-31660")) {
throw new Exception("Missing Vulnerability 'SNYK-JAVA-COMMONSHTTPCLIENT-31660' ");
}

return true;
22 changes: 19 additions & 3 deletions src/main/java/io/snyk/maven/plugins/SnykTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public class SnykTest extends AbstractMojo {
@Parameter
private boolean failOnAuthError = false;

@Parameter
private boolean onlyFailFixable = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caroalmirola, let's keep parameter naming consistent (failOnSeverity, failOnAuthError).
How about failOnFixableOnly.


@Parameter(property = "snyk.skip")
private boolean skip;

Expand Down Expand Up @@ -262,15 +265,17 @@ private void processVulns(JSONObject responseJson) throws MojoFailureException {
HashSet<String> vulnIdSet = new HashSet<String>();

JSONArray vulns = (JSONArray)responseJson.get("vulnerabilities");
int highestSeverity = SEVERITY_LOW;
int highestSeverity = Integer.MIN_VALUE;

Iterator<JSONObject> iterator = vulns.iterator();
while (iterator.hasNext()) {
JSONObject vuln = iterator.next();
vulnIdSet.add((String)vuln.get("id"));
Integer severityInt = severityMap.get(vuln.get("severity"));
if(severityInt != null && severityInt > highestSeverity) {
highestSeverity = severityInt;
if (!onlyFailFixable || (onlyFailFixable && isIssueFixable(vuln))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement can be simplified. Atm second onlyFailFixable is always true.

if (severityInt != null && severityInt > highestSeverity) {
highestSeverity = severityInt;
}
}
printVuln(vuln);
}
Expand All @@ -292,6 +297,17 @@ private void processVulns(JSONObject responseJson) throws MojoFailureException {
}
}

private boolean isIssueFixable(JSONObject vuln) {
boolean upgradable =
(vuln.get("isUpgradable") != null && (boolean) vuln.get("isUpgradable"));
boolean fixable = false;
if (vuln.get("fixedIn") != null) {
JSONArray fixedIn = (JSONArray) vuln.get("fixedIn");
fixable = (!fixedIn.isEmpty());
}
return upgradable || fixable;
}

Comment on lines +300 to +310
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue is fixable if it can be eliminated with an upgrade ("isUpgradable") or patch ("isPatchable").

/**
* print a single vuln to the build log
* @param vuln a JSON object containing a single vuln
Expand Down