Skip to content

Commit 8c1f5bb

Browse files
committed
added nosql sanitization
1 parent 06c2b44 commit 8c1f5bb

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

backend/routes/data.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ const mongoose = require('mongoose');
44
const { ObjectId } = require('mongodb');
55
const verifyApiKey = require('../middleware/verifyApiKey');
66

7+
8+
// sanitizer to remove keys starting with $
9+
const sanitize = (obj) => {
10+
const clean = {};
11+
for (const key in obj) {
12+
if (!key.startsWith('$')) {
13+
clean[key] = obj[key];
14+
}
15+
}
16+
return clean;
17+
};
18+
19+
20+
21+
22+
723
// Dynamic POST Route
824
// Example: POST /api/data/products
925
router.post('/:collectionName', verifyApiKey, async (req, res) => {
@@ -43,6 +59,9 @@ router.post('/:collectionName', verifyApiKey, async (req, res) => {
4359
}
4460
}
4561

62+
const safeData = sanitize(cleanData);
63+
cleanData = safeData;
64+
4665
// --- NEW: CHECK DB LIMIT ---
4766
// Calculate approx size in bytes
4867
const docSize = Buffer.byteLength(JSON.stringify(cleanData));
@@ -214,7 +233,11 @@ router.put('/:collectionName/:id', verifyApiKey, async (req, res) => {
214233
}
215234
// String check is loose in JS, usually fine.
216235
}
217-
// ---------------------------------------
236+
237+
238+
const safeData = sanitize(incomingData);
239+
incomingData = safeData;
240+
218241

219242
const finalCollectionName = `${project._id}_${collectionName}`;
220243

backend/tests/auth.test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const mongoose = require('mongoose');
33
const app = require('../app');
44
const Developer = require('../models/Developer');
55
const bcrypt = require('bcryptjs');
6-
require('dotenv').config();
6+
require('dotenv').config();
77

88
// --- SETUP ---
99
beforeAll(async () => {
@@ -13,17 +13,14 @@ beforeAll(async () => {
1313
throw new Error("TEST_MONGO_URL not defined in .env");
1414
}
1515

16-
// Disconnect any existing connection first
1716
await mongoose.disconnect();
1817
await mongoose.connect(uri);
1918
});
2019

2120
afterAll(async () => {
22-
// Clean up and close connection
2321
await mongoose.connection.close();
2422
});
2523

26-
// --- TESTS ---
2724
describe('Auth API Security', () => {
2825

2926
// Har test se pehle database saaf karein

0 commit comments

Comments
 (0)