CVE-2015-10031: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: A vulnerability classified as critical was found in purpleparrots 491-Project. This vulnerability affects unknown code of the file update.php of the component Highscore Handler. The manipulation leads to sql injection. The name of the patch is a812a5e4cf72f2a635a716086fe1ee2b8fa0b1ab. It is recommended to apply a patch to fix this issue. The identifier of this vulnerability is VDB-217648.
CWE description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
CVSS v3 base score: 9.8
Published: 2023-01-08T17:15Z
Repository: https://github.com/purpleparrots/491-Project
Language: PHP
Files changed: update.php
Commit date: 2015-03-10 22:41:40 -0700
Commit message: fixed update.php to prevent sql injections because people are douchebags

Diff: diff --git a/update.php b/update.php
index 56b6b99..0501d0f 100644
--- a/update.php
+++ b/update.php
@@ -1,28 +1,33 @@
 <?php
-	$name = $_GET["name"];
-	$score = $_GET["score"];
 	
-	$username = "alpertmd";
-	$password = "yofer`";
-	$hostname = "repos.insttech.washington.edu";
+	if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
+		$username = "alpertmd";
+		$password = "yofer`";
+		$hostname = "repos.insttech.washington.edu";
 
-	$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
-
-	$selected = mysql_select_db("alpertmd", $dbhandle) or die("Could not select high score db");
+		$dbhandle = mysqli_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
 	
-	$result = mysql_query("SELECT MAX(idHighscores) FROM Highscores");
-	$id = mysql_result($result, 0) + 1;
+		$selected = mysqli_select_db($dbhandle,$username) or die("Could not select high score db");
 	
-	$query = "INSERT INTO Highscores VALUES ($id, '$name', $score)";
-	$result = mysql_query($query);
-		
-	$query = "SELECT idHighscores FROM Highscores WHERE score = (SELECT Min(score) FROM Highscores)";
-	$result = mysql_query($query);
-	$minID = mysql_result($result, 0);
+		$result = mysqli_query($dbhandle, "SELECT MAX(idHighscores) FROM Highscores");
+	
+		$id = mysqli_fetch_row($result)[0] + 1;
+		$name = $_POST["name"];
+		$score = $_POST["score"];
 
+		$query =  mysqli_prepare($dbhandle, "INSERT INTO Highscores Values (?, ?, ?)");
+	
+		mysqli_stmt_bind_param($query, 'isi', $id, $name, $score);
+		mysqli_stmt_execute($query);
+		mysqli_stmt_close($query);
+		
+		$query = "SELECT idHighscores FROM Highscores WHERE score = (SELECT Min(score) FROM Highscores)";
+		$result = mysqli_query($dbhandle, $query);
+		$minID = mysqli_fetch_row($result)[0];
 
-	$query = "DELETE FROM Highscores WHERE idHighscores = $minID";
-	$result = mysql_query($query);
+		$query = "DELETE FROM Highscores WHERE idHighscores = $minID";
+		$result = mysqli_query($dbhandle, $query);
 	
-	mysql_close($dbhandle);
+		mysqli_close($dbhandle);
+	}
 ?>
\ No newline at end of file

Vulnerable code: 	$name = $_GET["name"];
	$score = $_GET["score"];
	$username = "alpertmd";
	$password = "yofer`";
	$hostname = "repos.insttech.washington.edu";
	$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");

	$selected = mysql_select_db("alpertmd", $dbhandle) or die("Could not select high score db");
	$result = mysql_query("SELECT MAX(idHighscores) FROM Highscores");
	$id = mysql_result($result, 0) + 1;
	$query = "INSERT INTO Highscores VALUES ($id, '$name', $score)";
	$result = mysql_query($query);
		
	$query = "SELECT idHighscores FROM Highscores WHERE score = (SELECT Min(score) FROM Highscores)";
	$result = mysql_query($query);
	$minID = mysql_result($result, 0);
	$query = "DELETE FROM Highscores WHERE idHighscores = $minID";
	$result = mysql_query($query);
	mysql_close($dbhandle);
Fixed code: 	if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
		$username = "alpertmd";
		$password = "yofer`";
		$hostname = "repos.insttech.washington.edu";
		$dbhandle = mysqli_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
		$selected = mysqli_select_db($dbhandle,$username) or die("Could not select high score db");
		$result = mysqli_query($dbhandle, "SELECT MAX(idHighscores) FROM Highscores");
	
		$id = mysqli_fetch_row($result)[0] + 1;
		$name = $_POST["name"];
		$score = $_POST["score"];
		$query =  mysqli_prepare($dbhandle, "INSERT INTO Highscores Values (?, ?, ?)");
	
		mysqli_stmt_bind_param($query, 'isi', $id, $name, $score);
		mysqli_stmt_execute($query);
		mysqli_stmt_close($query);
		
		$query = "SELECT idHighscores FROM Highscores WHERE score = (SELECT Min(score) FROM Highscores)";
		$result = mysqli_query($dbhandle, $query);
		$minID = mysqli_fetch_row($result)[0];
		$query = "DELETE FROM Highscores WHERE idHighscores = $minID";
		$result = mysqli_query($dbhandle, $query);
		mysqli_close($dbhandle);
	}