Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for masking sensitive debug data #4550

Merged
merged 10 commits into from
Apr 16, 2021
12 changes: 12 additions & 0 deletions app/Config/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ class Exceptions extends BaseConfig
* @var string
*/
public $errorViewPath = APPPATH . 'Views/errors';

/**
* --------------------------------------------------------------------------
* HIDE FROM DEBUG TRACE
* --------------------------------------------------------------------------
* Any data that you would like to hide from the debug trace.
* In order to specify 2 levels, use "/" to separate.
* ex. ['server', 'setup/password', 'secret_token']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 'server' in the example? What is level ?
What does this feature hide?
Does this feature really work?

*
* @var array
*/
public $sensitiveDataInTrace = [];
}
49 changes: 48 additions & 1 deletion system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,64 @@ protected function render(Throwable $exception, int $statusCode)
*/
protected function collectVars(Throwable $exception, int $statusCode): array
{
$trace = $exception->getTrace();
if (! empty($this->config->sensitiveDataInTrace))
{
$this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
}

return [
'title' => get_class($exception),
'type' => get_class($exception),
'code' => $statusCode,
'message' => $exception->getMessage() ?? '(null)',
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace(),
'trace' => $trace,
];
}

/**
* Mask sensitive data in the trace.
*
* @param array|object $trace
* @param array $keysToMask
* @param string $path
*/
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = '')
{
foreach ($keysToMask as $keyToMask)
{
$explode = explode('/', $keyToMask);
$index = end($explode);

if (strpos(strrev($path . '/' . $index), strrev($keyToMask)) === 0)
{
if (is_array($trace) && array_key_exists($index, $trace))
{
$trace[$index] = '******************';
}
elseif (is_object($trace) && property_exists($trace, $index) && isset($trace->$index))
{
$trace->$index = '******************';
}
}
}

if (! is_iterable($trace) && is_object($trace))
{
$trace = get_object_vars($trace);
}

if (is_iterable($trace))
{
foreach ($trace as $pathKey => $subarray)
{
$this->maskSensitiveData($subarray, $keysToMask, $path . '/' . $pathKey);
}
}
}

/**
* Determines the HTTP status code and the exit status code for this request.
*
Expand Down