Skip to content

Commit 7102278

Browse files
james-johnston-thumbtackbarryvdh
authored andcommitted
RequestIdGeneratorInterface now returns more random values (php-debugbar#336)
Implementers of RequestIdGeneratorInterface::generate() need to be sure that each call to generate() will return a unique ID. This could fail to happen in the existing implementation if microtime() returns the same value on two successive generate() calls that come within the same microsecond. For example, in a tight ID-generating loop. It was unlikely, but now it is practically impossible. Additionally, implementers should make sure that the return value is not all-numeric to avoid any mixed data types when IDs are used as keys in PHP arrays. (For example, numeric IDs could result in bugs when array_merge is used.)
1 parent ba8bf73 commit 7102278

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/DebugBar/RequestIdGenerator.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,33 @@
1111
namespace DebugBar;
1212

1313
/**
14-
* Request id generator based on the $_SERVER array
14+
* Basic request ID generator
1515
*/
1616
class RequestIdGenerator implements RequestIdGeneratorInterface
1717
{
18+
protected $index = 0;
19+
1820
/**
1921
* @return string
2022
*/
2123
public function generate()
2224
{
23-
return md5(serialize($_SERVER) . microtime());
25+
if (function_exists('random_bytes')) {
26+
// PHP 7 only
27+
return 'X' . bin2hex(random_bytes(16));
28+
} else if (function_exists('openssl_random_pseudo_bytes')) {
29+
// PHP >= 5.3.0, but OpenSSL may not always be available
30+
return 'X' . bin2hex(openssl_random_pseudo_bytes(16));
31+
} else {
32+
// Fall back to a rudimentary ID generator:
33+
// * $_SERVER array will make the ID unique to this request.
34+
// * spl_object_hash($this) will make the ID unique to this object instance.
35+
// (note that object hashes can be reused, but the other data here should prevent issues here).
36+
// * uniqid('', true) will use the current microtime(), plus additional random data.
37+
// * $this->index guarantees the uniqueness of IDs from the current object.
38+
$this->index++;
39+
$entropy = serialize($_SERVER) . uniqid('', true) . spl_object_hash($this) . $this->index;
40+
return 'X' . md5($entropy);
41+
}
2442
}
2543
}

src/DebugBar/RequestIdGeneratorInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
interface RequestIdGeneratorInterface
1414
{
1515
/**
16-
* Generates a unique id for the current request
16+
* Generates a unique id for the current request. If called repeatedly, a new unique id must
17+
* always be returned on each call to generate() - even across different object instances.
18+
*
19+
* To avoid any potential confusion in ID --> value maps, the returned value must be
20+
* guaranteed to not be all-numeric.
1721
*
1822
* @return string
1923
*/

0 commit comments

Comments
 (0)