Skip to content

Commit

Permalink
Response: Format response objects and data automatically + remove dan…
Browse files Browse the repository at this point in the history
…gerous keys like password
  • Loading branch information
janbarasek committed Jan 6, 2020
1 parent 4f01c70 commit 4e30f6c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/Response/BaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
namespace Baraja\StructuredApi;


use Tracy\Debugger;
use Tracy\ILogger;

abstract class BaseResponse
{

public static $keysToHide = ['password', 'passwd', 'pass', 'pwd', 'creditcard', 'credit card', 'cc', 'pin'];

public static $hiddenKeyLabel = '*****';

/**
* @var mixed[]
*/
Expand Down Expand Up @@ -37,4 +44,40 @@ public function __toString(): string
return '';
}

/**
* @param mixed $key
* @param mixed $value
* @return bool
*/
protected function hideKey($key, $value): bool
{
static $hide;

if ($hide === null) {
$hide = [];
foreach (self::$keysToHide as $hideKey) {
$hide[$hideKey] = true;
}
}

if (isset($hide[$key]) === true) {
if (preg_match('/^\$2[ayb]\$.{56}$/', $value)) { // Allow BCrypt hash only.
return false;
}

if (\class_exists(Debugger::class) === true) {
Debugger::log(
new RuntimeStructuredApiException(
'Security warning: User password may have been compromised! Key "' . $key . '" given.'
. "\n" . 'The Baraja API prevented passwords being passed through the API in a readable form.'
), ILogger::CRITICAL
);
}

return true;
}

return false;
}

}
50 changes: 49 additions & 1 deletion src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,55 @@ public function getHaystack(): array
*/
public function getJson(): string
{
return Json::encode($this->haystack, Json::PRETTY);
return Json::encode($this->process($this->haystack), Json::PRETTY);
}

/**
* Convert common haystack to json compatible format.
*
* @param mixed $haystack
* @return array|string|mixed
*/
private function process($haystack)
{
if (\is_array($haystack) === true) {
$return = [];

foreach ($haystack as $key => $value) {
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}

return $return;
}

if (\is_object($haystack) === true) {
if (\method_exists($haystack, '__toString') === true) {
return (string) $haystack;
}

$return = [];

try {
foreach ((new \ReflectionClass($haystack))->getProperties() as $property) {
$property->setAccessible(true);

if (($key = $property->getName()) && ($key[0] ?? '') === '_') {
continue;
}

$value = $property->getValue($haystack);
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}
} catch (\ReflectionException $e) {
foreach ($haystack as $key => $value) {
$return[$key] = $this->hideKey($key, $value) ? self::$hiddenKeyLabel : $this->process($value);
}
}

return $return;
}

return $haystack;
}

}

0 comments on commit 4e30f6c

Please sign in to comment.