Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b29ecdaaac |
@@ -13,41 +13,48 @@ if ($conn->connect_error) {
|
|||||||
die("Conexión fallida: " . $conn->connect_error);
|
die("Conexión fallida: " . $conn->connect_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mitigación: uso de prepared statements y validación de entrada
|
/*
|
||||||
|
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'])) {
|
if(isset($_GET['id'])) {
|
||||||
$id_raw = $_GET['id'];
|
$id = $_GET['id']; // Input del usuario tomado directamente desde la URL
|
||||||
// Validar que el id es un entero
|
|
||||||
if (filter_var($id_raw, FILTER_VALIDATE_INT) === false) {
|
// Whitelist/validate: allow only digits to prevent injection
|
||||||
echo "ID inválido";
|
if (!preg_match('/^\d+$/', $id)) {
|
||||||
|
echo "Invalid id";
|
||||||
} else {
|
} else {
|
||||||
$id = (int)$id_raw;
|
$id_int = (int)$id;
|
||||||
// Preparar consulta con parámetros y seleccionar columnas específicas
|
|
||||||
if ($stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?")) {
|
// Use a prepared statement to avoid SQL injection
|
||||||
$stmt->bind_param("i", $id);
|
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
||||||
|
if ($stmt) {
|
||||||
|
$stmt->bind_param("i", $id_int);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->bind_result($res_id, $res_nombre);
|
$result = $stmt->get_result();
|
||||||
$found = false;
|
|
||||||
while ($stmt->fetch()) {
|
if ($result && $result->num_rows > 0) {
|
||||||
$found = true;
|
while($row = $result->fetch_assoc()) {
|
||||||
// Escapar salida para prevenir XSS
|
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
|
||||||
echo "id: " . htmlspecialchars($res_id, ENT_QUOTES, 'UTF-8') . " - Nombre: " . htmlspecialchars($res_nombre, ENT_QUOTES, 'UTF-8') . "<br>";
|
}
|
||||||
}
|
} else {
|
||||||
if (!$found) {
|
|
||||||
echo "0 resultados";
|
echo "0 resultados";
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
} else {
|
} else {
|
||||||
// No revelar detalles internos al usuario
|
// Do not execute an unsafe query if prepare() failed
|
||||||
echo "Error en la consulta";
|
echo "Query preparation failed";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mitigación de Cross-Site Scripting (XSS)
|
|
||||||
// Escapar la salida del mensaje para evitar ejecución de HTML/JS
|
// 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'];
|
$mensaje = $_GET['mensaje']; // Input del usuario susceptible a XSS
|
||||||
echo "<div>" . htmlspecialchars($mensaje, ENT_QUOTES, 'UTF-8') . "</div>";
|
echo "<div>$mensaje</div>"; // Vulnerable a XSS
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cerrar conexión
|
// Cerrar conexión
|
||||||
|
|||||||
Reference in new issue
Block a user