mirror of
https://github.com/elicpeter/nyx.git
synced 2026-06-09 19:45:13 +02:00
23 lines
663 B
Java
23 lines
663 B
Java
|
|
import java.sql.*;
|
||
|
|
import java.security.SecureRandom;
|
||
|
|
|
||
|
|
class Negative {
|
||
|
|
// Safe: parameterized query
|
||
|
|
void safeQuery(Connection conn, String user) throws Exception {
|
||
|
|
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE name = ?");
|
||
|
|
ps.setString(1, user);
|
||
|
|
ResultSet rs = ps.executeQuery();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Safe: SecureRandom instead of Random
|
||
|
|
void safeRandom() {
|
||
|
|
SecureRandom sr = new SecureRandom();
|
||
|
|
int token = sr.nextInt();
|
||
|
|
}
|
||
|
|
|
||
|
|
// Safe: no concatenation in SQL
|
||
|
|
void safeLiteralQuery(Statement stmt) throws Exception {
|
||
|
|
stmt.executeQuery("SELECT COUNT(*) FROM users");
|
||
|
|
}
|
||
|
|
}
|