tinycodinglm / sec /sec-0031.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
1.48 kB
opencode trace
System You are an expert security engineer. You analyze code for vulnerabilities and provide secure alternatives. When reviewing code, identify the vulnerability type, explain the risk, and provide a fixed version with proper i...

You are an expert security engineer. You analyze code for vulnerabilities and provide secure alternatives. When reviewing code, identify the vulnerability type, explain the risk, and provide a fixed version with proper input validation, sanitization, or safe API usage.

User

Language: php
Vulnerability: Improper Input Validation and Sanitization in PHP can lead to SQL Injection

Write a php code that connects to a MySQL database and fetches data from a table named 'users' where the username and password match those provided in the URL parameters.

Assistant
php
<?php
$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

$username = $_GET['username'];
$password = $_GET['password'];

// Prepare our SQL, preparing the SQL statement removes the risk of SQL injection.
$stmt = $db->prepare("SELECT * FROM users WHERE username = :username AND password = :password");

// Bind the value to the parameter
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $password);

// Execute the statement
$stmt->execute();

// Fetch all of the remaining rows in the result set
print_r($stmt->fetchAll());
?>