flawopen.com/संदर्भ/MongoDB NoSQL Injection: जब input query बदल देता है
MongoDB NoSQL Injection: जब input query बदल देता है
MongoDB SQL execute नहीं करता, इसलिए SQL string जोड़ना यहाँ समस्या नहीं है। NoSQL injection तब हो सकता है जब application scalar की जगह अविश्वसनीय nested object स्वीकार करे और उसे MongoDB filter में दे। यह application का input-validation दोष है, MongoDB server CVE नहीं।
सरल भाषा में: value नियम बन जाती है
कल्पना करें कि फ़ॉर्म नाम माँगता है, लेकिन क्लर्क उस बॉक्स में फ़ाइलिंग निर्देशों का पूरा सेट स्वीकार कर लेता है। डेटाबेस एक नाम की तुलना करने के बजाय निर्देशों का पालन करता है। अपेक्षित value type जाँचें और search rule स्वयं बनाएँ।
Trust boundary: request JSON से MongoDB filter तक
- Query filter
- BSON document जिसे MongoDB field comparisons और स्पष्ट रूप से दिए जाने पर query operators के रूप में समझता है।
- Operator injection
- Application उस जगह attacker-controlled structure डालता है जहाँ एक literal value अपेक्षित थी।
- Scalar validation
- Database operation को प्रभावित करने से पहले input का type, length और allowed format जाँचना।
- Projection
- Query से लौटने वाले fields की server-chosen सूची, जो अनचाहे data exposure को सीमित करती है।
दोष कैसे होता है
Endpoint scalar की अपेक्षा करता है
API contract में username एक string है, लेकिन JSON parser strings, arrays, objects, booleans और null भी दे सकता है।
Application type validation छोड़ देता है
Handler parsed value को सीधे server-created filter में डालता है, इसलिए object literal string के बजाय query structure बना रहता है।
MongoDB दी गई structure का मूल्यांकन करता है
Nested document में query operators हों तो predicate इच्छित exact-value comparison से अलग अर्थ दे सकता है। प्रभावित records query और application के authorization checks पर निर्भर हैं।
Response exposure तय करता है
लौटाए गए fields और records की संख्या projection, endpoint व्यवहार और database account privileges पर निर्भर करती है। इससे अपने आप authentication bypass या पूरे database का access सिद्ध नहीं होता।
MongoDB की आधिकारिक driver guidance इस boundary को तब दर्ज करती है जब endpoint बिना validate किए JSON को BSON में बदलकर filter बनाता है। यह guide application-level failure दिखाती है। यह दावा नहीं करती कि हर MongoDB deployment vulnerable है, server CVE नहीं बताती और authentication bypass की विधि नहीं देती।
Node.js में vulnerable और hardened patterns
app.post("/users/lookup", async (req, res, next) => {
try {
// The route expects a string, but req.body.username is untrusted JSON.
const user = await users.findOne({ username: req.body.username });
res.json(user ? { username: user.username } : null);
} catch (error) {
next(error);
}
});
const loginSchema = z.object({
username: z.string().trim().min(1).max(128),
password: z.string().min(1).max(1024)
}).strict();
app.post("/login", async (req, res, next) => {
try {
const parsed = loginSchema.safeParse(req.body);
if (!parsed.success) return res.sendStatus(400);
const { username, password } = parsed.data;
// The application owns the filter key; input supplies only a string value.
const user = await users.findOne(
{ username },
{ projection: { _id: 1, username: 1, passwordHash: 1 } }
);
const passwordMatches = user &&
await argon2.verify(user.passwordHash, password);
if (!passwordMatches) return res.sendStatus(401);
return res.sendStatus(204); // Session creation is application-specific.
} catch (error) {
next(error);
}
});
मजबूत किया गया प्रवाह database access से पहले non-string fields को अस्वीकार करता है, client से filter object स्वीकार नहीं करता, application code में filter key तय करता है, केवल चुने हुए fields लौटाता है और user lookup के बाद password hash verify करता है। उदाहरण मानता है कि z, Zod है; users, MongoDB collection है; और argon2, configured password-hashing library है। Session बनाना जानबूझकर उदाहरण के दायरे से बाहर है।
Fix की जाँच के लिए checklist
- ✓सामान्य string भेजें और पुष्टि करें कि handler केवल application-owned exact field पर query करता है।
- ✓Object, array, number, boolean, null, missing field, बहुत लंबी string और unknown property भेजें; MongoDB call से पहले schema invalid shapes अस्वीकार करे।
- ✓पुष्टि करें कि database call को server-built filter मिले, client-provided filter या raw JSON fragment कभी नहीं।
- ✓जाँचें कि projection password hashes और unrelated fields हटाती है तथा database role को केवल आवश्यक collection permissions हैं।
- ✓Argon2id या bcrypt से password hash करें और application में verify करें; भेजे गए password को MongoDB filter में कभी न डालें।
प्राथमिक स्रोत
- MongoDB — Client Libraries Best Practices: JSON को BSON में बदलने से पहले untrusted input validate करें
- OWASP — NoSQL Security Cheat Sheet
- MongoDB — Query Filter बनाएँ (C# Driver): validation application की ज़िम्मेदारी है