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

[11.x] Use secure randomness in Arr::random() and Arr::shuffle() #49642

Merged
merged 4 commits into from
Jan 11, 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
30 changes: 11 additions & 19 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ArrayAccess;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Random\Randomizer;

class Arr
{
Expand Down Expand Up @@ -661,24 +662,24 @@ public static function random($array, $number = null, $preserveKeys = false)
);
}

if (is_null($number)) {
return $array[array_rand($array)];
if (empty($array) || (! is_null($number) && $number <= 0)) {
return is_null($number) ? null : [];
}

if ((int) $number === 0) {
return [];
}
$keys = (new Randomizer)->pickArrayKeys($array, $requested);

$keys = array_rand($array, $number);
if (is_null($number)) {
return $array[$keys[0]];
}

$results = [];

if ($preserveKeys) {
foreach ((array) $keys as $key) {
foreach ($keys as $key) {
$results[$key] = $array[$key];
}
} else {
foreach ((array) $keys as $key) {
foreach ($keys as $key) {
$results[] = $array[$key];
}
}
Expand Down Expand Up @@ -730,20 +731,11 @@ public static function set(&$array, $key, $value)
* Shuffle the given array and return the result.
*
* @param array $array
* @param int|null $seed
* @return array
*/
public static function shuffle($array, $seed = null)
public static function shuffle($array)
{
if (is_null($seed)) {
shuffle($array);
} else {
mt_srand($seed);
shuffle($array);
mt_srand();
}

return $array;
return (new Randomizer)->shuffleArray($array);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1125,12 +1125,11 @@ public function shift($count = 1)
/**
* Shuffle the items in the collection.
*
* @param int|null $seed
* @return static
*/
public function shuffle($seed = null)
public function shuffle()
{
return new static(Arr::shuffle($this->items, $seed));
return new static(Arr::shuffle($this->items));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Collections/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,9 @@ public function search($value, $strict = false);
/**
* Shuffle the items in the collection.
*
* @param int|null $seed
* @return static
*/
public function shuffle($seed = null);
public function shuffle();

/**
* Create chunks representing a "sliding window" view of the items in the collection.
Expand Down
5 changes: 2 additions & 3 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1058,12 +1058,11 @@ public function search($value, $strict = false)
/**
* Shuffle the items in the collection.
*
* @param int|null $seed
* @return static
*/
public function shuffle($seed = null)
public function shuffle()
{
return $this->passthru('shuffle', func_get_args());
return $this->passthru('shuffle', []);
}

/**
Expand Down
24 changes: 20 additions & 4 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -907,14 +907,30 @@ public function testSet()
$this->assertEquals([1 => 'hAz'], Arr::set($array, 1, 'hAz'));
}

public function testShuffleWithSeed()
public function testShuffle()
{
$this->assertEquals(
Arr::shuffle(range(0, 100, 10), 1234),
Arr::shuffle(range(0, 100, 10), 1234)
$input = ['a', 'b', 'c', 'd', 'e', 'f'];

$this->assertNotEquals(
$input,
Arr::shuffle($input)
);
Comment on lines +914 to +917

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: This assertion is going to fail in 1 of 720 cases.


$this->assertNotEquals(
Arr::shuffle($input),
Arr::shuffle($input)
);
Comment on lines +919 to 922

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also going to fail in 1 of 720 cases. Combined this gives a failure rate of 1439/518400 for this testcase, which is roughly 1 in 360, which is too high of a false-positive rate for my taste.

}

public function testShuffleKeepsSameValues()
{
$input = ['a', 'b', 'c', 'd', 'e', 'f'];
$shuffled = Arr::shuffle($input);
sort($shuffled);

$this->assertEquals($input, $shuffled);
}

public function testEmptyShuffle()
{
$this->assertEquals([], Arr::shuffle([]));
Expand Down
11 changes: 0 additions & 11 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,6 @@ public function testCollectionIsConstructed($collection)
$this->assertEmpty($data->all());
}

#[DataProvider('collectionClassProvider')]
public function testCollectionShuffleWithSeed($collection)
{
$data = new $collection(range(0, 100, 10));

$firstRandom = $data->shuffle(1234);
$secondRandom = $data->shuffle(1234);

$this->assertEquals($firstRandom, $secondRandom);
}

#[DataProvider('collectionClassProvider')]
public function testSkipMethod($collection)
{
Expand Down