-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
458 additions
and
5 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
Binary file not shown.
Binary file not shown.
File renamed without changes.
72 changes: 72 additions & 0 deletions
72
byteflow-core/src/examples/java/org/byteflow/examples/SqlInjectionSample.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,72 @@ | ||
package org.byteflow.examples; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
@SuppressWarnings({"unused", "DuplicatedCode"}) | ||
public class SqlInjectionSample { | ||
boolean isProduction; | ||
|
||
public boolean isAdmin(String userId) throws SQLException { | ||
String adminUserName; | ||
if (isProduction) { | ||
adminUserName = getAdminUserNameProd(); | ||
} else { | ||
adminUserName = getAdminUserNameDev(); | ||
} | ||
|
||
boolean isAdmin; | ||
if (isProduction) { | ||
isAdmin = checkUserIsAdminProd(userId, adminUserName); | ||
} else { | ||
isAdmin = checkUserIsAdminDev(userId, adminUserName); | ||
} | ||
|
||
return isAdmin; | ||
} | ||
|
||
private String getAdminUserNameProd() { | ||
return "root"; | ||
} | ||
|
||
private String getAdminUserNameDev() { | ||
return System.getenv("admin_name"); | ||
} | ||
|
||
private boolean checkUserIsAdminProd(String userId, String adminName) throws SQLException { | ||
String adminId; | ||
try (Connection dbConnection = DriverManager.getConnection("url://127.0.0.1:8080"); | ||
Statement statement = dbConnection.createStatement()) { | ||
// SECS: potential SQL injection | ||
ResultSet rs = statement.executeQuery("SELECT id from users where name='" + adminName + "'"); | ||
if (rs.next()) { | ||
adminId = rs.getString(0); | ||
} else { | ||
throw new IllegalStateException("No admin id"); | ||
} | ||
} | ||
|
||
if (adminId == null) { | ||
throw new IllegalStateException("No admin id"); | ||
} | ||
|
||
return adminId.equals(userId); | ||
} | ||
|
||
private boolean checkUserIsAdminDev(String userId, String adminName) { | ||
String adminId; | ||
if ("root_1".equals(adminName)) { | ||
adminId = "1"; | ||
} else if ("root_2".equals(adminName)) { | ||
adminId = "2"; | ||
} else { | ||
adminId = "0"; | ||
} | ||
|
||
return adminId.equals(userId); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
byteflow-core/src/examples/java/org/byteflow/examples/SqlInjectionSample2.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,72 @@ | ||
package org.byteflow.examples; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
@SuppressWarnings({"unused", "DuplicatedCode"}) | ||
public class SqlInjectionSample2 { | ||
boolean isProduction; | ||
|
||
public boolean isAdmin(String userId) throws SQLException { | ||
String adminUserName; | ||
if (isProduction) { | ||
adminUserName = getAdminUserNameProd(); | ||
} else { | ||
adminUserName = getAdminUserNameDev(); | ||
} | ||
|
||
boolean isAdmin; | ||
if (isProduction) { | ||
isAdmin = checkUserIsAdminProd(userId, adminUserName); | ||
} else { | ||
isAdmin = checkUserIsAdminProd(userId, adminUserName); | ||
} | ||
|
||
return isAdmin; | ||
} | ||
|
||
private String getAdminUserNameProd() { | ||
return "root"; | ||
} | ||
|
||
private String getAdminUserNameDev() { | ||
return System.getenv("admin_name"); | ||
} | ||
|
||
private boolean checkUserIsAdminProd(String userId, String adminName) throws SQLException { | ||
String adminId; | ||
try (Connection dbConnection = DriverManager.getConnection("url://127.0.0.1:8080"); | ||
Statement statement = dbConnection.createStatement()) { | ||
// SECS: potential SQL injection | ||
ResultSet rs = statement.executeQuery("SELECT id from users where name='" + adminName + "'"); | ||
if (rs.next()) { | ||
adminId = rs.getString(0); | ||
} else { | ||
throw new IllegalStateException("No admin id"); | ||
} | ||
} | ||
|
||
if (adminId == null) { | ||
throw new IllegalStateException("No admin id"); | ||
} | ||
|
||
return adminId.equals(userId); | ||
} | ||
|
||
private boolean checkUserIsAdminDev(String userId, String adminName) { | ||
String adminId; | ||
if ("root_1".equals(adminName)) { | ||
adminId = "1"; | ||
} else if ("root_2".equals(adminName)) { | ||
adminId = "2"; | ||
} else { | ||
adminId = "0"; | ||
} | ||
|
||
return adminId.equals(userId); | ||
} | ||
|
||
} |
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.