# CVE-2016-20018: Knex.js SQL injection via object WHERE clause

Source: https://junglewise.ai/threats/cve-2016-20018-knex-js-sql-injection-via-object-where-clause  
Publisher: Junglewise Threat Intelligence, https://junglewise.ai/threats  
Published: 2022-12-19. Updated: 2026-09-25  
License: CC BY 4.0 with a link back

CVE-2016-20018 · Severity: low · CVSS 3.1 · Published 2022-12-19

Knex.js is a popular SQL query builder used by thousands of Node.js applications to safely construct database queries. Due to improper input validation, attackers can pass JavaScript objects as WHERE clause parameters to bypass query filters and extract unintended data from MySQL databases. This vulnerability allows attackers to ignore security restrictions intended to limit query results, potentially exposing sensitive information.

Technologies: [knex (npm)](https://junglewise.ai/threats/technologies/knex). Vendors: [npm](https://junglewise.ai/threats/vendors/npm).

## Quick answers

**Is CVE-2016-20018 exploited in the wild?** No. Junglewise Threat Intelligence has no report of CVE-2016-20018 being exploited in the wild.

**How severe is CVE-2016-20018?** Low severity, with a CVSS score of 3.1, published 19 December 2022.

**Which products and versions are affected?** knex (npm) SEMVER:introduced=0,fixed=2.4.0.

**Is there a fix for CVE-2016-20018?** Yes. knex (npm): upgrade to 2.4.0 or later. Junglewise finds every place you run it, drafts the upgrade and applies it once you approve.

**How is CVE-2016-20018 attacked?** It is attacked over the network.

**Am I affected by CVE-2016-20018?** Check whether you run knex (npm) at an affected version. Junglewise does that check for you around the clock: connect your code, cloud and domains, and it matches new vulnerabilities like this one against what you actually run and tells you when one reaches you.

## How to fix

- knex (npm): upgrade to 2.4.0 or later

  ```sh
  npm install knex@2.4.0
  ```


## Technical details

Knex.js has a limited SQL injection vulnerability in the WHERE clause handler (CWE-89) affecting versions through 2.3.0. When an attacker passes a JavaScript object or array as a WHERE clause value (e.g., `.where({stringfield: {hello: 1}})`), the code does not validate the input type and instead interpolates the object keys directly into the SQL query. This behavior is specific to the MySQL driver; PostgreSQL and other backends properly reject such values. An attacker with the ability to control WHERE clause parameters (typically through HTTP request parameters) can craft payloads that rewrite WHERE conditions to ignore security filters, potentially reading arbitrary rows from the table. The fix, released in version 2.4.0, adds input type assertions to reject objects and arrays in basic WHERE clause values.

## Affected products

- Knex knex through 2.3.0

## Identifiers

- CVE-2016-20018
- GHSA-4jv9-3563-23j3

## Timeline

- 2016-02-25: disclosed: Vulnerability first reported on GitHub issue #1227
- 2023-01-05: patched: Fix merged in PR #5417
- 2023-01-06: patched: Version 2.4.0 released with fix
- 2022-12-19: advisory: GHSA-4jv9-3563-23j3 published

## References

- [https://github.com/knex/knex/issues/1227](https://github.com/knex/knex/issues/1227)
- [https://github.com/knex/knex/pull/5417](https://github.com/knex/knex/pull/5417)
- [https://github.com/knex/knex/commit/e145322da92749be7749f9ade5b5f5a66d6586a4](https://github.com/knex/knex/commit/e145322da92749be7749f9ade5b5f5a66d6586a4)
- [https://github.com/knex/knex](https://github.com/knex/knex)
- [https://github.com/knex/knex/releases/tag/2.4.0](https://github.com/knex/knex/releases/tag/2.4.0)
- [https://www.ghostccamm.com/blog/knex_sqli](https://www.ghostccamm.com/blog/knex_sqli)

## Related threats

- [knex SQL Injection in MSSQL dialect](https://junglewise.ai/threats/cve-2019-10757-knex-sql-injection-in-mssql-dialect)
