From 82a332ebcf46ea80e8fa63e8ebe048ee3b40e48b Mon Sep 17 00:00:00 2001 From: sheepish Date: Thu, 22 Oct 2026 21:31:24 +0000 Subject: [PATCH] replace openssl_random_pseudo_bytes with native php random functions --- app/Util/Crypto.php | 2 +- lib/util.php | 12 ++++-------- sections/tools/data/site_info.php | 2 -- templates/admin/site-info.twig | 10 ++-------- tests/phpunit/RequestTest.php | 2 +- tests/phpunit/SearchEmailTest.php | 2 +- tests/phpunit/TGroupTest.php | 14 +++++++------- tests/phpunit/TagTest.php | 10 +++++----- 8 files changed, 21 insertions(+), 33 deletions(-) diff --git a/app/Util/Crypto.php b/app/Util/Crypto.php index fe7b6b4c6..26abb2cd9 100644 --- a/app/Util/Crypto.php +++ b/app/Util/Crypto.php @@ -12,7 +12,7 @@ class Crypto { throw new \Exception("internal openssl iv error"); } $key = static::hash($key); - $iv = openssl_random_pseudo_bytes($iv_size); + $iv = random_bytes($iv_size); // @phpstan-ignore argument.type (iv is >0) $cipher = openssl_encrypt( $plaintext, static::ENC_ALGO, $key, OPENSSL_RAW_DATA, $iv, $tag, $aad, static::GCM_TAG_SIZE); diff --git a/lib/util.php b/lib/util.php index 7bbb9e3bc..0504a4950 100644 --- a/lib/util.php +++ b/lib/util.php @@ -683,16 +683,12 @@ function make_utf8(?string $str): string { * @param int $len * @return string random alphanumeric string */ -function randomString($len = 32) { +function randomString(int $len = 32): string { $alphabet = str_split('abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ0123456789'); - $max = count($alphabet); - $mask = 2 ** (int)ceil(log($len, 2)) - 1; + $max = count($alphabet) - 1; $out = ''; - while (strlen($out) < $len) { - $n = ord(openssl_random_pseudo_bytes(1)) & $mask; - if ($n < $max) { - $out .= $alphabet[$n]; - } + for ($i = 0; $i < $len; $i++) { + $out .= $alphabet[random_int(0, $max)]; } return $out; } diff --git a/sections/tools/data/site_info.php b/sections/tools/data/site_info.php index c8f867503..b77969b65 100644 --- a/sections/tools/data/site_info.php +++ b/sections/tools/data/site_info.php @@ -40,7 +40,6 @@ if (isset($_GET['mode']) && $_GET['mode'] === 'userrank') { 'table' => $rankTable, ]); } else { - $random = openssl_random_pseudo_bytes(8, $strong); $db = new DB(); $pg = new DB\Pg(PG_RO_DSN); echo $Twig->render('admin/site-info.twig', [ @@ -48,7 +47,6 @@ if (isset($_GET['mode']) && $_GET['mode'] === 'userrank') { 'gid' => gid(posix_getgid()), 'euid' => uid(posix_geteuid()), 'egid' => gid(posix_getegid()), - 'openssl_strong' => $strong, 'mysql_version' => $db->version(), 'index' => [ 'redundant' => $db->redundantIndexList(), diff --git a/templates/admin/site-info.twig b/templates/admin/site-info.twig index 41796c905..7c71f88ff 100644 --- a/templates/admin/site-info.twig +++ b/templates/admin/site-info.twig @@ -29,15 +29,9 @@ div#phpinfo hr {width: 934px; background-color: #ccc; border: 0; height: 1px;} Uptime: {{ site_info.uptime.uptime|raw }}
Idle time: {{ site_info.uptime.idletime|raw }}

User: {{ uid }}
- Group: {{ euid }}
+ Group: {{ gid }}
Effective User: {{ euid }}
- Effective Group: {{ euid }}
-
openssl_random_pseudo_bytes():  -{%- if openssl_strong -%} - Strong -{%- else -%} - Weak/broken -{%- endif -%} + Effective Group: {{ egid }}

Timestamps

diff --git a/tests/phpunit/RequestTest.php b/tests/phpunit/RequestTest.php index 518ee3b2a..9468ae6a6 100644 --- a/tests/phpunit/RequestTest.php +++ b/tests/phpunit/RequestTest.php @@ -97,7 +97,7 @@ class RequestTest extends TestCase { ); $this->assertCount(0, $this->request->tagNameList()); - $nameList = ['phpunit.' . randomString(6), 'phpunit.' . randomString(6)]; + $nameList = ['phpunit.' . strtolower(randomString(6)), 'phpunit.' . strtolower(randomString(6))]; sort($nameList); $tagMan = new Manager\Tag(); $tag = $tagMan->create($nameList[0], $admin); diff --git a/tests/phpunit/SearchEmailTest.php b/tests/phpunit/SearchEmailTest.php index 7dbc96090..3077d2ee7 100644 --- a/tests/phpunit/SearchEmailTest.php +++ b/tests/phpunit/SearchEmailTest.php @@ -33,7 +33,7 @@ class SearchEmailTest extends TestCase { $this->assertSame(3, $search->add($list), 'search-add-list'); $emailList = array_map(fn($u) => $u->email(), $this->userList); - sort($emailList); + sort($emailList, SORT_STRING | SORT_FLAG_CASE); $this->assertSame($emailList, $search->emailList(), 'search-email-list'); $search->setColumn(1); // Username diff --git a/tests/phpunit/TGroupTest.php b/tests/phpunit/TGroupTest.php index f1eee3f95..994461456 100644 --- a/tests/phpunit/TGroupTest.php +++ b/tests/phpunit/TGroupTest.php @@ -73,8 +73,9 @@ class TGroupTest extends TestCase { /* Random artist names may not be in alphabetical order, but * will be rendered in order, so arrange as appropriate. */ - protected function namePair(string $one, string $two): array { - return $two < $one ? [$two, $one] : [$one, $two]; + protected function namePair(string ...$names): array { + sort($names, SORT_STRING | SORT_FLAG_CASE); + return $names; } public function testTGroupCreate(): void { @@ -591,7 +592,7 @@ class TGroupTest extends TestCase { public function testTGroupTag(): void { $user = $this->userList['admin']; - $name = 'phpunit.' . randomString(6); + $name = 'phpunit.' . strtolower(randomString(6)); $this->assertSame( 'tags_', @@ -611,7 +612,7 @@ class TGroupTest extends TestCase { $this->assertSame( 'tags_' . ucfirst(str_replace('.', '_', $name)), $this->tgroup->primaryTagCss(), - 'tgroup-create-noprimary-tag-css' + 'tgroup-create-primary-tag-css' ); $tag2 = $tagMan->create('phpunit.' . randomString(6), $user); @@ -937,12 +938,11 @@ class TGroupTest extends TestCase { $this->tgroup->artistRole()->text(), 'tgroup-dj-3-text', ); - $sortedArtistList = [ + $sortedArtistList = $this->namePair( $artistName[0], $artistName[1], $artistName[2], - ]; - sort($sortedArtistList); + ); $this->assertSame( '