Skip to content

Commit

Permalink
Validate rules objects loaded from plugin rule modules (#1542)
Browse files Browse the repository at this point in the history
Validate rule objects loaded from plugin rule modules before those use
to avoid issues because of broken rule modules.

Signed-Off-By: Satoru SATOH <satoru.satoh@gmail.com>
  • Loading branch information
ssato committed May 3, 2021
1 parent 3b09b80 commit 99c7d53
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ansiblelint/rules/__init__.py
Expand Up @@ -176,10 +176,13 @@ def matchyaml(self, file: Lintable) -> List[MatchError]:
return matches


def load_plugins(directory: str) -> List[AnsibleLintRule]:
"""Return a list of rule classes."""
result = []
def is_valid_rule(rule: AnsibleLintRule) -> bool:
"""Check if given rule is valid or not."""
return isinstance(rule, AnsibleLintRule) and bool(rule.id) and bool(rule.shortdesc)


def load_plugins(directory: str) -> Iterator[AnsibleLintRule]:
"""Yield a rule class."""
for pluginfile in glob.glob(os.path.join(directory, '[A-Za-z]*.py')):

pluginname = os.path.basename(pluginfile.replace('.py', ''))
Expand All @@ -188,9 +191,13 @@ def load_plugins(directory: str) -> List[AnsibleLintRule]:
if spec and isinstance(spec.loader, Loader):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
obj = getattr(module, pluginname)()
result.append(obj)
return result
try:
rule = getattr(module, pluginname)()
if is_valid_rule(rule):
yield rule

except (TypeError, ValueError, AttributeError):
_logger.warning("Skipped invalid rule from %s", pluginname)


class RulesCollection:
Expand Down

0 comments on commit 99c7d53

Please sign in to comment.