Hallucination Attacks in Mongodb
How Hallucination Attacks Manifests in Mongodb
Hallucination attacks in MongoDB contexts occur when AI systems or LLM integrations generate fabricated database operations, schema definitions, or query structures that don't exist in reality. These attacks exploit the trust relationship between AI assistants and database systems, causing applications to execute operations on non-existent collections, fields, or documents.
The most common MongoDB hallucination pattern involves AI-generated queries that reference phantom collections. For example, an AI assistant might generate:
db.nonexistentCollection.find({ userId: 123 })This appears legitimate but targets a collection that doesn't exist in the database schema. When executed, it either returns empty results or throws errors, potentially leaking information about database structure through error messages.
Another manifestation involves fabricated field names in MongoDB queries. AI systems might generate:
db.users.find({ nonExistentField: { $exists: true } })These phantom field references can cause applications to behave unpredictably, potentially exposing default values or triggering schema validation bypass mechanisms.
Hallucination attacks also manifest through fabricated aggregation pipelines. An AI assistant might generate complex pipelines referencing stages or operators that don't exist:
db.users.aggregate([
{ $nonExistentStage: { $field: "value" } },
{ $sort: { age: 1 } }
])When executed, these pipelines either fail or produce unexpected results, potentially exposing internal database state through error messages or fallback behaviors.
Schema hallucination represents another critical attack vector. AI systems might generate operations based on fabricated schema definitions:
db.users.insertOne({
username: "testuser",
email: "[email protected]",
nonExistentField: "phantom data"
})These operations can trigger schema validation errors, potentially exposing validation rules or default configurations through error responses.
Mongodb-Specific Detection
Detecting hallucination attacks in MongoDB requires monitoring for patterns that deviate from expected database operations. The first indicator is query execution against non-existent collections or databases.
Database audit logs provide the primary detection mechanism. Monitor for queries against collections that don't exist in your schema registry:
db.adminCommand({ listCollections: 1, authorizedCollections: 1 })Compare executed queries against your known schema. Any operation targeting collections not returned by this command warrants investigation.
Schema validation failures serve as another detection signal. Enable strict schema validation on collections:
db.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["username", "email"],
properties: {
username: { bsonType: "string" },
email: { bsonType: "string", pattern: "^[^@\s]+@[^@\s]+\.[^@\s]+$" }
}
}
},
validationLevel: "strict",
validationAction: "error"
})Monitor for validation errors that indicate operations attempting to insert or update with fabricated field structures.
Query pattern analysis helps identify hallucination attempts. Monitor for:
- Queries with field names that don't match any known schema
- Aggregation pipelines using non-existent stages
- Operations targeting databases or collections outside your application's scope
- Queries with unusual operator combinations or nesting patterns
middleBrick's MongoDB scanning specifically targets these hallucination vulnerabilities. The scanner analyzes your API endpoints for:
- Dynamic query construction that could incorporate AI-generated content
- Missing schema validation on write operations
- Error message exposure that could leak schema information
- Unrestricted aggregation pipeline execution
The scanner tests for prompt injection vulnerabilities that could cause AI assistants to generate malicious MongoDB operations, then provides specific remediation guidance based on the findings.
Mongodb-Specific Remediation
Remediating hallucination attack vulnerabilities in MongoDB requires a defense-in-depth approach combining schema validation, query whitelisting, and proper error handling.
Implement strict schema validation on all collections. Define comprehensive schemas using MongoDB's JSON Schema validation:
db.createCollection("products", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "price", "category"],
properties: {
name: { bsonType: "string", minLength: 1 },
price: { bsonType: "double", minimum: 0 },
category: { enum: ["electronics", "clothing", "books", "other"] },
description: { bsonType: "string" }
}
}
},
validationLevel: "strict",
validationAction: "error"
})This prevents insertion of documents with fabricated fields or invalid data structures.
Implement query whitelisting at the application layer. Rather than constructing queries dynamically from user input or AI-generated content, use parameterized queries with predefined field whitelists:
const allowedFields = ["username", "email", "age", "createdDate"];
function buildSafeQuery(input) {
const query = {};
for (const [key, value] of Object.entries(input)) {
if (allowedFields.includes(key)) {
query[key] = value;
}
}
return query;
}
// Usage
const userInput = { username: "test", nonExistentField: "malicious" };
const safeQuery = buildSafeQuery(userInput);
// Result: { username: "test" } - malicious field removedSanitize aggregation pipelines by validating pipeline stages against allowed operations:
const allowedStages = ["$match", "$sort", "$limit", "$skip"];
function validatePipeline(pipeline) {
return pipeline.every(stage => {
const stageName = Object.keys(stage)[0];
return allowedStages.includes(stageName);
});
}Implement proper error handling to prevent schema information leakage:
try {
const result = await collection.find(query).toArray();
return { success: true, data: result };
} catch (error) {
// Log detailed error internally
console.error("Database error:", error.message);
// Return generic error to client
return { success: false, error: "Database operation failed" };
}Use MongoDB's built-in role-based access control to limit operations:
// Create restricted user role
db.createRole({
role: "app_user",
privileges: [
{ resource: { db: "myapp", collection: "users" }, actions: ["find", "findOne"] },
{ resource: { db: "myapp", collection: "products" }, actions: ["find"] }
],
roles: []
});This ensures even if hallucination attacks succeed, they're limited to predefined operations on specific collections.
Related CWEs: llmSecurity
| CWE ID | Name | Severity |
|---|---|---|
| CWE-754 | Improper Check for Unusual or Exceptional Conditions | MEDIUM |