Compare commits

..
Author SHA1 Message Date
PlexicusAI e13f75cfb5 SQL Injection 2026-03-24 18:40:59 +00:00
+13 -8
View File
@@ -14,30 +14,35 @@ if ($conn->connect_error) {
}
// Vulnerabilidad de SQL Injection
// Este bloque ha sido corregido: se valida/castea el id, se usa sentencia preparada y se codifica la salida para mitigar SQLi y XSS.
// El siguiente código es vulnerable a SQL Injection ya que el input del usuario se concatena directamente en la consulta SQL sin validación o sanitización.
if(isset($_GET['id'])) {
$id = $_GET['id']; // Input del usuario tomado directamente desde la URL
// Validar que el id contiene sólo dígitos (evita inyección y coerción de tipo)
// Whitelist validation: ensure the id consists only of digits
if (!ctype_digit($id)) {
echo "Parámetro inválido.";
echo "0 resultados";
} else {
// Usar prepared statement para evitar SQL Injection
$id_int = (int)$id;
// Use a prepared statement to avoid SQL injection
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
$int_id = (int)$id;
$stmt->bind_param("i", $int_id);
if ($stmt) {
$stmt->bind_param("i", $id_int);
$stmt->execute();
$result = $stmt->get_result();
if ($result && $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Codificar la salida para prevenir XSS
echo "id: " . htmlspecialchars($row["id"], ENT_QUOTES, 'UTF-8') . " - Nombre: " . htmlspecialchars($row["nombre"], ENT_QUOTES, 'UTF-8') . "<br>";
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
}
} else {
echo "0 resultados";
}
$stmt->close();
} else {
// If prepare fails, do not run an unsafe query; return no results
echo "0 resultados";
}
}
}