|
7 | 7 | use Icinga\Authentication\Auth as IcingaAuth; |
8 | 8 | use Icinga\Exception\ConfigurationError; |
9 | 9 | use Icinga\Module\Icingadb\Authentication\ObjectAuthorization; |
| 10 | +use Icinga\Security\SecurityException; |
10 | 11 | use Icinga\Util\StringHelper; |
11 | 12 | use ipl\Orm\Compat\FilterProcessor; |
12 | 13 | use ipl\Orm\Model; |
@@ -109,6 +110,75 @@ public function isMatchedOn(string $queryString, Model $object): bool |
109 | 110 | return ObjectAuthorization::matchesOn($queryString, $object); |
110 | 111 | } |
111 | 112 |
|
| 113 | + /** |
| 114 | + * Assert that the given filter does not contain any column restrictions |
| 115 | + * |
| 116 | + * @param Filter\Rule $filter The filter to check |
| 117 | + * |
| 118 | + * @return void |
| 119 | + * |
| 120 | + * @throws SecurityException |
| 121 | + */ |
| 122 | + public function assertColumnRestrictions(Filter\Rule $filter): void |
| 123 | + { |
| 124 | + if ($this->getAuth()->getUser() === null || $this->getAuth()->getUser()->isUnrestricted()) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + $forbiddenVars = Filter::all(); |
| 129 | + $protectedVars = Filter::any(); |
| 130 | + $hiddenVars = Filter::any(); |
| 131 | + foreach ($this->getAuth()->getUser()->getRoles() as $role) { |
| 132 | + if (($restriction = $role->getRestrictions('icingadb/denylist/variables'))) { |
| 133 | + $denied = Filter::none(); |
| 134 | + $hiddenVars->add($denied); |
| 135 | + foreach (explode(',', $restriction) as $value) { |
| 136 | + $denied->add(Filter::like('name', trim($value))->ignoreCase()); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (($restriction = $role->getRestrictions('icingadb/protect/variables'))) { |
| 141 | + $protected = Filter::none(); |
| 142 | + $protectedVars->add($protected); |
| 143 | + foreach (explode(',', $restriction) as $value) { |
| 144 | + $protected->add(Filter::like('name', trim($value))->ignoreCase()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (! $hiddenVars->isEmpty()) { |
| 150 | + $forbiddenVars->add($hiddenVars); |
| 151 | + } |
| 152 | + |
| 153 | + if (! $protectedVars->isEmpty()) { |
| 154 | + $forbiddenVars->add($protectedVars); |
| 155 | + } |
| 156 | + |
| 157 | + if ($forbiddenVars->isEmpty()) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + $checkVars = static function (Filter\Rule $filter) use ($forbiddenVars, &$checkVars) { |
| 162 | + if ($filter instanceof Filter\Chain) { |
| 163 | + foreach ($filter as $rule) { |
| 164 | + $checkVars($rule); |
| 165 | + } |
| 166 | + } elseif ( |
| 167 | + ! $filter->metaData()->get('_isRestriction', false) |
| 168 | + && preg_match('/^(?:host|service)\.vars\.(.*)/i', $filter->getColumn(), $matches) |
| 169 | + ) { |
| 170 | + if (! Filter::match($forbiddenVars, ['name' => $matches[1]])) { |
| 171 | + throw new SecurityException( |
| 172 | + 'The variable "%s" is not allowed to be queried.', |
| 173 | + $matches[1] |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + $checkVars($filter); |
| 180 | + } |
| 181 | + |
112 | 182 | /** |
113 | 183 | * Apply Icinga DB Web's restrictions depending on what is queried |
114 | 184 | * |
@@ -333,9 +403,13 @@ function (Filter\Condition $condition) use ( |
333 | 403 | foreach ($allowedColumns as $column) { |
334 | 404 | if (is_callable($column)) { |
335 | 405 | if ($column($condition->getColumn())) { |
| 406 | + $condition->metaData()->set('_isRestriction', true); |
| 407 | + |
336 | 408 | return; |
337 | 409 | } |
338 | 410 | } elseif ($column === $condition->getColumn()) { |
| 411 | + $condition->metaData()->set('_isRestriction', true); |
| 412 | + |
339 | 413 | return; |
340 | 414 | } |
341 | 415 | } |
|
0 commit comments