feat: handle large number #111
Open
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.
Description
This PR addresses an issue in the
node-jwslibrary where decoding JSON Web Tokens (JWTs) containing large integers would result in a loss of precision. This issue occurs because JavaScript’sNumbertype cannot safely represent integers beyondNumber.MAX_SAFE_INTEGER, causing the loss of accuracy.Background:
The
nodeservice depends on tokens issued by upstream services (such as Go/Java services). Many of these tokens contain IDs stored aslongintegers. Since JavaScript cannot accurately handle such large values (e.g., larger thanNumber.MAX_SAFE_INTEGER), precision is lost when decoding these tokens. This problem leads to inaccurate processing of IDs in downstream logic.Key Changes:
safe-parse.jsfile to separate concerns and improve maintainability.safeJsonParseWithBigInt, to specifically handle large numbers by converting them into strings during the parsing process, preventing any loss of precision.safeJsonParseWithBigInt.test.jsto validate the behavior of the new utility function and ensure that large numbers are correctly handled.This change ensures that large integers are decoded accurately as strings without loss of precision, which avoids breaking the existing behavior for non-large integers.
Reproduction Steps
To reproduce the issue and verify the fix, you can use the following payload:
userIdvalue.userIdwill be decoded correctly as a string, preserving its full value.References
Testing
To test this fix, reviewers can:
userIdvalue above, and ensure the number is correctly parsed as a string, without losing precision.safeJsonParseWithBigInt.test.jsto confirm that the functionality works as expected.Checklist