Skip to content

Instantly share code, notes, and snippets.

@humbleRumble
Created July 18, 2015 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humbleRumble/dba413be6db717e6acc8 to your computer and use it in GitHub Desktop.
Save humbleRumble/dba413be6db717e6acc8 to your computer and use it in GitHub Desktop.

###Possible security implications of accepting user queries

The following code receives a query string from the $_POST variable then it queries the database. If there are results, it will json_encode() those results and output that. If there are no results then nothing will happen, this is to hopefully give attackers as little information as possible.

The MySQL user account only has the SELECT privilege and only on a database deemed to have no private information. The page will also not respond if the user has not authenticated and will track the queries that each user account submits.

Are there any other security holes that I'm missing?

if(isset($_POST) && isset($_POST['q'])) {
    $mysqli = new mysqli($db_config['host'],$db_config['user'],$db_config['pass'],$db_config['name']);
    if(!$mysqli->connect_error && $result = $mysqli->query($_GET['q'])) {
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        echo json_encode($rows);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment