Auto-sanitize only in the monitoring module

refs #2635
This commit is contained in:
Alexander A. Klimov 2018-05-14 10:19:32 +02:00
parent 1a94a21263
commit 02b60633ff
5 changed files with 44 additions and 6 deletions

View File

@ -17,11 +17,12 @@ class Json
* @param mixed $value
* @param int $options
* @param int $depth
* @param bool $autoSanitize Automatically sanitize invalid UTF-8 (if any)
*
* @return string
* @throws JsonEncodeException
*/
public static function encode($value, $options = 0, $depth = 512)
public static function encode($value, $options = 0, $depth = 512, $autoSanitize = false)
{
if (version_compare(phpversion(), '5.5.0', '<')) {
$encoded = json_encode($value, $options);
@ -33,8 +34,11 @@ class Json
case JSON_ERROR_NONE:
return $encoded;
/** @noinspection PhpMissingBreakStatementInspection */
case JSON_ERROR_UTF8:
return static::encode(static::sanitizeUtf8Recursive($value), $options, $depth);
if ($autoSanitize) {
return static::encode(static::sanitizeUtf8Recursive($value), $options, $depth);
}
default:
throw new JsonEncodeException('%s: %s', static::lastErrorMsg(), var_export($value, true));

View File

@ -45,6 +45,13 @@ class JsonResponse extends Response
*/
protected $encodingOptions = 0;
/**
* Whether to automatically sanitize invalid UTF-8 (if any)
*
* @var bool
*/
protected $autoSanitize = false;
/**
* Error message if the API call failed due to a server error
*
@ -96,6 +103,30 @@ class JsonResponse extends Response
return $this;
}
/**
* Get whether to automatically sanitize invalid UTF-8 (if any)
*
* @return bool
*/
public function getAutoSanitize()
{
return $this->autoSanitize;
}
/**
* Set whether to automatically sanitize invalid UTF-8 (if any)
*
* @param bool $autoSanitize
*
* @return $this
*/
public function setAutoSanitize($autoSanitize = true)
{
$this->autoSanitize = $autoSanitize;
return $this;
}
/**
* Get the error message if the API call failed due to a server error
*
@ -191,7 +222,7 @@ class JsonResponse extends Response
$body['data'] = $this->getSuccessData();
break;
}
echo Json::encode($body, $this->getEncodingOptions());
echo Json::encode($body, $this->getEncodingOptions(), 512, $this->autoSanitize);
}
/**

View File

@ -79,7 +79,7 @@ class ListCommand extends Command
$query = $query->getQuery();
switch ($format) {
case 'json':
echo Json::encode($query->fetchAll());
echo Json::encode($query->fetchAll(), 0, 512, true);
break;
case 'csv':
Csv::fromQuery($query)->dump();

View File

@ -60,7 +60,7 @@ class Controller extends IcingaWebController
'Content-Disposition',
'inline; filename=' . $this->getRequest()->getActionName() . '.json'
)
->appendBody(Json::encode($query->fetchAll()))
->appendBody(Json::encode($query->fetchAll()), 0, 512, true)
->sendResponse();
exit;
case 'csv':

View File

@ -153,7 +153,10 @@ abstract class MonitoredObjectController extends Controller
);
$groupName = $this->object->getType() . 'groups';
$payload[$groupName] = $this->object->$groupName;
$this->getResponse()->json()->setSuccessData($payload)->sendResponse();
$this->getResponse()->json()
->setSuccessData($payload)
->setAutoSanitize()
->sendResponse();
}
}