Make all exception classes extend Box_Exception

This makes all of them translatable, I also created a new option for Box_Exception to hide values from the stack trace for improved security.
This commit is contained in:
Belle Aerni committed 2023-01-26 15:28:03 -08:00
1 parent 226c4766d4
commit 5b232ce4dc
4 files changed
+49 -21

No files matched your search

+15 -8
View File
@@ -1,4 +1,5 @@
<?php <?php
/** /**
* FOSSBilling * FOSSBilling
* *
@@ -18,18 +19,20 @@ class Box_Exception extends Exception
* Creates a new translated exception. * Creates a new translated exception.
* *
* @param string error message * @param string error message
* @param array translation variables * @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, hide them from the stack trace.
*/ */
public function __construct($message, array $variables = NULL, $code = 0) public function __construct(string $message, array|null $variables = null, int $code = 0, bool $protected = false)
{ {
$config = include PATH_ROOT . '/config.php'; $config = include PATH_ROOT . '/config.php';
$debug = (isset($config['debug'])) ? $config['debug'] : false; $debug = $config['debug'] ?? false;
$logStack = (isset($config['log_stacktrace'])) ? $config['log_stacktrace'] : true; $logStack = $config['log_stacktrace'] ?? true;
$stackLength = (isset($config['stacktrace_length'])) ? $config['stacktrace_length'] : 25; $stackLength = $config['stacktrace_length'] ?? 25;
if ($debug && $logStack) { if ($debug && $logStack) {
error_log('An exception has been thrown. Stacktrace:'); error_log('An exception has been thrown. Stacktrace:');
error_log( $this->stackTrace($stackLength) ); error_log($this->stackTrace($stackLength, $protected));
} }
// Translate the exception // Translate the exception
@@ -39,12 +42,14 @@ class Box_Exception extends Exception
parent::__construct($message, $code); parent::__construct($message, $code);
} }
/** /**
* Big thank you to jhurliman and jambroseclarke on Stack Overflow for this backtrace formatter. * Big thank you to jhurliman and jambroseclarke on Stack Overflow for this backtrace formatter.
* We have slightly modified it for our purposes * We have slightly modified it for our purposes
* https://stackoverflow.com/a/32365961 * https://stackoverflow.com/a/32365961
*/ */
private function stackTrace($Length = 25) { private function stackTrace($Length = 25, $protected = false)
{
$stack = debug_backtrace($Length); $stack = debug_backtrace($Length);
$output = ''; $output = '';
@@ -57,7 +62,9 @@ class Box_Exception extends Exception
$argsLen = count($entry['args']); $argsLen = count($entry['args']);
for ($j = 0; $j < $argsLen; $j++) { for ($j = 0; $j < $argsLen; $j++) {
$my_entry = $entry['args'][$j]; $my_entry = $entry['args'][$j];
if (is_string($my_entry)) { if ($protected) {
$func .= "***";
} else if (is_string($my_entry)) {
$func .= $my_entry; $func .= $my_entry;
} }
if ($j < $argsLen - 1) $func .= ', '; if ($j < $argsLen - 1) $func .= ', ';
+7 -9
View File
@@ -12,20 +12,18 @@
* with this source code in the file LICENSE * with this source code in the file LICENSE
*/ */
class Payment_Exception extends Exception class Payment_Exception extends Box_Exception
{ {
/** /**
* Creates a new translated exception. * Creates a new translated exception, using the Box_Exception class.
* *
* @param string error message * @param string error message
* @param array translation variables * @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/ */
public function __construct($message, array $variables = NULL, $code = 0) public function __construct(string $message, array $variables = NULL, int $code = 0)
{ {
// Set the message parent:: __construct($message, $variables, $code, true);
$message = __trans($message, $variables);
// Pass the message to the parent
parent::__construct($message, $code);
} }
} }
+13 -2
View File
@@ -1,5 +1,16 @@
<?php <?php
class Registrar_Exception extends Exception class Registrar_Exception extends Box_Exception
{ {
/**
* Creates a new translated exception, using the Box_Exception class.
*
* @param string error message
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/
public function __construct(string $message, array $variables = NULL, int $code = 0)
{
parent::__construct($message, $variables, $code, true);
}
} }
+14 -2
View File
@@ -1,4 +1,5 @@
<?php <?php
/** /**
* FOSSBilling * FOSSBilling
* *
@@ -12,7 +13,18 @@
* with this source code in the file LICENSE * with this source code in the file LICENSE
*/ */
class Server_Exception extends Exception class Server_Exception extends Box_Exception
{ {
/**
* Creates a new translated exception, using the Box_Exception class.
*
* @param string error message
* @param array|null translation variables
* @param int The exception code.
* @param bool If the varibles in this should be considered protect, if so, disable stacktracing abilities.
*/
public function __construct(string $message, array $variables = NULL, int $code = 0)
{
parent::__construct($message, $variables, $code, true);
}
} }