Sql Injection in Feathersjs with Dynamodb
Sql Injection in Feathersjs with Dynamodb — how this specific combination creates or exposes the vulnerability
SQL Injection (SQLi) is commonly associated with relational databases, but when using FeathersJS with DynamoDB, developers can still encounter injection-like issues via query manipulation and unsafe input handling. Although DynamoDB itself is a NoSQL database and does not use SQL, vulnerabilities arise in the application layer when user input is directly embedded into query parameters, especially in the context of condition expressions, filter logic, or when building queries programmatically.
FeathersJS abstracts service interactions and often relies on hooks to transform incoming requests into DynamoDB queries. If these hooks or custom logic concatenate or interpolate user-provided data into query structures without validation or sanitization, attackers can supply crafted input that alters the intended query semantics. For example, a malicious actor might inject values that change filter conditions, bypass intended access controls, or cause excessive scans by manipulating key conditions.
Consider a FeathersJS service that retrieves user records by a numeric user ID. If the service directly uses the request query parameter to build a DynamoDB KeyConditionExpression, an attacker could provide input such as userId=1 OR attribute_exists(aws) in a loosely implemented handler. While DynamoDB does not parse this as SQL, the application might incorrectly treat the string as part of the expression, leading to unintended behavior or exposure of multiple items. Additionally, if the service uses string interpolation to construct FilterExpression values, special characters or logical operators could modify the filtering logic, effectively injecting conditions that broaden result sets or expose sensitive data.
Another vector involves scan operations where user input influences Select or projection behavior. An attacker might attempt to inject expressions that force the query to return more attributes than necessary, increasing data exposure. Since DynamoDB queries rely on precise key schemas, malformed or unexpected input can also trigger unhandled exceptions or error states that reveal internal details in logs or responses. These patterns do not exploit a database engine weakness but highlight insecure coding practices within FeathersJS service implementations.
Even though DynamoDB does not support traditional SQL, the term SQL Injection is sometimes used loosely to describe injection-style manipulation of query structures. The risk in a FeathersJS and DynamoDB stack is real, not because DynamoDB parses SQL, but because untrusted input can distort query construction, bypass intended filters, or expose data through poorly designed access patterns. The combination of FeathersJS's flexibility and DynamoDB's strict query requirements increases the importance of rigorous input validation and schema-aware parameter handling.
To detect such issues, middleBrick performs black-box scanning against the unauthenticated API surface, analyzing how queries respond to varied inputs without assuming internal implementation details. This approach mirrors attacker behavior by testing boundary conditions, logical operators, and malformed payloads to uncover insecure query building practices. Findings from such scans are mapped to relevant categories such as Input Validation and Property Authorization, providing prioritized remediation steps tailored to the stack.
Dynamodb-Specific Remediation in Feathersjs — concrete code fixes
Securing FeathersJS services that interact with DynamoDB requires strict input validation, schema-aware query construction, and avoiding string interpolation for building query components. Below are concrete code examples demonstrating secure practices.
1. Use Parameterized Condition Expressions
Instead of concatenating user input into condition strings, use DynamoDB's expression attribute values to safely bind parameters.
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB.DocumentClient();
// Secure: using ExpressionAttributeValues
const params = {
TableName: 'users',
KeyConditionExpression: 'userId = :uid',
ExpressionAttributeValues: {
':uid': parseInt(req.query.userId, 10) // Ensure numeric type
}
};
dynamodb.query(params, (err, data) => {
if (err) { /* handle error */ }
// process data
});
2. Validate and Restrict Input Types
Ensure incoming values conform to expected formats before using them in queries. Reject unexpected types or malformed values early in the hook chain.
// FeathersJS before hook for validation
app.service('users').hooks({
before: {
find: [context => {
const { userId } = context.query;
if (typeof userId !== 'string' || !/^\d+$/.test(userId)) {
throw new Error('Invalid user ID');
}
context.query.userId = parseInt(userId, 10);
return context;
}]
}
});
3. Avoid Dynamic KeyConditionExpression Building
Do not construct KeyConditionExpression from raw user strings. Define allowed patterns and map inputs to known attributes.
const allowedIndex = {
'byUser': 'userId = :uid',
'byStatus': 'status = :status'
};
const indexName = allowedIndex[req.query.index];
if (!indexName) { throw new Error('Unsupported index'); }
const params = {
TableName: 'events',
IndexName: indexName,
ExpressionAttributeValues: {
':uid': req.query.userId,
':status': req.query.status
}
};
4. Limit Returned Attributes
Use ProjectionExpression to restrict returned data and reduce exposure risk. Do not allow user input to dictate projection contents.
const params = {
TableName: 'users',
KeyConditionExpression: 'userId = :uid',
ProjectionExpression: 'userId, email, createdAt',
ExpressionAttributeValues: {
':uid': req.query.userId
}
};
5. Use middleBrick for Continuous Verification
With the Pro plan, you can enable continuous monitoring to ensure your DynamoDB queries remain secure over time. The middleBrick CLI allows you to scan endpoints from the terminal and integrate checks into scripts, while the GitHub Action can fail builds if security scores drop below your defined threshold. For AI-assisted workflows, the MCP Server lets you scan APIs directly from your coding assistant.
Remediation guidance from scans emphasizes input validation, schema-aware query design, and avoiding dynamic expression assembly. These practices align with secure development principles and help maintain a robust security posture when using FeathersJS and DynamoDB together.
Related CWEs: inputValidation
| CWE ID | Name | Severity |
|---|---|---|
| CWE-20 | Improper Input Validation | HIGH |
| CWE-22 | Path Traversal | HIGH |
| CWE-74 | Injection | CRITICAL |
| CWE-77 | Command Injection | CRITICAL |
| CWE-78 | OS Command Injection | CRITICAL |
| CWE-79 | Cross-site Scripting (XSS) | HIGH |
| CWE-89 | SQL Injection | CRITICAL |
| CWE-90 | LDAP Injection | HIGH |
| CWE-91 | XML Injection | HIGH |
| CWE-94 | Code Injection | CRITICAL |