Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8441834cae |
@@ -14,37 +14,30 @@ if ($conn->connect_error) {
|
||||
}
|
||||
|
||||
// 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'])) {
|
||||
$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) {
|
||||
echo "Entrada 'id' inválida";
|
||||
|
||||
// Validar que el id contiene sólo dígitos (evita inyección y coerción de tipo)
|
||||
if (!ctype_digit($id)) {
|
||||
echo "Parámetro inválido.";
|
||||
} else {
|
||||
$id = (int)$id;
|
||||
// Use a prepared statement to avoid SQL injection
|
||||
$stmt = $conn->prepare("SELECT * FROM usuarios WHERE id = ?");
|
||||
if ($stmt) {
|
||||
$stmt->bind_param("i", $id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
// Usar prepared statement para evitar SQL Injection
|
||||
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
||||
$int_id = (int)$id;
|
||||
$stmt->bind_param("i", $int_id);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result === false) {
|
||||
echo "Error en la consulta";
|
||||
} else {
|
||||
if ($result->num_rows > 0) {
|
||||
while($row = $result->fetch_assoc()) {
|
||||
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
|
||||
}
|
||||
} else {
|
||||
echo "0 resultados";
|
||||
}
|
||||
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>";
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
} else {
|
||||
echo "Error al preparar la consulta";
|
||||
echo "0 resultados";
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user