GuardRails

GuardRails

  • Languages iconEnglish
    • 中文

›PHP

Overview

  • Introduction
  • What is GuardRails
  • Getting started
  • Findings
  • Vulnerabilities
  • Configuration
  • Custom Engines
  • False Positives
  • Enforce Passing Checks
  • Build Status on Pull Requests
  • FAQ
  • Pricing
  • Glossary
  • Tools & Licenses

API

  • Usage Guide
  • Endpoints

Vulnerabilities

  • Introduction
  • General

    • Overview
    • Hard-Coded Secrets

    Apex

    • Overview
    • Insecure Access Control
    • Insecure Network Communication
    • Insecure Processing of Data
    • Insecure Use of Cryptography
    • Insecure Use of Language/Framework API
    • Insecure Use of SQL Queries

    C/C++

    • Overview
    • Insecure Access Control
    • Insecure File Management
    • Insecure Processing of Data
    • Insecure Use of Cryptography
    • Insecure Use of Dangerous Function

    Dotnet

    • Overview
    • Insecure Access Control
    • Insecure Configuration
    • Insecure File Management
    • Insecure Processing of Data
    • Insecure Use of Cryptography
    • Insecure Use of Dangerous Function
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Elixir

    • Overview
    • Insecure Configuration
    • Insecure File Management
    • Insecure Processing of Data
    • Insecure Network Communication
    • Insecure Use of Dangerous Function
    • Insecure Use of Language/Framework API
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Go

    • Overview
    • Insecure File Management
    • Insecure Network Communication
    • Insecure Processing of Data
    • Insecure Use of Cryptography
    • Insecure Use of Dangerous Function
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Java

    • Overview
    • Using Vulnerable Libraries
    • Insecure Use of SQL Queries
    • Insecure Use of Dangerous Function
    • Insecure Use of Regular Expressions
    • Insecure Authentication
    • Insecure Configuration
    • Insecure File Management
    • Insecure Use of Cryptography
    • Insecure Use of Language/Framework API
    • Insecure Processing of Data
    • Insecure Network Communication

    Javascript/TypeScript

    • Overview
    • Insecure Authentication
    • Insecure Processing of Data
    • Insecure Use of SQL Queries
    • Insecure Use of Regular Expressions
    • Insecure Use of Language/Framework API
    • Insecure Use of Dangerous Function
    • Using Vulnerable Libraries

    Kubernetes

    • Overview
    • Insecure Access Control
    • Insecure Configuration
    • Insecure Network Communication

    PHP

    • Overview
    • Insecure Configuration
    • Insecure File Management
    • Insecure Network Communication
    • Insecure Processing of Data
    • Insecure Use of Dangerous Function
    • Insecure Use of Language/Framework API
    • Insecure Use of Regular Expressions
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Python

    • Overview
    • Insecure Configuration
    • Insecure Use of Cryptography
    • Insecure Network Communication
    • Insecure Processing of Data
    • Insecure Use of Dangerous Function
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Ruby

    • Overview
    • Insecure Access Control
    • Insecure Configuration
    • Insecure File Management
    • Insecure Network Communication
    • Insecure Processing of Data
    • Insecure Use of Dangerous Function
    • Insecure Use of Language/Framework API
    • Insecure Use of Regular Expressions
    • Insecure Use of SQL Queries
    • Using Vulnerable Libraries

    Rust

    • Overview
    • Using Vulnerable Libraries

    Solidity

    • Overview
    • Insecure Integer Arithmetic
    • Insecure Use of Low-Level Call
    • Reliance on Insecure Random Numbers
    • State Change After External Call
    • Transaction Order Dependence
    • Unprotected Critical Function
    • Use of Insecure Function
    • Dependence on Predictable Environment Variables
    • Write to Arbitrary Storage Location
    • Call to Untrusted Contract

    Terraform

    • Overview
    • Hard-Coded Secrets
    • Insecure Access Control
    • Insecure Configuration
    • Insecure Network Communication
    • Insecure Use of Cryptography

Insecure Use of SQL Queries

Why is this important?

SQL injections are dangerous because they can be easily identified by attackers. Hackers can use SQL injections to read from and sometimes even write to your database. SQL injections are very common and have been the cause of many high-profile breaches.

PHP packages with combined downloads of 50 million were affected by this.

Check out this video for a high-level explanation:

SQL Injection Explanation Video

Read below to find out how to fix it.

Fixing Insecure Use of SQL Queries

Option A: Use Prepared Statements - Mysqli

  1. Setup Mysqli.
  2. Go through the issues that GuardRails identified in the PR.
  3. Replace the code that looks like:
mysql_query("SELECT * from users where email = '" . $_GET['email'] . "';");

with:

// $mysqli instance of mysqli class
if ($stmt = $mysqli->prepare("SELECT * FROM users WHERE email=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $_GET['email']);

    /* execute query */
    $stmt->execute();
    [...]
}
  1. Test it
  2. Ship it 🚢 and relax 🌴

Option B: Use Prepared Statements - PDO

  1. Setup PDO.
  2. Go through the issues that GuardRails identified in the PR.
  3. Replace the code that looks like:
mysql_query("SELECT * from users where email = '" . $_GET['email'] . "';");

with:

// $pdo instance of PDO class
$stmt = $pdo->prepare('SELECT * FROM users WHERE email=?');
$sth->execute($_GET['email']);
$result = $stmt->fetchAll();
  1. Test it
  2. Ship it 🚢 and relax 🌴

Option C: Use Prepared Statements - Doctrine ORM

  1. Install an ORM like doctrine/orm.
  2. Go through the issues that GuardRails identified in the PR.
  3. Replace the code that looks like:
mysql_query("SELECT * from users where email = '" . $_GET['email'] . "';");

with:

// $em instance of EntityManager
$qb = $em->createQueryBuilder();
$qb->select('u')
 ->from('users', 'u')
 ->where('u.email = ?1')
 ->setParameter(1, $_GET['email']);

$query = $qb->getQuery();
$result = $query->getResult();
  1. Test it
  2. Ship it 🚢 and relax 🌴

More information:

  • Getting Started with Doctrine
  • ORM
  • SQL Injection Prevention Cheat Sheet
  • PDO documentation
  • Mysqli documentation
  • Common Weakness Enumeration (CWE-89)
← Insecure Use of Regular ExpressionsUsing Vulnerable Libraries →
  • Why is this important?
  • Fixing Insecure Use of SQL Queries
    • Option A: Use Prepared Statements - Mysqli
    • Option B: Use Prepared Statements - PDO
    • Option C: Use Prepared Statements - Doctrine ORM
  • More information:
  • Status
  • Help
  • Security
  • Terms
  • Privacy

© 2021 GuardRails