IdoQuery: Accept any character in custom var names

fixes #4342

(cherry picked from commit fe51f8c96811d03e39010325cb2af6c124d0f112)
This commit is contained in:
Johannes Meyer 2021-04-06 10:30:14 +02:00
parent 4c929c3dbb
commit 00ff11ea9f

View File

@ -17,6 +17,7 @@ use Icinga\Exception\ProgrammingError;
use Icinga\Exception\QueryException; use Icinga\Exception\QueryException;
use Icinga\Web\Session; use Icinga\Web\Session;
use Icinga\Module\Monitoring\Data\ColumnFilterIterator; use Icinga\Module\Monitoring\Data\ColumnFilterIterator;
use Zend_Db_Select;
/** /**
* Base class for Ido Queries * Base class for Ido Queries
@ -1189,7 +1190,13 @@ abstract class IdoQuery extends DbQuery
{ {
// TODO: This is not generic enough yet // TODO: This is not generic enough yet
list($type, $name) = $this->customvarNameToTypeName($customvar); list($type, $name) = $this->customvarNameToTypeName($customvar);
$alias = ($type === 'host' ? 'hcv_' : 'scv_') . $name; $alias = ($type === 'host' ? 'hcv_' : 'scv_') . preg_replace('~[^a-zA-Z0-9_]~', '_', $name);
// We're replacing any problematic char with an underscore, which will lead to duplicates, this avoids them
$from = $this->select->getPart(Zend_Db_Select::FROM);
for ($i = 2; array_key_exists($alias, $from); $i++) {
$alias = $alias . '_' . $i;
}
$this->customVars[strtolower($customvar)] = $alias; $this->customVars[strtolower($customvar)] = $alias;
@ -1228,7 +1235,7 @@ abstract class IdoQuery extends DbQuery
protected function customvarNameToTypeName($customvar) protected function customvarNameToTypeName($customvar)
{ {
$customvar = strtolower($customvar); $customvar = strtolower($customvar);
if (! preg_match('~^_(host|service)_([a-zA-Z0-9_]+)$~', $customvar, $m)) { if (! preg_match('~^_(host|service)_(.+)$~', $customvar, $m)) {
throw new ProgrammingError( throw new ProgrammingError(
'Got invalid custom var: "%s"', 'Got invalid custom var: "%s"',
$customvar $customvar