Skip to content

Commit 2f57086

Browse files
author
Chris Raynor
committed
Merge pull request firebase#6 from 4026/master
Preventing large ints being converted to floats when decoding.
2 parents 5c2eb12 + f4013f8 commit 2f57086

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
@@ -130,7 +130,20 @@ public static function sign($msg, $key, $method = 'HS256')
130130
*/
131131
public static function jsonDecode($input)
132132
{
133-
$obj = json_decode($input);
133+
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
134+
/* In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you to specify that large ints (like Steam
135+
* Transaction IDs) should be treated as strings, rather than the PHP default behaviour of converting them to floats.
136+
*/
137+
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
138+
} else {
139+
/* Not all servers will support that, however, so for older versions we must manually detect large ints in the JSON
140+
* string and quote them (thus converting them to strings) before decoding, hence the preg_replace() call.
141+
*/
142+
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
143+
$json_without_bigints = preg_replace('/:\s*(\d{'.$max_int_length.',})/', ': "$1"', $input);
144+
$obj = json_decode($json_without_bigints);
145+
}
146+
134147
if (function_exists('json_last_error') && $errno = json_last_error()) {
135148
JWT::_handleJsonError($errno);
136149
} else if ($obj === null && $input !== 'null') {

0 commit comments

Comments
 (0)