Xml External Entities in Firestore
How Xml External Entities Manifests in Firestore
Xml External Entities (XXE) vulnerabilities in Firestore environments typically emerge through XML-based data exchange patterns, particularly when integrating with legacy systems or processing XML exports from Firestore data. While Firestore itself uses JSON for data storage and retrieval, XXE attacks can occur when applications transform Firestore data to XML formats or when interfacing with services that expect XML input.
A common Firestore-specific XXE scenario occurs during data export operations. Consider an application that allows users to export Firestore collections as XML reports. If the export functionality uses vulnerable XML parsers without proper configuration, attackers can craft malicious XML documents that trigger external entity processing:
// Vulnerable export function - DO NOT USE
const exportCollectionAsXML = async (collectionPath) => {
const docs = await db.collection(collectionPath).get();
const xml = '<collection>';
docs.forEach(doc => {
xml += `<document id="${doc.id}">`;
Object.entries(doc.data()).forEach(([key, value]) => {
xml += `<${key}>${value}</${key}>`;
});
xml += '</document>';
});
xml += '</collection>';
return xml;
};The vulnerability manifests when this XML is processed by downstream systems using unsafe parsers. An attacker could inject:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "file:///etc/passwd">
<!ENTITY xxe2 SYSTEM "http://attacker.com/xxe">
]>
<export><data>&xxe;&xxe2;</data></export>Another Firestore-specific XXE vector appears in webhook integrations. Applications often use Firestore triggers to process data and send XML-formatted payloads to external services:
// Vulnerable webhook trigger
exports.sendXMLWebhook = functions.firestore
.document('orders/{orderId}')
.onCreate(async (snap, context) => {
const orderData = snap.data();
const xmlPayload = `<order>
<id>${context.params.orderId}</id>
<customer>${orderData.customerName}</customer>
<amount>${orderData.total}</amount>
</order>`;
// Vulnerable XML parsing on the receiving end
await fetch('https://webhook-service.example.com', {
method: 'POST',
headers: { 'Content-Type': 'application/xml' },
body: xmlPayload
});
});The receiving webhook service might use vulnerable XML parsers, allowing XXE attacks even though Firestore itself never processes XML. This represents a classic supply-chain vulnerability where Firestore acts as the data source but the XML processing occurs elsewhere in the application ecosystem.
Firestore-Specific Detection
Detecting XXE vulnerabilities in Firestore-based applications requires examining both the data flow and the XML processing components. middleBrick's black-box scanning approach is particularly effective for this scenario since it tests the unauthenticated attack surface without requiring access credentials.
When scanning Firestore-integrated applications, middleBrick specifically looks for:
XML Input Endpoints - Any API endpoints accepting XML content are tested with XXE payloads. This includes file upload endpoints, webhook receivers, and configuration interfaces that might process XML:
{
"endpoint": "POST /api/upload-xml",
"test_case": "XXE Injection",
"payload": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE foo [\n <!ENTITY xxe SYSTEM \"file:///etc/passwd\">\n]>\n<data>&xxe;</data>",
"vulnerability": "XML External Entity Processing",
"severity": "high"
}XML Export Functionality - middleBrick tests export features that might generate XML from Firestore data, attempting to inject malicious content that could be processed by vulnerable downstream parsers.
Webhook Processing - For applications using Firestore triggers to send XML to external services, middleBrick analyzes the request/response patterns to identify potential XXE vectors in the communication chain.
The scanner's LLM/AI security module adds another layer of detection for modern Firestore applications that use AI integrations. Many AI-powered applications store prompts and model configurations in Firestore, creating potential XXE vectors if these configurations are processed as XML:
// AI configuration stored in Firestore - potential XXE vector
const aiConfig = {
systemPrompt: '<?xml version="1.0"?><prompt>Hello</prompt>',
modelParameters: { temperature: 0.7, maxTokens: 2000 }
};
await db.collection('ai-configs').doc('gpt-4').set(aiConfig);middleBrick's continuous monitoring (Pro plan) automatically rescans APIs on configurable schedules, catching XXE vulnerabilities that might be introduced through code changes or new integrations. The GitHub Action integration allows teams to fail builds if XXE-related security scores drop below acceptable thresholds.
Firestore-Specific Remediation
Remediating XXE vulnerabilities in Firestore applications requires a defense-in-depth approach that addresses both the XML processing components and the data flow patterns. The primary strategy is to eliminate XML processing entirely where possible, but when XML is required, use secure parsing libraries and validate all inputs.
Eliminate XML Processing - The most effective remediation is to avoid XML entirely in Firestore applications:
// Instead of XML exports, use JSON
const exportCollectionAsJSON = async (collectionPath) => {
const docs = await db.collection(collectionPath).get();
const result = {
collection: collectionPath,
documents: docs.docs.map(doc => ({
id: doc.id,
data: doc.data()
}))
};
return JSON.stringify(result, null, 2);
};Secure XML Parsing - When XML processing is unavoidable, use secure parser configurations that disable external entity resolution:
// Secure XML parsing using DOMParser with security settings
const parseXMLSecurely = (xmlString) => {
const parser = new DOMParser({
errorHandler: (error) => {
throw new Error(`XML parsing error: ${error.message}`);
}
});
// Create a secure parser configuration
const options = {
resolveExternal: false,
validate: false,
externalResources: false
};
return parser.parseFromString(xmlString, 'application/xml', options);
};Input Validation - Validate all data before processing, especially when transforming Firestore data to XML formats:
// Input validation for XML export
const validateForXMLExport = (data) => {
const sanitized = {};
for (const [key, value] of Object.entries(data)) {
// Remove special characters that could be used in XXE
const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, '_');
if (typeof value === 'string') {
// Encode special characters
sanitized[sanitizedKey] = value
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
} else {
sanitized[sanitizedKey] = value;
}
}
return sanitized;
};Firestore Security Rules - While Firestore security rules don't directly prevent XXE, they can limit the attack surface by controlling data access patterns:
// Firestore security rules to limit XML-related operations
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated users to export data
match /{document=**}/export:
allow read: if request.auth != null;
// Block XML content in specific fields
match /users/{userId}/profile:
allow update: if request.auth.uid == userId &&
!request.resource.data.contentType.matches('.*xml.*');
}
}Monitoring and Alerting - Implement monitoring for suspicious XML processing patterns. The Pro plan's continuous monitoring can alert teams when XXE-related vulnerabilities are detected, while the GitHub Action integration can prevent vulnerable code from reaching production.