Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8441834cae |
@@ -14,37 +14,30 @@ if ($conn->connect_error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Vulnerabilidad de SQL Injection
|
// Vulnerabilidad de SQL Injection
|
||||||
// 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.
|
// Este bloque ha sido corregido: se valida/castea el id, se usa sentencia preparada y se codifica la salida para mitigar SQLi y XSS.
|
||||||
if(isset($_GET['id'])) {
|
if(isset($_GET['id'])) {
|
||||||
$id = $_GET['id']; // Input del usuario tomado directamente desde la URL
|
$id = $_GET['id']; // Input del usuario tomado directamente desde la URL
|
||||||
// Validate that id is an integer (whitelist) before using it in a query
|
|
||||||
if (filter_var($id, FILTER_VALIDATE_INT) === false) {
|
// Validar que el id contiene sólo dígitos (evita inyección y coerción de tipo)
|
||||||
echo "Entrada 'id' inválida";
|
if (!ctype_digit($id)) {
|
||||||
|
echo "Parámetro inválido.";
|
||||||
} else {
|
} else {
|
||||||
$id = (int)$id;
|
// Usar prepared statement para evitar SQL Injection
|
||||||
// Use a prepared statement to avoid SQL injection
|
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
||||||
$stmt = $conn->prepare("SELECT * FROM usuarios WHERE id = ?");
|
$int_id = (int)$id;
|
||||||
if ($stmt) {
|
$stmt->bind_param("i", $int_id);
|
||||||
$stmt->bind_param("i", $id);
|
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$result = $stmt->get_result();
|
$result = $stmt->get_result();
|
||||||
|
|
||||||
if ($result === false) {
|
if ($result && $result->num_rows > 0) {
|
||||||
echo "Error en la consulta";
|
|
||||||
} else {
|
|
||||||
if ($result->num_rows > 0) {
|
|
||||||
while($row = $result->fetch_assoc()) {
|
while($row = $result->fetch_assoc()) {
|
||||||
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
|
// Codificar la salida para prevenir XSS
|
||||||
|
echo "id: " . htmlspecialchars($row["id"], ENT_QUOTES, 'UTF-8') . " - Nombre: " . htmlspecialchars($row["nombre"], ENT_QUOTES, 'UTF-8') . "<br>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
echo "0 resultados";
|
echo "0 resultados";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
} else {
|
|
||||||
echo "Error al preparar la consulta";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user