From 6dfd429b259ce210091ee10b54d5690e55eecdd0 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Thu, 27 Aug 2020 17:45:25 -0400 Subject: [PATCH] Added bulletproofing to configuation parsing (#282) --- apprise/AppriseConfig.py | 8 +++++++- test/test_apprise_config.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/apprise/AppriseConfig.py b/apprise/AppriseConfig.py index 3525474b..fa5e6fba 100644 --- a/apprise/AppriseConfig.py +++ b/apprise/AppriseConfig.py @@ -27,6 +27,7 @@ import six from . import config from . import ConfigBase +from . import CONFIG_FORMATS from . import URLBase from .AppriseAsset import AppriseAsset @@ -252,7 +253,12 @@ class AppriseConfig(object): # Create ourselves a ConfigMemory Object to store our configuration instance = config.ConfigMemory( content=content, format=format, asset=asset, tag=tag, - recursion=self.recursion, insecure_includes=insecure_includes) + recursion=recursion, insecure_includes=insecure_includes) + + if instance.config_format not in CONFIG_FORMATS: + logger.warning( + "The format of the configuration could not be deteced.") + return False # Add our initialized plugin to our server listings self.configs.append(instance) diff --git a/test/test_apprise_config.py b/test/test_apprise_config.py index 284e37aa..bdffbf7e 100644 --- a/test/test_apprise_config.py +++ b/test/test_apprise_config.py @@ -29,6 +29,7 @@ import io import mock import pytest from apprise import NotifyFormat +from apprise import ConfigFormat from apprise import ConfigIncludeMode from apprise.Apprise import Apprise from apprise.AppriseConfig import AppriseConfig @@ -304,10 +305,11 @@ def test_apprise_add_config(): """ # Create ourselves a config object ac = AppriseConfig() - assert ac.add_config(content=content) + assert ac.add_config(content=content) is True # One configuration file should have been found assert len(ac) == 1 + assert ac[0].config_format is ConfigFormat.TEXT # Object can be directly checked as a boolean; response is True # when there is at least one entry @@ -337,6 +339,39 @@ def test_apprise_add_config(): # and 6 urls.. (as we've doubled up) assert len(ac.servers()) == 6 + content = """ + # A YAML File + urls: + - mailto://usera:pass@gmail.com + - gnome://: + tag: taga,tagb + """ + + # Create ourselves a config object + ac = AppriseConfig() + assert ac.add_config(content=content) is True + + # One configuration file should have been found + assert len(ac) == 1 + assert ac[0].config_format is ConfigFormat.YAML + + # Object can be directly checked as a boolean; response is True + # when there is at least one entry + assert ac + + # We should be able to read our 2 servers from that + assert len(ac.servers()) == 2 + + # Now an invalid configuration file + content = "invalid" + + # Create ourselves a config object + ac = AppriseConfig() + assert ac.add_config(content=content) is False + + # Nothing is loaded + assert len(ac.servers()) == 0 + def test_apprise_config_tagging(tmpdir): """