-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: Harden MongoDB query and options handling (fixes #58) #59
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aac456f
Fix: Harden MongoDB query and options handling (fixes #58)
taylortom e1007c4
Update: Use custom AdaptError for blocked query operators (refs #58)
taylortom bf48cad
Fix: Remove stale parseOptions tests replaced by processParams
taylortom 5a756da
Fix: deep-clone query/data before conversion; strict integer validati…
taylortom 1d4cfd5
Fix: replace structuredClone with deepClone to preserve ObjectId inst…
taylortom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| export { assertSafeQuery } from './utils/assertSafeQuery.js' | ||
| export { convertObjectIds } from './utils/convertObjectIds.js' | ||
| export { createObjectId } from './utils/createObjectId.js' | ||
| export { findDuplicates, getFieldNames } from './utils/findDuplicates.js' | ||
| export { isObjectId } from './utils/isObjectId.js' | ||
| export { isValidObjectId } from './utils/isValidObjectId.js' | ||
| export { parseObjectId } from './utils/parseObjectId.js' | ||
| export { convertObjectIds } from './utils/convertObjectIds.js' | ||
| export { processParams } from './utils/processParams.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Operators that allow arbitrary JavaScript execution on the MongoDB server. | ||
| * @type {Set<string>} | ||
| */ | ||
| const BLOCKED_OPERATORS = new Set(['$where', '$accumulator', '$function']) | ||
|
|
||
| /** | ||
| * Recursively checks a MongoDB query object for dangerous operators that could | ||
| * allow arbitrary code execution on the server. | ||
| * @param {*} input The query object (or nested value) to check | ||
| * @param {ErrorsModule} errors The app errors object | ||
| * @throws {AdaptError} If a blocked operator is found | ||
| * @memberof mongodb | ||
| */ | ||
| export function assertSafeQuery (input, errors) { | ||
| if (Array.isArray(input)) { | ||
| for (const item of input) { | ||
| assertSafeQuery(item, errors) | ||
| } | ||
| return | ||
| } | ||
| if (input === null || typeof input !== 'object') { | ||
| return | ||
| } | ||
| for (const key of Object.keys(input)) { | ||
| if (BLOCKED_OPERATORS.has(key)) { | ||
| throw errors.MONGO_BLOCKED_OPERATOR.setData({ operator: key }) | ||
| } | ||
| assertSafeQuery(input[key], errors) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { App, isObject } from 'adapt-authoring-core' | ||
| import { assertSafeQuery } from './assertSafeQuery.js' | ||
| import { convertObjectIds } from './convertObjectIds.js' | ||
|
|
||
| function deepClone (v) { | ||
| if (Array.isArray(v)) return v.map(deepClone) | ||
| if (v !== null && typeof v === 'object' && v.constructor === Object) { | ||
| return Object.fromEntries(Object.entries(v).map(([k, v2]) => [k, deepClone(v2)])) | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| /** | ||
| * Validators for each allowed MongoDB driver option. | ||
| * Each function returns a parsed value if valid, or undefined to strip the option. | ||
| * @type {Object<string, function>} | ||
| */ | ||
| const OPTION_VALIDATORS = { | ||
| collation: v => isObject(v) ? v : undefined, | ||
| includeResultMetadata: v => typeof v === 'boolean' ? v : undefined, | ||
| limit: v => { const n = Number(v); return Number.isInteger(n) ? n : undefined }, | ||
| projection: v => isObject(v) ? v : undefined, | ||
| returnCursor: v => typeof v === 'boolean' ? v : undefined, | ||
| returnDocument: v => v === 'before' || v === 'after' ? v : undefined, | ||
| skip: v => { const n = Number(v); return Number.isInteger(n) ? n : undefined }, | ||
| sort: v => isObject(v) ? v : undefined, | ||
| upsert: v => typeof v === 'boolean' ? v : undefined | ||
| } | ||
|
|
||
| /** | ||
| * Validates and normalises query, data, and options before a database operation. | ||
| * Returns deep copies so the caller's originals are not mutated. | ||
| * @param {Object} params | ||
| * @param {Object} [params.query] The query object to validate and convert | ||
| * @param {Object} [params.data] The data object to convert and sanitise | ||
| * @param {Object} [params.options] The options object to parse | ||
| * @returns {{ query: Object, data: Object, options: Object }} | ||
| * @memberof mongodb | ||
| */ | ||
| export function processParams ({ query, data, options } = {}) { | ||
| const result = {} | ||
|
|
||
| if (query) { | ||
| result.query = deepClone(query) | ||
| assertSafeQuery(result.query, App.instance.errors) | ||
| convertObjectIds(result.query) | ||
| } | ||
|
|
||
| if (data) { | ||
| result.data = deepClone(data) | ||
| convertObjectIds(result.data) | ||
| if (!options?.preserveId) { | ||
| delete result.data._id | ||
| if (result.data.$set) delete result.data.$set._id | ||
| } | ||
| } | ||
|
taylortom marked this conversation as resolved.
|
||
|
|
||
| if (options) { | ||
| result.options = {} | ||
| for (const [key, value] of Object.entries(options)) { | ||
| const validate = OPTION_VALIDATORS[key] | ||
| if (!validate) continue | ||
| const parsed = validate(value) | ||
| if (parsed !== undefined) { | ||
| result.options[key] = parsed | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.