From 92bc72654d07c816d57116e97aa84e262f2607f1 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Tue, 23 Nov 2021 08:15:38 +0100 Subject: [PATCH 1/6] Allow more then just $config$ in CustomVariableString --- library/Director/CustomVariable/CustomVariableString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php index 2d509681c..dec14cb40 100644 --- a/library/Director/CustomVariable/CustomVariableString.php +++ b/library/Director/CustomVariable/CustomVariableString.php @@ -46,7 +46,7 @@ public function flatten(array &$flat, $prefix) public function toConfigString($renderExpressions = false) { if ($renderExpressions) { - return c::renderStringWithVariables($this->getValue(), ['config']); + return c::renderStringWithVariables($this->getValue()); } else { return c::renderString($this->getValue()); } From 96bab851252fb2b511c8c59ae729ab32a18cf536 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Mon, 6 Dec 2021 09:34:57 +0100 Subject: [PATCH 2/6] Add RegEx option to match whitelist including trailing parameters --- .../Director/IcingaConfig/IcingaConfigHelper.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 03c017ee1..9c5ab0f09 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -381,7 +381,7 @@ public static function isValidMacroName($name) && ! preg_match('/\.$/', $name); } - public static function renderStringWithVariables($string, array $whiteList = null) + public static function renderStringWithVariables($string, array $whiteList = null, bool $matchRegex = false) { $len = strlen($string); $start = false; @@ -400,7 +400,16 @@ public static function renderStringWithVariables($string, array $whiteList = nul // We got a macro $macroName = substr($string, $start + 1, $i - $start - 1); if (static::isValidMacroName($macroName)) { - if ($whiteList === null || in_array($macroName, $whiteList)) { + $whiteListMatch = false; + if ($whiteList !== null || $matchRegex) { + foreach ($array as $entry) { + $pattern = "/^(" . $entry . "|" . $entry . "\..*)$/i"; + $whiteListMatch = preg_match($pattern, $macroName); + } + } elseif ($whiteList !== null) { + $whiteListMatch = in_array($macroName, $whiteList); + } + if ($whiteList === null || $whiteListMatch) { if ($start > $offset) { $parts[] = static::renderString( substr($string, $offset, $start - $offset) From 9161c022afc86917799b9832ad2fe9230abaa4a4 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Mon, 6 Dec 2021 09:35:36 +0100 Subject: [PATCH 3/6] Add whitelist for config string and use RegEx to allow parameters --- library/Director/CustomVariable/CustomVariableString.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php index dec14cb40..ea1a0ca6c 100644 --- a/library/Director/CustomVariable/CustomVariableString.php +++ b/library/Director/CustomVariable/CustomVariableString.php @@ -46,7 +46,7 @@ public function flatten(array &$flat, $prefix) public function toConfigString($renderExpressions = false) { if ($renderExpressions) { - return c::renderStringWithVariables($this->getValue()); + return c::renderStringWithVariables($this->getValue(), ['config'], true); } else { return c::renderString($this->getValue()); } From b51404804f3f4707b75a5a1f1a1ea38d389f8404 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Mon, 6 Dec 2021 09:42:39 +0100 Subject: [PATCH 4/6] Wrong Variable used --- library/Director/IcingaConfig/IcingaConfigHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 9c5ab0f09..6c2b631dc 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -402,7 +402,7 @@ public static function renderStringWithVariables($string, array $whiteList = nul if (static::isValidMacroName($macroName)) { $whiteListMatch = false; if ($whiteList !== null || $matchRegex) { - foreach ($array as $entry) { + foreach ($whiteList as $entry) { $pattern = "/^(" . $entry . "|" . $entry . "\..*)$/i"; $whiteListMatch = preg_match($pattern, $macroName); } From 8c97160e84a2a313a6a6d791c76837cd21cae148 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Mon, 6 Dec 2021 14:50:58 +0100 Subject: [PATCH 5/6] add comments --- library/Director/IcingaConfig/IcingaConfigHelper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 6c2b631dc..105e24ff5 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -401,11 +401,13 @@ public static function renderStringWithVariables($string, array $whiteList = nul $macroName = substr($string, $start + 1, $i - $start - 1); if (static::isValidMacroName($macroName)) { $whiteListMatch = false; + // If matchRegex, match macro with parameter if ($whiteList !== null || $matchRegex) { foreach ($whiteList as $entry) { $pattern = "/^(" . $entry . "|" . $entry . "\..*)$/i"; $whiteListMatch = preg_match($pattern, $macroName); } + // Otherwise simply match against array entries } elseif ($whiteList !== null) { $whiteListMatch = in_array($macroName, $whiteList); } From a5d3407cc77349f64efc36df93350e9e2e0d3270 Mon Sep 17 00:00:00 2001 From: Alexander Votteler Date: Tue, 7 Dec 2021 09:59:08 +0100 Subject: [PATCH 6/6] Fix problem wit RegEx match for multiple whitelist entries --- library/Director/IcingaConfig/IcingaConfigHelper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/Director/IcingaConfig/IcingaConfigHelper.php b/library/Director/IcingaConfig/IcingaConfigHelper.php index 105e24ff5..26adb17a2 100644 --- a/library/Director/IcingaConfig/IcingaConfigHelper.php +++ b/library/Director/IcingaConfig/IcingaConfigHelper.php @@ -405,7 +405,9 @@ public static function renderStringWithVariables($string, array $whiteList = nul if ($whiteList !== null || $matchRegex) { foreach ($whiteList as $entry) { $pattern = "/^(" . $entry . "|" . $entry . "\..*)$/i"; - $whiteListMatch = preg_match($pattern, $macroName); + if (preg_match($pattern, $macroName)) { + $whiteListMatch = true; + } } // Otherwise simply match against array entries } elseif ($whiteList !== null) {