globalizejs / globalizejs/globalize

Globalize-runtime has unsafe runtime-key hash function

Open
#705 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
4.8k
Forks
585
PR merge metrics
No merged PRs in 30d

Description

The runtime key is generated from a `JSON.stringify` of the arguments (see [here](https://github.com/globalizejs/globalize/blob/c6cd869fd5a6518eab0595503be85b6b29a7b43f/src/common/runtime-key.js#L8).

Problem is that `JSON.stringify` does not guarantee an order that the properties are stringified. See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

This already bit me once where formatters were called with the exact same properties but were stringified differently. For example:

```js
// Compile time
compile(Globalize.numberFormatter({
minimumSignificantDigits: 1,
maximumSignificantDigits: 3
}))

// Runtime
Globalize.numberFormatter({
maximumSignificantDigits: 3,
minimumSignificantDigits: 1
});

```

By making sure the properties are specified in the same order, it *appears* to work in the current browsers, but this is a bandaid because `JSON.stringify` is defined as unstable.

The solution is to use a stable hash of the arguments. Here's a fairly small, stable stringify: https://github.com/substack/json-stable-stringify. Or you can non-stringify hash.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.