<commit_before>import hashlib
import logging
import re

from defusedxml import ElementTree

from dojo.models import Finding

logger = logging.getLogger(__name__)


class DependencyCheckParser(object):
    def get_field_value(self, parent_node, field_name):
        field_node = parent_node.find(self.namespace + field_name)
        field_value = '' if field_node is None else field_node.text
        return field_value

    def get_filename_from_dependency(self, dependency):
        return self.get_field_value(dependency, 'fileName')

    def get_finding_from_vulnerability(self, vulnerability, filename, test):
        name = self.get_field_value(vulnerability, 'name')
        cwe_field = self.get_field_value(vulnerability, 'cwe')
        description = self.get_field_value(vulnerability, 'description')

        title = '{0} | {1}'.format(filename, name)
        cve = name
        # Use CWE-1035 as fallback
        cwe = 1035  # Vulnerable Third Party Component
        if cwe_field:
            m = re.match(r"^(CWE-)?(\d+)", cwe_field)
            if m:
                cwe = int(m.group(2))
        cvssv2_node = vulnerability.find(self.namespace + 'cvssV2')
        try:
            if cvssv2_node is not None:
                severity = self.get_field_value(cvssv2_node, 'severity').lower().capitalize()
            else:
                severity = self.get_field_value(vulnerability, 'severity').lower().capitalize()
        except:
            severity = 'Info'

        reference_detail = None
        references_node = vulnerability.find(self.namespace + 'references')

        if references_node is not None:
            reference_detail = ''
            for reference_node in references_node.findall(self.namespace +
                                                          'reference'):
                name = self.get_field_value(reference_node, 'name')
                source = self.get_field_value(reference_node, 'source')
                url = self.get_field_value(reference_node, 'url')
                reference_detail += 'name: {0}\n' \
                                     'source: {1}\n' \
                                     'url: {2}\n\n'.format(name, source, url)

        return Finding(
            title=title,
            file_path=filename,
            test=test,
            cwe=cwe,
            cve=cve,
            active=False,
            verified=False,
            description=description,
            severity=severity,
            numerical_severity=Finding.get_numerical_severity(severity),<commit_msg>small severity fix<commit_after>import hashlib
import logging
import re

from defusedxml import ElementTree

from dojo.models import Finding

logger = logging.getLogger(__name__)

SEVERITY = ['Info', 'Low', 'Medium', 'High', 'Critical']

class DependencyCheckParser(object):
    def get_field_value(self, parent_node, field_name):
        field_node = parent_node.find(self.namespace + field_name)
        field_value = '' if field_node is None else field_node.text
        return field_value

    def get_filename_from_dependency(self, dependency):
        return self.get_field_value(dependency, 'fileName')

    def get_finding_from_vulnerability(self, vulnerability, filename, test):
        name = self.get_field_value(vulnerability, 'name')
        cwe_field = self.get_field_value(vulnerability, 'cwe')
        description = self.get_field_value(vulnerability, 'description')

        title = '{0} | {1}'.format(filename, name)
        cve = name
        # Use CWE-1035 as fallback
        cwe = 1035  # Vulnerable Third Party Component
        if cwe_field:
            m = re.match(r"^(CWE-)?(\d+)", cwe_field)
            if m:
                cwe = int(m.group(2))
        cvssv2_node = vulnerability.find(self.namespace + 'cvssV2')
        if cvssv2_node is not None:
            severity = self.get_field_value(cvssv2_node, 'severity').lower().capitalize()
        else:
            severity = self.get_field_value(vulnerability, 'severity').lower().capitalize()
        
        if severity in SEVERITY:
            severity = severity
        else:
            severity = "Info"
        
        reference_detail = None
        references_node = vulnerability.find(self.namespace + 'references')

        if references_node is not None:
            reference_detail = ''
            for reference_node in references_node.findall(self.namespace +
                                                          'reference'):
                name = self.get_field_value(reference_node, 'name')
                source = self.get_field_value(reference_node, 'source')
                url = self.get_field_value(reference_node, 'url')
                reference_detail += 'name: {0}\n' \
                                     'source: {1}\n' \
                                     'url: {2}\n\n'.format(name, source, url)

        return Finding(
            title=title,
            file_path=filename,
            test=test,
            cwe=cwe,
            cve=cve,
            active=False,
            verified=False,
            description=description,
            severity=severity,
            numerical_severity=Finding.get_numerical_severity(severity),
{'fraction_non_alphanumeric': 0.09507379720145677, 'fraction_numerical': 0.006900517538815411, 'mean_word_length': 2.060410557184751, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6799140', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1458, 'n_words': 300}