Hallucination Attacks in Feathersjs
How Hallucination Attacks Manifests in Feathersjs
Hallucination attacks in Feathersjs occur when the framework's service architecture and dynamic query generation create opportunities for attackers to manipulate database queries through crafted input. Feathersjs's flexible service model, while powerful, can inadvertently expose APIs to injection-style attacks that cause the system to "hallucinate" results that don't exist in the database or expose data beyond intended boundaries.
The most common manifestation appears in Feathersjs's query syntax, which uses MongoDB-style operators by default. An attacker can exploit this by sending requests with special operators like $where, $regex, or $ne that modify the query's logic. For example, a simple GET request to /users?id=2 might be manipulated to /users?id[$ne]=null, causing the service to return all users instead of just user ID 2.
// Vulnerable Feathersjs service endpoint
app.service('users').find({
query: {
id: req.query.id // Unsanitized user input
}
});This becomes particularly dangerous in Feathersjs's before hooks, where dynamic query construction can introduce vulnerabilities. If a hook constructs queries based on user input without proper validation, it creates an attack surface:
// Vulnerable hook pattern
const { filters } = context.params.query;
const query = { ...filters }; // Direct injection of user filters
Another Feathers-specific vector involves the $select operator, which controls which fields are returned. An attacker can manipulate $select to extract sensitive fields that should be hidden, effectively causing the API to "hallucinate" data exposure that wasn't intended by the API designer.
Feathersjs's real-time features also introduce unique hallucination attack patterns. Through socket.io or Primus connections, attackers can potentially manipulate subscription queries or event filtering, causing the real-time service to broadcast data to unauthorized clients.
Feathersjs-Specific Detection
Detecting hallucination attacks in Feathersjs requires examining both the application code and runtime behavior. Code-level detection focuses on identifying dangerous patterns where user input directly influences query construction without validation.
Static analysis should look for these specific Feathersjs patterns:
// Dangerous: Direct query parameter injection
app.service('items').find({
query: context.params.query // Unsanitized
});Dynamic query construction in hooks is another critical detection point:
// Vulnerable: Building queries from user input
const dangerousPatterns = [
'$where', '$regex', '$ne', '$nin', '$in',
'$exists', '$not', '$gt', '$lt', '$gte', '$lte'
];Runtime detection with middleBrick specifically identifies these Feathersjs vulnerabilities through its black-box scanning approach. The scanner tests for:
- Query parameter injection using Feathers-specific operators
- Authentication bypass through manipulated service calls
- Data exposure via $select and field manipulation
- Rate limiting bypasses through crafted requests
middleBrick's Feathersjs detection includes testing for common injection patterns like:
// middleBrick would test these injection attempts
GET /users?email[$ne]=null
GET /users?age[$gt]=0
GET /users?$select[name,email,address]The scanner also examines OpenAPI specs for Feathersjs applications, identifying service endpoints that accept query parameters without proper validation constraints. This spec analysis catches vulnerabilities that might not be apparent from runtime testing alone.
Logging and monitoring in Feathersjs applications should track unusual query patterns, particularly those involving MongoDB operators or unexpected field selections. Tools like feathers-hooks-logger can help identify suspicious query construction patterns in production.
Feathersjs-Specific Remediation
Remediating hallucination attacks in Feathersjs requires a defense-in-depth approach using the framework's built-in security features. The first line of defense is strict query parameter validation using Feathers's whitelist approach.
// Secure pattern: Whitelisted query parameters
const whitelist = ['id', 'name', 'email'];
const safeQuery = {};
Object.keys(context.params.query).forEach(key => {
if (whitelist.includes(key)) {
safeQuery[key] = context.params.query[key];
}
});
For more sophisticated validation, Feathersjs supports custom query parser hooks that can sanitize and validate incoming queries before they reach the service:
const { disallowQuery } = require('feathers-hooks-common');
const secureQueryHook = disallowQuery([
'$where', '$regex', '$ne', '$nin', '$in',
'$exists', '$not', '$gt', '$lt', '$gte', '$lte'
]);
app.service('users').before({
find: [secureQueryHook],
get: [secureQueryHook]
});Feathersjs's authentication and authorization system provides another layer of protection. Using feathers-authentication hooks with proper role-based access control prevents unauthorized data access:
const { authenticate } = require('@feathersjs/authentication').hooks;
const { iff, isProvider, fastJoin } = require('feathers-hooks-common');
app.service('users').before({
find: iff(
isProvider('external'),
[authenticate('jwt'), requireRole('user')]
)
});For applications using MongoDB, consider implementing a custom query parser that strips dangerous operators:
// Custom query parser to prevent injection
const safeOperators = ['$eq', '$in'];
function sanitizeQuery(query) {
const sanitized = {};
for (const [key, value] of Object.entries(query)) {
if (key.startsWith('$')) {
if (!safeOperators.includes(key)) continue;
}
sanitized[key] = value;
}
return sanitized;
}
// Apply to all services
app.hooks({
before: {
all: [
context => {
context.params.query = sanitizeQuery(context.params.query);
}
]
}
});Field-level security can be implemented using feathers-shield or custom hooks to control which fields are accessible based on user roles:
const fieldPermissions = {
admin: ['id', 'name', 'email', 'ssn', 'salary'],
user: ['id', 'name', 'email'],
guest: ['id', 'name']
};
function restrictFields(context) {
const userRole = context.params.user.role;
const allowedFields = fieldPermissions[userRole];
if (context.params.query.$select) {
context.params.query.$select =
context.params.query.$select.filter(f => allowedFields.includes(f));
}
}
Finally, implement comprehensive logging and monitoring for query patterns. Use feathers-hooks-logger to track all queries and set up alerts for unusual patterns that might indicate attempted hallucination attacks.
Related CWEs: llmSecurity
| CWE ID | Name | Severity |
|---|---|---|
| CWE-754 | Improper Check for Unusual or Exceptional Conditions | MEDIUM |