Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e13f75cfb5 |
@@ -17,33 +17,31 @@ if ($conn->connect_error) {
|
|||||||
// 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.
|
// 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'])) {
|
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) {
|
// Whitelist validation: ensure the id consists only of digits
|
||||||
echo "Entrada 'id' inválida";
|
if (!ctype_digit($id)) {
|
||||||
|
echo "0 resultados";
|
||||||
} else {
|
} else {
|
||||||
$id = (int)$id;
|
$id_int = (int)$id;
|
||||||
|
|
||||||
// Use a prepared statement to avoid SQL injection
|
// Use a prepared statement to avoid SQL injection
|
||||||
$stmt = $conn->prepare("SELECT * FROM usuarios WHERE id = ?");
|
$stmt = $conn->prepare("SELECT id, nombre FROM usuarios WHERE id = ?");
|
||||||
if ($stmt) {
|
if ($stmt) {
|
||||||
$stmt->bind_param("i", $id);
|
$stmt->bind_param("i", $id_int);
|
||||||
$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>";
|
echo "id: " . $row["id"]. " - Nombre: " . $row["nombre"]. "<br>";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
echo "0 resultados";
|
echo "0 resultados";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
} else {
|
} else {
|
||||||
echo "Error al preparar la consulta";
|
// If prepare fails, do not run an unsafe query; return no results
|
||||||
|
echo "0 resultados";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user