Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

Count PHP shuffle and random picks as randomness (#1360)

Hashem Alkhanaizi183f99700d51
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d38d7036..6cf28ea0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
## Unreleased
+- PHP: `shuffle()`, `str_shuffle()`, `array_rand()`, `random_bytes()` and `lcg_value()` count as
+ randomness, as `rand()` does. Before, a function that picked or shuffled with them was bridged as
+ pure, and each recorded case was one draw no port could repeat (#1340 by @Alkhanizi-hashem).
- Picking at random from a collection counts as randomness: Swift `.shuffled()` and
`.randomElement()` (the entries never matched, since a name after `.` is skipped), Ruby `.shuffle`
and `.sample`, and Java and Kotlin `.shuffled()`, `.random()` and `Collections.shuffle(`. Before,
diff --git a/docs/how-it-works.md b/docs/how-it-works.md
index cd063465..b5b5de89 100644
--- a/docs/how-it-works.md
+++ b/docs/how-it-works.md
@@ -304,7 +304,7 @@ bad it usually is and gives one example. The severities are the ones `analyze` s
| Already compiled (numba, Cython, JAX) | medium | Python: decorated code that is not plain Python any more |
| numpy array parameters | high | Python: `np.ndarray` arguments and returns |
| Platform-specific C (asm, volatile, bitfields, packing) | high | C: `asm`, `volatile`, `:3` bitfields, `#pragma pack` |
-| Clock and random numbers | low | TS/JS `Date.now`/`Math.random`, Python `time.time`/`random`/`os.urandom` (also `from time import time`), Go `time.Now`/`rand`, Java `System.currentTimeMillis`, Java and Kotlin `.shuffled()`/`.random()`/`Collections.shuffle`, Ruby `Time.now`/`.shuffle`/`.sample`, PHP `date`/`time`, C# `DateTime.Now`, Swift `Date()`/`.shuffled()`/`.randomElement()`, PowerShell `Get-Date`/`Get-Random`/`New-Guid`: `record --pin-clock` covers TS/JS with the napi bridge and Python; elsewhere compare by hand |
+| Clock and random numbers | low | TS/JS `Date.now`/`Math.random`, Python `time.time`/`random`/`os.urandom` (also `from time import time`), Go `time.Now`/`rand`, Java `System.currentTimeMillis`, Java and Kotlin `.shuffled()`/`.random()`/`Collections.shuffle`, Ruby `Time.now`/`.shuffle`/`.sample`, PHP `date`/`time`/`shuffle`/`array_rand`/`random_bytes`, C# `DateTime.Now`, Swift `Date()`/`.shuffled()`/`.randomElement()`, PowerShell `Get-Date`/`Get-Random`/`New-Guid`: `record --pin-clock` covers TS/JS with the napi bridge and Python; elsewhere compare by hand |
| Implicit output and the pipeline | medium | PowerShell: `Write-Output`, `echo` and values left uncaptured go to the pipeline |
| Case-insensitive comparison | medium | PowerShell `-eq`, `-like`, `-match`, `-contains` and `switch` ignore case |
| Implicit type conversion | medium | PowerShell: casts such as `[int]"abc"`, and `/` on integers giving a double |
diff --git a/src/lang/effects.rs b/src/lang/effects.rs
index 3786046e..c623d7b0 100644
--- a/src/lang/effects.rs
+++ b/src/lang/effects.rs
@@ -242,6 +242,11 @@ fn clock_and_random(lang: Lang) -> &'static [&'static str] {
"microtime(",
"random_int(",
"uniqid(",
+ "shuffle(",
+ "str_shuffle(",
+ "array_rand(",
+ "random_bytes(",
+ "lcg_value(",
],
Lang::Csharp => &[
"DateTime.Now",
@@ -1508,6 +1513,36 @@ mod tests {
"<?php\nfunction f() { return date('Y'); }\n",
Risk::ClockAndRandom,
),
+ (
+ Lang::Php,
+ "deck.php",
+ "<?php\nfunction f(array $a) { return $a[array_rand($a)]; }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Php,
+ "deck.php",
+ "<?php\nfunction f(array $a) { shuffle($a); return $a; }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Php,
+ "deck.php",
+ "<?php\nfunction f() { return str_shuffle('abcdef'); }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Php,
+ "deck.php",
+ "<?php\nfunction f() { return bin2hex(random_bytes(8)); }\n",
+ Risk::ClockAndRandom,
+ ),
+ (
+ Lang::Php,
+ "deck.php",
+ "<?php\nfunction f() { return lcg_value(); }\n",
+ Risk::ClockAndRandom,
+ ),
(
Lang::Csharp,
"X.cs",