|
21 | 21 | from __future__ import annotations |
22 | 22 |
|
23 | 23 | import json |
| 24 | +import re |
24 | 25 | from requests.exceptions import HTTPError |
25 | 26 |
|
26 | 27 | import sonar.logging as log |
27 | 28 | from sonar.util import types |
28 | 29 | from sonar import sqobject, utilities |
29 | 30 | from sonar.permissions import template_permissions |
30 | 31 | import sonar.platform as pf |
| 32 | +from sonar.audit.rules import get_rule, RuleId |
31 | 33 | import sonar.audit.problem as pb |
32 | 34 |
|
33 | 35 | _OBJECTS = {} |
@@ -176,9 +178,23 @@ def to_json(self, export_settings: types.ConfigSettings = None) -> types.ObjectJ |
176 | 178 | json_data["lastUpdate"] = utilities.date_to_string(self.last_update) |
177 | 179 | return utilities.remove_nones(utilities.filter_export(json_data, _IMPORTABLE_PROPERTIES, export_settings.get("FULL_EXPORT", False))) |
178 | 180 |
|
| 181 | + def _audit_pattern(self, audit_settings: types.ConfigSettings) -> list[pb.Problem]: |
| 182 | + log.debug("Auditing %s projectKeyPattern ('%s')", str(self.project_key_pattern)) |
| 183 | + if not self.project_key_pattern or self.project_key_pattern == "": |
| 184 | + if not (self.is_applications_default() or self.is_portfolios_default() or self.is_projects_default()): |
| 185 | + return [pb.Problem(get_rule(RuleId.TEMPLATE_WITH_NO_PATTERN), self, str(self))] |
| 186 | + else: |
| 187 | + # Inspect regexp to detect suspicious pattern - Can't determine all bad cases but do our best |
| 188 | + # Currently detecting: |
| 189 | + # - Absence of '.' in the regexp |
| 190 | + # - '*' not preceded by '.' (confusion between wildcard and regexp) |
| 191 | + if not re.search(r"(^|[^\\])\.", self.project_key_pattern) or re.search(r"(^|[^.])\*", self.project_key_pattern): |
| 192 | + return [pb.Problem(get_rule(RuleId.TEMPLATE_WITH_SUSPICIOUS_PATTERN), self, str(self), self.project_key_pattern)] |
| 193 | + return [] |
| 194 | + |
179 | 195 | def audit(self, audit_settings: types.ConfigSettings) -> list[pb.Problem]: |
180 | 196 | log.debug("Auditing %s", str(self)) |
181 | | - return self.permissions().audit(audit_settings) |
| 197 | + return self._audit_pattern(audit_settings) + self.permissions().audit(audit_settings) |
182 | 198 |
|
183 | 199 |
|
184 | 200 | def get_object(endpoint: pf.Platform, name: str) -> PermissionTemplate: |
|
0 commit comments