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

Added tests for HS-JAVA-134 rule #687

Merged
merged 2 commits into from
Oct 19, 2021
Merged
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
23 changes: 23 additions & 0 deletions internal/services/engines/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ func TestRulesVulnerableCode(t *testing.T) {
},
},
},

// Java
{
name: "HS-JAVA-134",
rule: java.NewSQLInjection(),
src: SampleVulnerableJavaSQLInjection,
findings: []engine.Finding{
{
CodeSample: "var pstmt = con.prepareStatement(\"select * from mytable where field01 = '\" + field01 + \"'\");",
SourceLocation: engine.Location{
Line: 14,
Column: 50,
},
},
},
},
}

for _, tt := range testcases {
Expand Down Expand Up @@ -605,6 +621,13 @@ func TestRulesSafeCode(t *testing.T) {
rule: leaks.NewWPConfig(),
src: SampleSafeLeaksRegularWPConfig,
},

// Java
{
name: "HS-JAVA-134",
rule: java.NewSQLInjection(),
src: SampleSafeJavaSQLInjection,
},
}

for _, tt := range testcases {
Expand Down
47 changes: 47 additions & 0 deletions internal/services/engines/samples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,51 @@ func main() {
<?php
define('AUTH_KEY', getenv("AUTH_KEY"));
`

SampleVulnerableJavaSQLInjection = `
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class VulnerableCodeSQLInjection134 {

public void printResults(DataSource ds, String field01) throws SQLException {
try (
var con = ds.getConnection();
var pstmt = con.prepareStatement("select * from mytable where field01 = '" + field01 + "'");
var rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
}
`

SampleSafeJavaSQLInjection = `
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class VulnerableCodeSQLInjection134 {

public void printResults(DataSource ds, String field01) throws SQLException {
try {
var con = ds.getConnection();
var pstmt = con.prepareStatement("select * from mytable where field01 = ? ");
pstmt.setString(1,field01);
var rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
}
`
)