Skip to content
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

Fix typo cause it will always trigger JSON11 parse #889

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed
### Fixed
- Upgrade JSON11 from 1.1.2 to 2.0.0 to ensure UTF-8 safety when stringifying JSON data
- Fixed typo cause JSON11 parse will always be execute when json string has number inside
### Security

## [3.0.0]
Expand Down
2 changes: 1 addition & 1 deletion lib/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class Serializer {
if (
numeralsAreNumbers &&
typeof val === 'number' &&
(val < Number.MAX_SAFE_INTEGER || val > Number.MAX_SAFE_INTEGER)
(val < Number.MIN_SAFE_INTEGER || val > Number.MAX_SAFE_INTEGER)
) {
numeralsAreNumbers = false;
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ test('Long numerals enabled', (t) => {
t.match(res, `"[ ${longNegative.toString()}, ${longPositive.toString()} ]"`);
});

test('Long numerals enabled and json not includes large numbers', (t) => {
t.plan(2);
const s = new Serializer({ enableLongNumeralSupport: true });
const longPositive = BigInt(Number.MAX_SAFE_INTEGER) * 2n; // eslint-disable-line no-undef
const longNegative = BigInt(Number.MIN_SAFE_INTEGER) * 2n; // eslint-disable-line no-undef
const json =
`{` +
// The space before and after the values, and the lack of spaces before comma are intentional
`"false-positive-1": "෴${longNegative.toString()}", ` +
`"false-positive-2": "[ ߷${longPositive.toString()} ]", ` +
`"false-positive-3": "\\": ֍${longPositive.toString()}\\"", ` +
`"false-positive-4": "෴߷֍${longPositive.toString()}", ` +
`"normal-number": 2024,` +
`"max-safe-integer": ${Number.MAX_SAFE_INTEGER},` +
`"min-safe-integer": ${Number.MIN_SAFE_INTEGER}` +
`}`;
const obj = s.deserialize(json);
const res = s.serialize(obj);
t.same(obj, {
'normal-number': 2024,
'max-safe-integer': `${Number.MAX_SAFE_INTEGER}`,
'min-safe-integer': `${Number.MIN_SAFE_INTEGER}`,
'false-positive-4': `෴߷֍${longPositive.toString()}`,
'false-positive-3': `": ֍${longPositive.toString()}"`,
'false-positive-2': `[ ߷${longPositive.toString()} ]`,
'false-positive-1': `෴${longNegative.toString()}`,
});
// The space before and after the values, and the lack of spaces before comma are intentional
t.equal(res.replace(/\s+/g, ''), json.replace(/\s+/g, ''));
});

test('long numerals not enabled', (t) => {
t.plan(5);
const s = new Serializer({ enableLongNumeralSupport: false });
Expand Down
Loading