fix(cast): reject non-numeric object in castDouble instead of storing Double(NaN)#16352
Open
spokodev wants to merge 1 commit into
Open
fix(cast): reject non-numeric object in castDouble instead of storing Double(NaN)#16352spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
… NaN castDouble coerced a non-numeric object or array with Number() and returned Double(NaN) without error, so casting and validation of a Double path silently passed. Number, Int32, and BigInt casters reject the same inputs. Add a NaN guard using the module's assert idiom, which SchemaDouble.cast translates to a CastError, while preserving explicit NaN inputs (plain NaN and valueOf() returning NaN) per the JSDoc.
vkarpov15
reviewed
Jul 5, 2026
vkarpov15
left a comment
Collaborator
There was a problem hiding this comment.
We can consider this, but it would have to be a breaking change because the documentation for lib/cast/double explicitly indicates that NaN is considered valid. However, given that Number explicitly disallows NaN, I'm inclined to think that we could block NaN for doubles for consistency's sake.
| } else { | ||
| coercedVal = Number(tempVal); | ||
| // ex: { a: 'im an object', valueOf: () => ({}) } // throw an error | ||
| assert.ok(!Number.isNaN(coercedVal) || Number.isNaN(tempVal)); |
Collaborator
There was a problem hiding this comment.
Why the || Number.isNaN(tempVal) here? tempVal isn't passed to BSON.Double(), so this condition doesn't seem relevant.
vkarpov15
reviewed
Jul 5, 2026
| assert.ok(!Number.isNaN(coercedVal) || Number.isNaN(tempVal)); | ||
| } | ||
| } else { | ||
| coercedVal = Number(val); |
Collaborator
There was a problem hiding this comment.
If we're going to block NaN, we should also block it here.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
castDoublesilently storesDouble(NaN)when given a non-numeric object or array instead of throwing, so casting and validation of aDoublepath pass with no error. TheNumber,Int32, andBigIntcasters all reject the same inputs, soDoubleis the outlier.Steps to reproduce
lib/cast/double.json the object branch:At the schema level, a non-numeric object reaches the stored document and passes validation:
The equivalent
Numberpath rejects the same input:So
new Model(req.body)with a body like{ price: { any: 1 } }persistsNaNon a scalar path with no cast error and no validation error.Root cause
The
typeof val === 'object'branch computescoercedVal = Number(tempVal)and returnsnew BSON.Double(coercedVal)with no check on the result. For a non-numeric object or array,Number(tempVal)isNaN, which is returned as a validDouble. The branch's own inline comment (// throw an error) shows the throw was intended but never added.Fix
Add a NaN guard in the object branch using the module's existing
assertidiom (SchemaDouble.casttranslates a thrown assertion into aCastError), while preserving explicit-NaN inputs that the JSDoc documents as valid:Plain
NaNand an object whosevalueOf()returnsNaNstill cast toDouble(NaN); only inputs that becomeNaNthrough non-numeric coercion are rejected.Authority
The
castDoubleJSDoc states it should "cast it to a IEEE 754-2008 floating point, or throw anErrorif the value cannot be casted" and listsnull,undefined, andNaNas the valid inputs.lib/cast/number.js,lib/cast/int32.js, andlib/cast/bigint.jsreject the same non-numeric objects.Tests
Two cases added under the existing
cast errorsblock intest/double.test.js: a plain non-numeric object and an object whosevalueOf()returns a non-numeric value, both asserting aCastError. Both fail onmaster(storedDouble(NaN), no validation error) and pass with the fix.test/double.test.js: 28 passingtest/cast.test.js+test/int32.test.js: 53 passing