Skip to content
Open
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
16 changes: 16 additions & 0 deletions DBConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.sql.*;

public class DBConnection {
public void getData() {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Hard-coded credentials leak secrets

"user" / "pass" embedded in source control violates OWASP secret-management guidance and surfaced in static analysis. Move them to env-vars or a secrets vault.

-Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
+String url  = System.getenv("DB_URL");
+String user = System.getenv("DB_USER");
+String pass = System.getenv("DB_PASS");
+Connection conn = DriverManager.getConnection(url, user, pass);
🤖 Prompt for AI Agents
In DBConnection.java at line 6, the database username and password are
hard-coded as "user" and "pass", which exposes sensitive credentials. Replace
these hard-coded values by retrieving the username and password from environment
variables or a secure secrets vault. Update the getConnection call to use these
externalized credentials instead of fixed strings.

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
Comment on lines +4 to +15
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Resource leak & unstructured error handling – switch to try-with-resources

Connection, Statement, and ResultSet are never closed; open connections hang until GC and can exhaust the pool. Use try-with-resources and log instead of printStackTrace().

-    public void getData() {
-        try {
-            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
-            Statement stmt = conn.createStatement();
-            ResultSet rs = stmt.executeQuery("SELECT * FROM users");
-            while (rs.next()) {
-                System.out.println(rs.getString("username"));
-            }
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+    public List<String> getUsernames() throws SQLException {
+        List<String> users = new ArrayList<>();
+        String query = "SELECT username FROM users";
+
+        try (Connection conn = DriverManager.getConnection(
+                 System.getenv("DB_URL"),
+                 System.getenv("DB_USER"),
+                 System.getenv("DB_PASS"));
+             PreparedStatement ps = conn.prepareStatement(query);
+             ResultSet rs = ps.executeQuery()) {
+
+            while (rs.next()) {
+                users.add(rs.getString("username"));
+            }
+        }
+        return users;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public void getData() {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<String> getUsernames() throws SQLException {
List<String> users = new ArrayList<>();
String query = "SELECT username FROM users";
try (Connection conn = DriverManager.getConnection(
System.getenv("DB_URL"),
System.getenv("DB_USER"),
System.getenv("DB_PASS"));
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
users.add(rs.getString("username"));
}
}
return users;
}
🧰 Tools
🪛 ast-grep (0.38.1)

[warning] 5-5: A secret is hard-coded in the application. Secrets stored in source code, such as credentials, identifiers, and other types of sensitive data, can be leaked and used by internal or external malicious actors. Use environment variables to securely provide credentials and other secrets or retrieve them from a secure vault or Hardware Security Module (HSM).
Context: DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass")
Note: [CWE-798] Use of Hard-coded Credentials. [REFERENCES]
- https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html

(drivermanager-hardcoded-secret-java)

🤖 Prompt for AI Agents
In DBConnection.java around lines 4 to 15, the Connection, Statement, and
ResultSet are not closed, causing resource leaks. Refactor the getData method to
use try-with-resources for these objects to ensure they are automatically
closed. Replace e.printStackTrace() with proper logging of the SQLException to
improve error handling.

}