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
Changes from 1 commit
c56800a
7974f00
88d584d
62ad08d
23d44e4
f899973
b393f87
f60b59a
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 |
---|---|---|
|
@@ -67,7 +67,19 @@ function flatten(update, path, options, schema) { | |
* ignore | ||
*/ | ||
|
||
function modifiedPaths(update, path, result) { | ||
function modifiedPaths(update, path, result, recursion=null) { | ||
if(recursion == null){ | ||
recursion = { | ||
count: 0, | ||
raw: {update, path} | ||
}; | ||
} | ||
|
||
recursion.count++; | ||
if(recursion.count >= 1024){ | ||
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. 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 |
||
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 commentThe 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 commentThe 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 commentThe 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 commentThe 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. |
||
} | ||
|
||
const keys = Object.keys(update || {}); | ||
const numKeys = keys.length; | ||
result = result || {}; | ||
|
@@ -83,7 +95,7 @@ function modifiedPaths(update, path, result) { | |
val = val.toObject({ transform: false, virtuals: false }); | ||
} | ||
if (shouldFlatten(val)) { | ||
modifiedPaths(val, path + key, result); | ||
modifiedPaths(val, path + key, result, recursion); | ||
} | ||
} | ||
|
||
|
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?