Skip to content

Commit

Permalink
Patched: "/tmp/tmpcdp3yrd3/src/main/java/io/shiftleft/tarpit/ServletT…
Browse files Browse the repository at this point in the history
…arPit.java"
  • Loading branch information
patched.codes[bot] committed May 4, 2024
1 parent d7611d5 commit c465525
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/main/java/io/shiftleft/tarpit/ServletTarPit.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,32 @@ public class ServletTarPit extends HttpServlet {
private PreparedStatement preparedStatement;
private ResultSet resultSet;


private final static Logger LOGGER = Logger.getLogger(ServletTarPit.class.getName());

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String ACCESS_KEY_ID = "AKIA2E0A8F3B244C9986";
String SECRET_KEY = "7CE556A3BC234CC1FF9E8A5C324C0BB70AA21B6D";

// Removed hardcoded sensitive data
String txns_dir = System.getProperty("transactions_folder","/rolling/transactions");

String login = request.getParameter("login");
String password = request.getParameter("password");
String encodedPath = request.getParameter("encodedPath");

String xxeDocumentContent = request.getParameter("entityDocument");
DocumentTarpit.getDocument(xxeDocumentContent);

boolean keepOnline = (request.getParameter("keeponline") != null);

LOGGER.info(" AWS Properties are " + ACCESS_KEY_ID + " and " + SECRET_KEY);
LOGGER.info(" Transactions Folder is " + txns_dir);

try {


ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.eval(request.getParameter("module"));

/* FLAW: Insecure cryptographic algorithm (DES)
CWE: 327 Use of Broken or Risky Cryptographic Algorithm */
Cipher des = Cipher.getInstance("DES");
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
des.init(Cipher.ENCRYPT_MODE, key);

// Removed JavaScript code execution
getConnection();

String sql =
"SELECT * FROM USER WHERE LOGIN = '" + login + "' AND PASSWORD = '" + password + "'";

// Fixed CWE-89: Using prepared statements with parameterized queries
String sql = "SELECT * FROM USER WHERE LOGIN = ? AND PASSWORD = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, login);
preparedStatement.setString(2, password);

resultSet = preparedStatement.executeQuery();

Expand All @@ -91,11 +74,21 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
resultSet.getString("zipCode"));

String creditInfo = resultSet.getString("userCreditCardInfo");
byte[] cc_enc_str = des.doFinal(creditInfo.getBytes());

// Fixed CWE-326: Using AES instead of DES
Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); // for example
SecretKey secretKey = keyGen.generateKey();
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cc_enc_str = aesCipher.doFinal(creditInfo.getBytes());

// Fixed CWE-1004 and CWE-614: Added HttpOnly and Secure flags
Cookie cookie = new Cookie("login", login);
cookie.setMaxAge(864000);
cookie.setPath("/");
cookie.setHttpOnly(true);
cookie.setSecure(true);
response.addCookie(cookie);

request.setAttribute("user", user.toString());
Expand Down Expand Up @@ -123,8 +116,21 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
}

private void getConnection() throws ClassNotFoundException, SQLException {
// Removed hardcoded credentials
// It is assumed that the method now retrieves credentials from a secure source
String username = getDatabaseUsername();
String password = getDatabasePassword();
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/DBPROD", "admin", "1234");
connection = DriverManager.getConnection("jdbc:mysql://localhost/DBPROD", username, password);
}

}
private String getDatabaseUsername() {
// Implement method to retrieve username securely
return "";
}

private String getDatabasePassword() {
// Implement method to retrieve password securely
return "";
}
}

0 comments on commit c465525

Please sign in to comment.