-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from cp-andrew-lindesay/extra-snyk-cli-commands
feat: implement new commands `code-test` and `container-test` + new c…
- Loading branch information
Showing
22 changed files
with
400 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# https://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#invokerPropertiesFile | ||
invoker.goals=test | ||
invoker.buildResult=failure |
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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.it</groupId> | ||
<artifactId>test-code-test</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<build> | ||
<plugins> | ||
|
||
<plugin> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>@project.artifactId@</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>code-test</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<cli> | ||
<executable>${env.SNYK_CLI_EXECUTABLE}</executable> | ||
</cli> | ||
<args> | ||
<arg>--print-deps</arg> | ||
</args> | ||
<apiToken>${env.SNYK_TEST_TOKEN}</apiToken> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.10.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
26 changes: 26 additions & 0 deletions
26
src/it/test-code-test/src/main/java/com/example/Application.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example; | ||
|
||
import java.sql.SQLException; | ||
|
||
public class Application { | ||
|
||
/** | ||
* <p>This code will allow the caller to provide an argument to the application which | ||
* finds its way into a SQL statement. For example <code>';DROP TABLE product;SELECT '</code> | ||
* could be passed in and it would give an opportunity to attack the system at the database | ||
* level.</p> | ||
*/ | ||
|
||
public static void main(String[] args) { | ||
VulnerableQueryHelper vulnerableQueryHelper = new VulnerableQueryHelper( | ||
null // not relevant for the purpose of analysis | ||
); | ||
|
||
try { | ||
System.out.println("" + vulnerableQueryHelper.countProductOrders(args[1])); | ||
} catch (SQLException se) { | ||
throw new Error(se); | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/it/test-code-test/src/main/java/com/example/VulnerableQueryHelper.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class VulnerableQueryHelper { | ||
|
||
private final DataSource dataSource; | ||
|
||
public VulnerableQueryHelper(DataSource dataSource) { | ||
this.dataSource = dataSource; | ||
} | ||
|
||
public int countProductOrders(String productCode) throws SQLException { | ||
try (Connection connection = dataSource.getConnection()) { | ||
return countProductOrders(connection, productCode); | ||
} | ||
} | ||
|
||
/** | ||
* <p>In this method, the argument provided is put directly into the SQL which would | ||
* allow an attacker to be able to execute arbitrary logic in the database.</p> | ||
*/ | ||
|
||
public int countProductOrders(Connection connection, String productCode) throws SQLException { | ||
String sql = "SELECT COUNT(id)\n" | ||
+ "FROM order o JOIN product p ON o.product_id = o.id\n" | ||
+ "WHERE p.code = '" + productCode + "'"; | ||
|
||
try (PreparedStatement statement = connection.prepareStatement(sql)) { | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (!resultSet.next()) { | ||
throw new IllegalStateException("expected a row to be returned from the sql query"); | ||
} | ||
return resultSet.getInt(1); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
String log = FileUtils.fileRead(new File(basedir, "build.log")) | ||
|
||
if (!log.contains("[High] SQL Injection")) { | ||
throw new Exception("no sql injection issue found") | ||
} | ||
|
||
return true; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# https://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#invokerPropertiesFile | ||
invoker.goals=install | ||
invoker.buildResult=failure |
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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.it</groupId> | ||
<artifactId>test-code-test</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<build> | ||
<plugins> | ||
|
||
<plugin> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>@project.artifactId@</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>container-test</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<cli> | ||
<executable>${env.SNYK_CLI_EXECUTABLE}</executable> | ||
</cli> | ||
<args> | ||
<arg>--print-deps</arg> | ||
<arg>--platform=linux/amd64</arg> | ||
<arg>nginx:1.21.1</arg> | ||
</args> | ||
<apiToken>${env.SNYK_TEST_TOKEN}</apiToken> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
String log = FileUtils.fileRead(new File(basedir, "build.log")) | ||
|
||
if (!log.contains("Medium severity vulnerability found in tiff/libtiff5")) { | ||
throw new Exception("Expected medium vulnerability not found") | ||
} | ||
|
||
if (!log.contains("Critical severity vulnerability found in zlib/zlib1g")) { | ||
throw new Exception("Expected critical vulnerability not found") | ||
} | ||
|
||
return true |
This file contains 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 |
---|---|---|
|
@@ -14,8 +14,8 @@ if (!log.contains("child-module-2 ..................................... FAILURE" | |
throw new Exception("child-module-2 should have failed."); | ||
} | ||
|
||
if (!log.contains("introduced by io.snyk.it:[email protected] > axis:[email protected]")) { | ||
throw new Exception("Could not find vulnerability in child-module-2."); | ||
if (!log.contains("introduced by axis:[email protected]")) { | ||
throw new Exception("Could not find expected vulnerability in child-module-2."); | ||
} | ||
|
||
return true; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# https://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#invokerPropertiesFile | ||
invoker.goals = install | ||
invoker.buildResult = success |
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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.it</groupId> | ||
<artifactId>test-not-fail-on-issues</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<!-- this contains a known vulnerability --> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>42.3.5</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
|
||
<plugin> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>@project.artifactId@</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>test</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<cli> | ||
<executable>${env.SNYK_CLI_EXECUTABLE}</executable> | ||
</cli> | ||
<args> | ||
<arg>--print-deps</arg> | ||
</args> | ||
<apiToken>${env.SNYK_TEST_TOKEN}</apiToken> | ||
<failOnIssues>false</failOnIssues> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
</project> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
String log = FileUtils.fileRead(new File(basedir, "build.log")) | ||
|
||
if (!log.contains("introduced by org.postgresql:[email protected]")) { | ||
throw new Exception("Vulnerability in dependency not found") | ||
} | ||
|
||
return true; |
This file contains 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 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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
String log = FileUtils.fileRead(new File(basedir, "build.log")); | ||
String log = FileUtils.fileRead(new File(basedir, "build.log")) | ||
String snykCliExecutable = System.getenv("SNYK_CLI_EXECUTABLE") | ||
|
||
if (snykCliExecutable == null || snykCliExecutable.isEmpty()) { | ||
throw new Exception("the environment variable `SNYK_CLI_EXECUTABLE` is not defined") | ||
} | ||
|
||
if (!log.contains("Snyk Executable Path: " + System.getenv("SNYK_CLI_EXECUTABLE"))) { | ||
throw new Exception("snyk executable path log line not found."); | ||
throw new Exception("snyk executable path log line not found.") | ||
} | ||
|
||
if (!(log =~ /Snyk CLI Version:\s+\d+\.\d+\.\d+/)) { | ||
throw new Exception("snyk version log line not found"); | ||
throw new Exception("snyk version log line not found") | ||
} | ||
|
||
if (!log.contains("for known issues, no vulnerable paths found.")) { | ||
throw new Exception("`snyk test` success output not found"); | ||
throw new Exception("`snyk test` success output not found") | ||
} | ||
|
||
return true; | ||
return true |
This file contains 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.