-
Notifications
You must be signed in to change notification settings - Fork 68
feat: Created Filter feature and redesign Database UI #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||||||||||||||||||||||||||||||||||
| class QueryEngine { | ||||||||||||||||||||||||||||||||||||||||
| constructor(query, queryString) { | ||||||||||||||||||||||||||||||||||||||||
| this.query = query; | ||||||||||||||||||||||||||||||||||||||||
| this.queryString = queryString; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| filter() { | ||||||||||||||||||||||||||||||||||||||||
| const queryObj = { ...this.queryString }; | ||||||||||||||||||||||||||||||||||||||||
| const excludedFields = ['page', 'sort', 'limit', 'fields']; | ||||||||||||||||||||||||||||||||||||||||
| excludedFields.forEach(el => delete queryObj[el]); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const mongoQuery = {}; | ||||||||||||||||||||||||||||||||||||||||
| for (const key in queryObj) { | ||||||||||||||||||||||||||||||||||||||||
| if (key.endsWith('_gt')) { | ||||||||||||||||||||||||||||||||||||||||
| const field = key.replace(/_gt$/, ''); | ||||||||||||||||||||||||||||||||||||||||
| mongoQuery[field] = { ...mongoQuery[field], $gt: queryObj[key] }; | ||||||||||||||||||||||||||||||||||||||||
| } else if (key.endsWith('_gte')) { | ||||||||||||||||||||||||||||||||||||||||
| const field = key.replace(/_gte$/, ''); | ||||||||||||||||||||||||||||||||||||||||
| mongoQuery[field] = { ...mongoQuery[field], $gte: queryObj[key] }; | ||||||||||||||||||||||||||||||||||||||||
| } else if (key.endsWith('_lt')) { | ||||||||||||||||||||||||||||||||||||||||
| const field = key.replace(/_lt$/, ''); | ||||||||||||||||||||||||||||||||||||||||
| mongoQuery[field] = { ...mongoQuery[field], $lt: queryObj[key] }; | ||||||||||||||||||||||||||||||||||||||||
| } else if (key.endsWith('_lte')) { | ||||||||||||||||||||||||||||||||||||||||
| const field = key.replace(/_lte$/, ''); | ||||||||||||||||||||||||||||||||||||||||
| mongoQuery[field] = { ...mongoQuery[field], $lte: queryObj[key] }; | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| mongoQuery[key] = queryObj[key]; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| this.query = this.query.find(mongoQuery); | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User-supplied query keys are passed directly into MongoDB filter — potential NoSQL injection vector. An attacker can craft query parameters like Proposed safeguard for (const key in queryObj) {
+ // Strip any keys that could be Mongo operators
+ const baseField = key.replace(/_(gt|gte|lt|lte)$/, '');
+ if (baseField.startsWith('$') || baseField.startsWith('__')) continue;
+
if (key.endsWith('_gt')) {🧰 Tools🪛 Biome (2.4.4)[error] 10-10: This callback passed to forEach() iterable method should not return a value. (lint/suspicious/useIterableCallbackReturn) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+7
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To prevent NoSQL injection, ensure that the value is a primitive. filter() {
const queryObj = { ...this.queryString };
const excludedFields = ['page', 'sort', 'limit', 'fields'];
excludedFields.forEach(el => delete queryObj[el]);
const mongoQuery = {};
for (const key in queryObj) {
// Prevent NoSQL injection by ensuring the value is a primitive
if (typeof queryObj[key] === 'object') continue;
if (key.endsWith('_gt')) {
const field = key.replace(/_gt$/, '');
mongoQuery[field] = { ...mongoQuery[field], $gt: queryObj[key] };
} else if (key.endsWith('_gte')) {
const field = key.replace(/_gte$/, '');
mongoQuery[field] = { ...mongoQuery[field], $gte: queryObj[key] };
} else if (key.endsWith('_lt')) {
const field = key.replace(/_lt$/, '');
mongoQuery[field] = { ...mongoQuery[field], $lt: queryObj[key] };
} else if (key.endsWith('_lte')) {
const field = key.replace(/_lte$/, '');
mongoQuery[field] = { ...mongoQuery[field], $lte: queryObj[key] };
} else {
mongoQuery[key] = queryObj[key];
}
}
this.query = this.query.find(mongoQuery);
return this;
} |
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| sort() { | ||||||||||||||||||||||||||||||||||||||||
| if (this.queryString.sort) { | ||||||||||||||||||||||||||||||||||||||||
| const sortBy = this.queryString.sort.split(',').map(item => { | ||||||||||||||||||||||||||||||||||||||||
| const [field, order] = item.split(':'); | ||||||||||||||||||||||||||||||||||||||||
| if (order && (order === '-1' || order.toLowerCase() === 'desc')) { | ||||||||||||||||||||||||||||||||||||||||
| return `-${field}`; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| return field; | ||||||||||||||||||||||||||||||||||||||||
| }).join(' '); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| this.query = this.query.sort(sortBy); | ||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||
| this.query = this.query.sort('-createdAt'); | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| paginate() { | ||||||||||||||||||||||||||||||||||||||||
| const page = parseInt(this.queryString.page, 10) || 1; | ||||||||||||||||||||||||||||||||||||||||
| const limit = parseInt(this.queryString.limit, 10) || 100; | ||||||||||||||||||||||||||||||||||||||||
| const skip = (page - 1) * limit; | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| this.query = this.query.skip(skip).limit(limit); | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| return this; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No upper bound on A client can pass Proposed fix paginate() {
+ const MAX_LIMIT = 500;
const page = parseInt(this.queryString.page, 10) || 1;
- const limit = parseInt(this.queryString.limit, 10) || 100;
+ const limit = Math.min(parseInt(this.queryString.limit, 10) || 100, MAX_LIMIT);
const skip = (page - 1) * limit;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| module.exports = QueryEngine; | ||||||||||||||||||||||||||||||||||||||||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Query values are always strings — numeric comparisons will silently misbehave.
req.queryvalues are always strings. When you build{ price: { $gt: "5" } }, MongoDB performs a lexicographic comparison, not a numeric one. This meansprice_gt=5won't correctly filter numeric fields (e.g.,"50"is less than"5"lexicographically).You need to coerce values to their appropriate types. At minimum, attempt numeric parsing for comparison operators:
Proposed fix
🤖 Prompt for AI Agents