Skip to content

Commit 565b2c6

Browse files
committed
Adding preprocessing of the JSON string when decoding to prevent large integers being converted to floats.
1 parent 53669d6 commit 565b2c6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Authentication/JWT.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,20 @@ public static function sign($msg, $key, $method = 'HS256')
126126
*/
127127
public static function jsonDecode($input)
128128
{
129-
$obj = json_decode($input);
129+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
130+
/* In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you to specify that large ints (like Steam
131+
* Transaction IDs) should be treated as strings, rather than the PHP default behaviour of converting them to floats.
132+
*/
133+
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
134+
} else {
135+
/* Not all servers will support that, however, so for older versions we must manually detect large ints in the JSON
136+
* string and quote them (thus converting them to strings) before decoding, hence the preg_replace() call.
137+
*/
138+
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
139+
$json_without_bigints = preg_replace('/:\s*(\d{'.$max_int_length.',})/', ': "$1"', $input);
140+
$obj = json_decode($json_without_bigints, true);
141+
}
142+
130143
if (function_exists('json_last_error') && $errno = json_last_error()) {
131144
JWT::_handleJsonError($errno);
132145
} else if ($obj === null && $input !== 'null') {

0 commit comments

Comments
 (0)