Fix SQL Injection (Legacy & Modern) in Feathers
SQL injection in FeathersJS typically manifests when developers bypass the abstraction layer to run raw SQL via Knex or Sequelize adapters. Even with modern ORMs, improper handling of user-supplied data in raw queries or custom hooks creates critical vulnerabilities. This guide covers neutralizing these vectors by enforcing parameterized inputs and leveraging built-in service abstractions.
The Vulnerable Pattern
// VULNERABLE: Direct string interpolation in a custom service method using Knex
async find(params) {
const { category } = params.query;
const knex = this.app.get('knexClient');
// DANGER: User input is concatenated directly into the SQL string
const result = await knex.raw(`SELECT * FROM products WHERE category = '${category}'`);
return result.rows;
}
The Secure Implementation
The vulnerability exists because the database engine interprets the concatenated string as a single command, allowing an attacker to break out of the quote and append malicious SQL (e.g., ' OR 1=1--). The fix involves using Parameterized Queries where the SQL structure and the data are sent to the DB engine separately. By using the '?' placeholder, the driver ensures the input is treated strictly as a literal value. In Feathers, the most robust defense is sticking to the service's built-in find/get methods, which leverage the underlying adapter's protection mechanisms by default.
// SECURE: Use parameterized queries (bindings) or Feathers built-in query syntax async find(params) { const { category } = params.query; const knex = this.app.get('knexClient');// Option 1: Parameterized Raw Query (Safe) const result = await knex.raw(‘SELECT * FROM products WHERE category = ?’, [category]);
// Option 2: Feathers Service Methods (Best Practice) // This uses the internal query builder which handles sanitization automatically return await this._find({ query: { category: category } }); }
Your Feathers API
might be exposed to SQL Injection (Legacy & Modern)
74% of Feathers apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.