fix: handle objects with broken toJSON in error annotation#3106
Open
mahmoodhamdi wants to merge 1 commit into
Open
fix: handle objects with broken toJSON in error annotation#3106mahmoodhamdi wants to merge 1 commit into
mahmoodhamdi wants to merge 1 commit into
Conversation
When validating objects that contain values with a toJSON method that throws (e.g. cloned URL instances on Node.js 20-22), Joi.assert() crashes with a TypeError instead of reporting the validation error. The safeStringify function now catches serialization errors and falls back to a placeholder, allowing the validation error details to still be reported. Closes hapijs#3070
Collaborator
|
Is there a real-world scenario where this is happening? It doesn't seem like it should be joi's responsibility to properly serialize something that unexpectedly fails. |
Collaborator
|
Sorry, just saw the original issue, I feel like the result of this fix is not as helpful as it could be on the annotation, as the fallback text just removes everything to replace it with that string. |
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.
When validating objects containing values with a
toJSONmethod that throws (e.g. clonedURLinstances on Node.js 20–22),Joi.assert()crashes with aTypeErrorinstead of reporting the validation error.Root cause
internals.safeStringifyinannotate.jsusesJSON.stringifywith a custom replacer to handle circular references and special values. However,JSON.stringifycallstoJSON()on values before the replacer can intervene. When@hapi/hoek'sclone()produces a plain object that inherits atoJSONmethod requiring a specific receiver (likeURL.prototype.toJSON), the call throws becausethisis no longer the correct instance.Reproduction
Fix
Wrap
JSON.stringifyinsafeStringifywith a try-catch, falling back to a placeholder string. The validation error details are still reported in the error message.Changes
lib/annotate.js: Added try-catch insafeStringifyto handle brokentoJSONmethodstest/errors.js: Added test for objects withtoJSONthat throwsAll tests pass: 1797 tests, 100% coverage, lint clean, types clean.
Closes #3070