Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ccd457a287 |
@@ -13,48 +13,56 @@ if ($conn->connect_error) {
|
|||||||
die("Conexión fallida: " . $conn->connect_error);
|
die("Conexión fallida: " . $conn->connect_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// ejemplo Julian
|
||||||
Brief analysis - ALEX:
|
|
||||||
This change fixes a SQL Injection vulnerability by using a parameterized prepared statement and strict validation of the 'id' parameter.
|
|
||||||
I will replace the concatenated SQL with a mysqli prepared statement binding the id as an integer; only index.php is modified.
|
|
||||||
*/
|
|
||||||
if(isset($_GET['id'])) {
|
|
||||||
$id = $_GET['id']; // Input del usuario tomado directamente desde la URL
|
|
||||||
|
|
||||||
// Whitelist/validate: allow only digits to prevent injection
|
// Corrección: Uso de sentencias preparadas para evitar SQL Injection
|
||||||
if (!preg_match('/^\d+$/', $id)) {
|
if (isset($_GET['id'])) {
|
||||||
echo "Invalid id";
|
// Validar y sanitizar el parámetro 'id'
|
||||||
|
$id_raw = $_GET['id'];
|
||||||
|
$id_valid = filter_var($id_raw, FILTER_VALIDATE_INT);
|
||||||
|
|
||||||
|
if ($id_valid === false) {
|
||||||
|
// El id no es un entero válido
|
||||||
|
echo "ID inválido";
|
||||||
} else {
|
} else {
|
||||||
$id_int = (int)$id;
|
$id = (int) $id_valid;
|
||||||
|
|
||||||
// Use a prepared statement to avoid SQL injection
|
// Preparar la sentencia para evitar inyección SQL
|
||||||
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
||||||
if ($stmt) {
|
if ($stmt === false) {
|
||||||
$stmt->bind_param("i", $id_int);
|
// Error en la preparación
|
||||||
|
error_log("Fallo al preparar la consulta: " . $conn->error);
|
||||||
|
echo "Error en la consulta";
|
||||||
|
} else {
|
||||||
|
$stmt->bind_param("i", $id);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$result = $stmt->get_result();
|
|
||||||
|
|
||||||
if ($result && $result->num_rows > 0) {
|
// Obtener resultados usando bind_result para mayor compatibilidad
|
||||||
while($row = $result->fetch_assoc()) {
|
$stmt->bind_result($res_id, $res_nombre);
|
||||||
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
|
|
||||||
}
|
$hasResults = false;
|
||||||
} else {
|
while ($stmt->fetch()) {
|
||||||
|
$hasResults = true;
|
||||||
|
// Escapar la salida para prevenir XSS
|
||||||
|
$safe_nombre = htmlspecialchars($res_nombre, ENT_QUOTES, 'UTF-8');
|
||||||
|
$safe_id = htmlspecialchars((string)$res_id, ENT_QUOTES, 'UTF-8');
|
||||||
|
echo "id: " . $safe_id . " - Nombre: " . $safe_nombre . "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $hasResults) {
|
||||||
echo "0 resultados";
|
echo "0 resultados";
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
} else {
|
|
||||||
// Do not execute an unsafe query if prepare() failed
|
|
||||||
echo "Query preparation failed";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Corregir vulnerabilidad XSS al mostrar 'mensaje'
|
||||||
// El siguiente código es vulnerable a XSS ya que imprime directamente en el HTML el contenido de una variable que puede ser manipulada por el usuario sin ninguna sanitización.
|
if (isset($_GET['mensaje'])) {
|
||||||
if(isset($_GET['mensaje'])) {
|
|
||||||
$mensaje = $_GET['mensaje']; // Input del usuario susceptible a XSS
|
$mensaje = $_GET['mensaje']; // Input del usuario susceptible a XSS
|
||||||
echo "<div>$mensaje</div>"; // Vulnerable a XSS
|
$safe_mensaje = htmlspecialchars($mensaje, ENT_QUOTES, 'UTF-8');
|
||||||
|
echo "<div>" . $safe_mensaje . "</div>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cerrar conexión
|
// Cerrar conexión
|
||||||
|
|||||||
Reference in new issue
Block a user