CVE-2015-10070: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection') (CWE-89)
value: A vulnerability was found in copperwall Twiddit. It has been rated as critical. This issue affects some unknown processing of the file index.php. The manipulation leads to sql injection. The identifier of the patch is 2203d4ce9810bdaccece5c48ff4888658a01acfc. It is recommended to apply a patch to fix this issue. The identifier VDB-218897 was assigned to this vulnerability.
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-19T10:15Z
Repository: https://github.com/copperwall/twiddit
Language: PHP
Files changed: index.php
Commit date: 2015-03-19 03:01:39 +0000
Commit message: Parameterize queries to prevent against SQL injection

Diff: diff --git a/index.php b/index.php
index 514a1d2..aaea344 100644
--- a/index.php
+++ b/index.php
@@ -35,35 +35,50 @@ $app->get('/signin', function()  use ($app) {
 });
 
 $app->get('/feed', function() use ($app) {
    $db = TwidditDB::db();
    $username = $_COOKIE['user'];
-   $query = "select redditor 
-            from followingRedditors 
-            where '$username' = userName";
-   $result = $db->query($query);
+   $query = <<<EOT
+      SELECT `redditor`
+      FROM `followingRedditors`
+      WHERE `userName` = :username
+EOT;
+
+   $statement = $db->prepare($query);
+   $statement->bindParam(':username', $username);
+   $statement->execute();
+   $result = $statement->fetchAll(PDO::FETCH_ASSOC);
+
    $users = [];
    foreach ($result as $row) {
       $users[] = $row['redditor'];
    }
    
    $comments = Reddit::getComments($users);
 
    echo json_encode($comments);
 });
+
 $app->get('/subreddits', function() use ($app) {
    $db = TwidditDB::db();
    $username = $_COOKIE['user'];
-   $query = "select subreddit 
-            from followingSubreddit 
-            where '$username' = userName";
-   $result = $db->query($query);
-   $subreddit = [];
-   foreach ($result as $row) {
-      $subreddit[] = $row['subreddit'];
+   $query = <<<EOT
+      SELECT `subreddit`
+      FROM `followingSubreddit`
+      WHERE `userName` = :username
+EOT;
+
+   $statement = $db->prepare($query);
+   $statement->bindParam(':username', $username);
+   $statement->execute();
+   $results = $statement->fetchAll(PDO::FETCH_ASSOC);
+
+   $subreddits = [];
+   foreach ($results as $row) {
+      $subreddits[] = $row['subreddit'];
    }
-   $data = Reddit::getSubredditPosts($subreddit);
+   $data = Reddit::getSubredditPosts($subreddits);
 
    echo json_encode($data);
 });
 
 $app->get('/reddit_callback', function() use ($app) {
@@ -105,11 +120,10 @@ $app->post('/login', function() use ($app) {
      setcookie($cookie_name, $cookie_value, time() + 36000); // cookie lasts 60 secs
      $app->redirect('/');
    }
 });
 
-
 $app->post('/signup', function() use ($app) {
    $db = TwidditDB::db();
    $username = $app->request->post('username');
    $password = $app->request->post('password');
 

Vulnerable code:    $query = "select redditor 
            from followingRedditors 
            where '$username' = userName";
   $result = $db->query($query);
   $query = "select subreddit 
            from followingSubreddit 
            where '$username' = userName";
   $result = $db->query($query);
   $subreddit = [];
   foreach ($result as $row) {
      $subreddit[] = $row['subreddit'];
   $data = Reddit::getSubredditPosts($subreddit);

Fixed code:    $query = <<<EOT
      SELECT `redditor`
      FROM `followingRedditors`
      WHERE `userName` = :username
EOT;

   $statement = $db->prepare($query);
   $statement->bindParam(':username', $username);
   $statement->execute();
   $result = $statement->fetchAll(PDO::FETCH_ASSOC);


   $query = <<<EOT
      SELECT `subreddit`
      FROM `followingSubreddit`
      WHERE `userName` = :username
EOT;

   $statement = $db->prepare($query);
   $statement->bindParam(':username', $username);
   $statement->execute();
   $results = $statement->fetchAll(PDO::FETCH_ASSOC);

   $subreddits = [];
   foreach ($results as $row) {
      $subreddits[] = $row['subreddit'];
   $data = Reddit::getSubredditPosts($subreddits);