Exemple #1
0
 def is_valid_rua_xmlfile(self, file):
     """ Perform some sanity checks on a RUA XML file:
         - is it free from XML threats?
         - does it look like an aggregate report?
         Returns True if valid, False if invalid """
     # To protect against various XML threats we use the parse function from defusedxml.ElementTree
     with open(file, 'r') as f:
         self.helper.log_debug(
             "is_valid_rua_xmlfile: start parsing xml file %s" % file)
         try:
             xmldata = defuse_parse(f)
         except Exception as e:
             self.helper.log_warning(
                 "is_valid_rua_xmlfile: XML parse error in file %s with exception %s"
                 % (file, e))
             f.close()
             return False
         # Does it look like an aggregate report?
         res = self.validate_xml_xsd(file, "rua_ta_dmarc_minimal_v01.xsd")
         if res["result"] != "pass":
             return False
         # Does it have the necessary elements?
         required_elements = [
             "report_metadata", "policy_published", "record"
         ]
         for required_element in required_elements:
             try:
                 element = xmldata.find(required_element)
             except Exception:
                 self.helper.log_warning(
                     "is_valid_rua_xmlfile: report did not contain a required XML element, %s"
                     % required_element)
                 return False
         return True
     return False
Exemple #2
0
 def process_xmlfile_to_lines(self, file):
     """ Processes an XML from from a given directory,
         and return a list of string lines in key=value format
     """
     lines = []
     with open(file, 'r') as f:
         self.helper.log_debug(
             "process_xmlfile_to_lines: start parsing xml file %s with do_resolve=%s"
             % (file, self.do_resolve))
         try:
             # To protect against various XML threats we use the parse function from defusedxml.ElementTree
             xmldata = defuse_parse(f)
         except Exception as e:
             self.helper.log_warning(
                 "process_xmlfile_to_lines: XML parse error in file %s with exception %s"
                 % (file, e))
         else:
             f.close()
             if self.do_validate_xml:
                 res = self.validate_xml(file)
                 lines = self.rua2kv(xmldata, res)
             else:
                 lines = self.rua2kv(xmldata)
             del xmldata
     return lines