Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#9138 Do not use mt_ functions for secrets
  • Loading branch information
asmecher committed Jul 10, 2023
1 parent 5cad3a1 commit e5e7e54
Show file tree
Hide file tree
Showing 8 changed files with 7 additions and 11 deletions.
3 changes: 0 additions & 3 deletions classes/core/PKPApplication.inc.php
Expand Up @@ -122,9 +122,6 @@ abstract class PKPApplication implements iPKPApplicationInfoProvider {
* Constructor
*/
public function __construct() {
// Seed random number generator
mt_srand(intval(((double) microtime()) * 1000000));

import('lib.pkp.classes.core.Core');
import('lib.pkp.classes.core.PKPString');
import('lib.pkp.classes.core.Registry');
Expand Down
3 changes: 1 addition & 2 deletions classes/core/PKPString.inc.php
Expand Up @@ -494,8 +494,7 @@ static function enumerateAlphabetically($steps) {
* @return string
*/
static function generateUUID() {
mt_srand((double)microtime()*10000);
$charid = strtoupper(md5(uniqid(rand(), true)));
$charid = strtoupper(md5(uniqid(random_int(), true)));
$hyphen = '-';
$uuid = substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
Expand Down
Expand Up @@ -127,7 +127,7 @@ function getRSSTokenByUserId($userId, $contextId) {
* @return int
*/
function insertNewRSSToken($userId, $contextId) {
$token = uniqid(rand());
$token = uniqid(random_int());

// Recurse if this token already exists
if($this->getUserIdByRSSToken($token, $contextId)) return $this->insertNewRSSToken($userId, $contextId);
Expand Down
2 changes: 1 addition & 1 deletion classes/oai/PKPOAIDAO.inc.php
Expand Up @@ -61,7 +61,7 @@ function getToken($tokenId) {
function insertToken($token) {
do {
// Generate unique token ID
$token->id = md5(uniqid(mt_rand(), true));
$token->id = md5(uniqid(random_int(), true));
$result = $this->retrieve(
'SELECT COUNT(*) AS row_count FROM oai_resumption_tokens WHERE token = ?',
[$token->id]
Expand Down
2 changes: 1 addition & 1 deletion classes/plugins/PluginHelper.inc.php
Expand Up @@ -46,7 +46,7 @@ public function extractPlugin($filePath, $originalFileName) {
}

// Create random dirname to avoid symlink attacks.
$pluginExtractDir = dirname($filePath) . DIRECTORY_SEPARATOR . $pluginShortName . substr(md5(mt_rand()), 0, 10);
$pluginExtractDir = dirname($filePath) . DIRECTORY_SEPARATOR . $pluginShortName . substr(md5(random_int()), 0, 10);
if (!mkdir($pluginExtractDir)) throw new Exception('Could not create directory ' . $pluginExtractDir);

// Test whether the tar binary is available for the export to work
Expand Down
2 changes: 1 addition & 1 deletion classes/security/Validation.inc.php
Expand Up @@ -292,7 +292,7 @@ public static function generatePassword($length = null) {

$password = "";
for ($i=0; $i<$length; $i++) {
$password .= mt_rand(1, 4) == 4 ? $numbers[mt_rand(0,strlen($numbers)-1)] : $letters[mt_rand(0, strlen($letters)-1)];
$password .= random_int(1, 4) == 4 ? $numbers[random_int(0,strlen($numbers)-1)] : $letters[random_int(0, strlen($letters)-1)];
}
return $password;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/session/Session.inc.php
Expand Up @@ -216,7 +216,7 @@ function getCSRFToken() {
// Generate random data
if (function_exists('openssl_random_pseudo_bytes')) $data = openssl_random_pseudo_bytes(128);
elseif (function_exists('random_bytes')) $data = random_bytes(128);
else $data = sha1(mt_rand());
else $data = sha1(random_int());

// Hash the data
$token = null;
Expand Down
2 changes: 1 addition & 1 deletion tools/generateTestMetrics.php
Expand Up @@ -69,7 +69,7 @@ function execute() {
'assoc_id' => $submissionId,
'submission_id' => $submissionId,
'metric_type' => METRIC_TYPE_COUNTER,
'metric' => rand(1, 10),
'metric' => random_int(1, 10),
'day' => $currentDate->format('Ymd'),
]);
$count++;
Expand Down

0 comments on commit e5e7e54

Please sign in to comment.