Skip to content

Commit 807634b

Browse files
divinity76jaapmarcus
authored andcommitted
urandom nitpick (hestiacp#2774)
original code was written with PHP5 compatibility in mind, and strictly speaking, did not account for the possibility of file_get_contents failing. php's built-in random_bytes() will throw an exception if it fails to get random bytes, the old code should've done the same but didn't, the new code does. also found some bugged autoloader file_exist check code, where depending on getcwd() or chmod it could pass the file_exist() check and still fail the require()
1 parent 7740498 commit 807634b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

web/inc/main.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
use PHPMailer\PHPMailer\SMTP;
88
use PHPMailer\PHPMailer\Exception;
99

10-
if (!file_exists(dirname(__FILE__).'/vendor/autoload.php')) {
11-
trigger_error('Unable able to load required libaries. Please run v-add-sys-phpmailer in command line');
12-
echo 'Unable able to load required libaries. Please run v-add-sys-phpmailer in command line';
10+
11+
try {
12+
require_once 'vendor/autoload.php';
13+
} catch (Throwable $ex) {
14+
$errstr = 'Unable able to load required libaries. Please run v-add-sys-phpmailer in command line. Error: ' . $ex->getMessage();
15+
trigger_error($errstr);
16+
echo $errstr;
1317
exit(1);
1418
}
1519

16-
require 'vendor/autoload.php';
17-
1820
define('HESTIA_DIR_BIN', '/usr/local/hestia/bin/');
1921
define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
2022
define('DEFAULT_PHP_VERSION', 'php-' . exec('php -r "echo substr(phpversion(),0,3);"'));
@@ -92,7 +94,7 @@ function destroy_sessions()
9294
// Generate CSRF Token
9395
if (isset($_SESSION['user'])) {
9496
if (!isset($_SESSION['token'])) {
95-
$token = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16));
97+
$token = bin2hex(random_bytes(16));
9698
$_SESSION['token'] = $token;
9799
}
98100
}

web/login/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function authenticate_user($user, $password, $twofa = '')
323323
}
324324

325325
// Generate CSRF token
326-
$token = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16));
326+
$token = bin2hex(random_bytes(16));
327327
$_SESSION['token'] = $token;
328328

329329
require_once('../templates/header.html');

0 commit comments

Comments
 (0)