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
make modifiedPaths
safer.
#12719
make modifiedPaths
safer.
#12719
Conversation
lib/helpers/common.js
Outdated
@@ -67,7 +67,19 @@ function flatten(update, path, options, schema) { | |||
* ignore | |||
*/ | |||
|
|||
function modifiedPaths(update, path, result) { | |||
function modifiedPaths(update, path, result, recursion=null) { |
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.
Will this not work?
function modifiedPaths(update, path, result, recursion=null) { | |
function modifiedPaths(update, path, result, recursion={count: 0, raw: {update, path}}) { |
lib/helpers/common.js
Outdated
|
||
recursion.count++; | ||
if(recursion.count >= 1024){ | ||
throw new Error(`Mongoose: bad update value, ${recursion.raw.update}, ${recursion.raw.path}`); |
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.
Maybe create a new MongooseError?
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.
I was just worried that it would cause a circular reference.
I will make a new commit tomorrow.
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.
Dont worry, I just think a new MongooseRecursionError or something like that would be nicer.
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.
Agree that this should at least be a MongooseError. Not sure it is worth it to create a new recursion error.
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.
Couple of suggestions. Also, can you please add a test case for this?
lib/helpers/common.js
Outdated
|
||
recursion.count++; | ||
if(recursion.count >= 1024){ | ||
throw new Error(`Mongoose: bad update value, ${recursion.raw.update}, ${recursion.raw.path}`); |
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.
Agree that this should at least be a MongooseError. Not sure it is worth it to create a new recursion error.
lib/helpers/common.js
Outdated
} | ||
|
||
recursion.count++; | ||
if(recursion.count >= 1024){ |
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.
I think this is a bit brittle. Admittedly we're unlikely to have depth >= 1024 unless there's an infinite recursion. But I think a better approach would be to use a WeakSet
or WeakMap
to track what objects we've seen in the recursion, similar to how getIndexes()
does it.
@vkarpov15 |
lib/helpers/common.js
Outdated
throw new MongooseError(`a circular reference in the update value, updateValue: | ||
${util.inspect(recursion.raw.update, { showHidden: false, depth: 1 })} | ||
updatePath: ${recursion.raw.path} | ||
-------`); |
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.
Minor nitpick: this ----
is a bit messy. But I'll clean that up after merging, no need for that to be a blocker.
Summary
helpers.common.modifiedPaths
is a recursion function.when user call
db.updateOne({key: badValue})
, thebadValue
will cause a endless recursion call. but it is impossible to check user's input. so I think we can check the recursion level , it will be helpful for debugging.Examples