`sql` does not properly escape parameters when building SQL queries, resulting in potential SQLi
The sql query-builder library for Node.js did not escape the limit and offset values when building queries, so a string passed there was inserted into the SQL text verbatim. An attacker who controls those values could inject arbitrary SQL statements into the resulting query.
- Triaged in 31 days
- Fixed in 3 months
- Disclosed after 3 months
I would like to report an SQLi in sql.
It allows to insert potentially user-controlled content into the queries without proper escaping, in cases where that is not verified additionally in the applications that are using sql library.
Module
module name: sql
version: 0.78.0
npm page: https://www.npmjs.com/package/sql
Module Description
sql string builder for node - supports PostgreSQL, mysql, Microsoft SQL Server, Oracle and sqlite dialects.
Module Stats
Stats 345 downloads in the last day 6 659 downloads in the last week 24 915 downloads in the last month
~298980 estimated downloads per year
Vulnerability
Vulnerability Description
sql module constructs SQL queries from structured input, and the problem is that it doesn't do proper escaping of limit/offset parameters, so in setups where those are user-controlled (e.g. received over network) without additional validation that can cause an SQL injection vulnerability.
Steps To Reproduce:
var sql = require('sql');
var user = sql.define({
name: 'users',
columns: ['id', 'name', 'email', 'lastLogin']
});
console.log(user.select(user.star()).from(user).limit('1; drop table users').toQuery().text);
console.log(user.select(user.star()).from(user).offset('1; drop table users').toQuery().text);
Output:
SELECT "users".* FROM "users" LIMIT 1; drop table users
SELECT "users".* FROM "users" OFFSET 1; drop table users
Supporting Material/References:
State all technical information about the stack where the vulnerability was found
- Arch Linux Current
- Node.js 9.5.0
- npm 5.6.0
Wrap up
- I contacted the maintainer to let him know: N
- I opened an issue in the related repository: N
Impact
SQL injection. See https://www.owasp.org/index.php/SQL_Injection
The hacker selected the SQL Injection weakness. This vulnerability type requires contextual information from the hacker. They provided the following answers:
Verified Yes
What exploitation technique did you utilize? Classic / In-Band
Please describe the results of your verification attempt. Observed constructed SQL queries.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the library concatenated the limit and offset values into the query string without validating or escaping them, assuming they would always be numbers.
Impact
Applications that pass user-controlled values into limit or offset without their own validation could be vulnerable to SQL injection; the reporter only observed the constructed query text and did not demonstrate execution against a database.
What the attacker needed
Application passes user input as limit/offset; no extra validation in the app
The fix
The library should validate that limit and offset are integers, or escape them, before placing them in the query.
How it was found
- Load the sql library and define a table with some columns.
- Build a select query on that table and pass a malicious string as the limit value.
- Print the generated query text and observe the injected statement appears unescaped.
- Repeat with the offset value and observe the same result.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A query builder is a helper library that assembles SQL statements from pieces of code. This one pasted the limit and offset numbers straight into the query text without cleaning them, so if a website passed user input into those fields, a malicious value could add extra SQL commands. The reporter showed the generated query text to prove it.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.