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.

Also, NPM packages with combined monthly downloads of 3 million were affected by this.

Check out this video for a high-level explanation:

SQL Injection Explanation Video

This is a biggie, read below to find out how to fix it.

Fixing Insecure Use of SQL Queries

Option A: Use ORM libraries

  1. Install an ORM like sequelize.
  2. Go through the issues that GuardRails identified in the PR.
  3. Replace the code that looks like:

    "SELECT * from users where email = " + req.params.email + ";";
    

    with:

    sequelize
    .query("SELECT * FROM user WHERE email = :email ", {
      raw: true,
      replacements: { status: req.params.email }
    })
    .then(projects => {
      // Do your stuff
    });
    
  4. Test it

  5. Ship it 🚢 and relax 🌴

Option B: Validate input with Joi

  1. Install JOI.
  2. Go through the issues that GuardRails identified in the PR.
  3. Replace the code that looks like:

    "SELECT * from users where email = " + req.params.email + ";";
    

    with:

    const Joi = require("joi");
    
    const user_schema = Joi.object().keys({
    email: Joi.string().email();
    });
    Joi.validate({ email: req.params.email }, user_schema, function(error, val) {
    if (error == null) {
      "SELECT * from users where email = " + req.params.email + ";";
      //do stuff
    } else {
      //catch validation error
    }
    });
    
  4. Test it

  5. Ship it 🚢 and relax 🌴

More information: