Пример #1
0
    def test_bulk_create_with_conflict_in_every_vuln(self, test_client, session, csrf_token):
        vuln_template_1 = VulnerabilityTemplate(name='conflict_vuln_1', severity='high')
        session.add(vuln_template_1)
        vuln_template_2 = VulnerabilityTemplate(name='conflict_vuln_2', severity='high')
        session.add(vuln_template_2)
        session.commit()

        vuln_1 = VulnerabilityFactory.build_dict()
        vuln_1['name'] = 'conflict_vuln_1'
        vuln_1['exploitation'] = vuln_1['severity']
        vuln_2 = VulnerabilityFactory.build_dict()
        vuln_2['name'] = 'conflict_vuln_2'
        vuln_2['exploitation'] = vuln_2['severity']

        data = {
            'csrf_token': csrf_token,
            'vulns': [vuln_1, vuln_2]
        }

        res = test_client.post('/v3/vulnerability_template/bulk_create', json=data)
        assert res.status_code == 409

        assert len(res.json['vulns_with_conflict']) == 2
        assert res.json['vulns_with_conflict'][0][1] == vuln_1['name']
        assert res.json['vulns_with_conflict'][1][1] == vuln_2['name']

        assert len(res.json['vulns_created']) == 0
Пример #2
0
    def test_bulk_delete_vulnerabilities_template(self, test_client, session):
        previous_count = session.query(VulnerabilityTemplate).count()
        vuln_template_1 = VulnerabilityTemplate(name='vuln_1', severity='high')
        session.add(vuln_template_1)
        vuln_template_2 = VulnerabilityTemplate(name='vuln_2', severity='high')
        session.add(vuln_template_2)
        vuln_template_3 = VulnerabilityTemplate(name='vuln_3', severity='high')
        session.add(vuln_template_3)
        session.commit()

        data = {'ids': [vuln_template_1.id, vuln_template_2.id, vuln_template_3.id]}
        res = test_client.delete(self.url(), data=data)
        assert res.status_code == 200
        assert res.json['deleted'] == 3
        assert previous_count == session.query(VulnerabilityTemplate).count()
    def test_bulk_create_with_one_conflict(self, test_client, session,
                                           csrf_token):
        vuln_template = VulnerabilityTemplate(name='conflict_vuln',
                                              severity='high')
        session.add(vuln_template)
        session.commit()

        vuln_1 = VulnerabilityFactory.build_dict()
        vuln_1['name'] = 'conflict_vuln'
        vuln_1['exploitation'] = vuln_1['severity']
        vuln_2 = VulnerabilityFactory.build_dict()
        vuln_2['exploitation'] = vuln_2['severity']

        data = {'csrf_token': csrf_token, 'vulns': [vuln_1, vuln_2]}

        res = test_client.post(
            self.check_url('/v2/vulnerability_template/bulk_create/'),
            json=data)
        assert res.status_code == 200

        assert len(res.json['vulns_with_conflict']) == 1
        assert res.json['vulns_with_conflict'][0][1] == vuln_1['name']

        assert len(res.json['vulns_created']) == 1
        assert res.json['vulns_created'][0][1] == vuln_2['name']
def import_vulnerability_templates(language):
    imported_rows = 0
    duplicated_rows = 0
    with get_app().app_context():
        try:
            res = requests.get(f'{CWE_URL}/cwe_{language}.csv')
        except Exception as e:
            print(
                f'[{Fore.RED}-{Style.RESET_ALL}] An error has occurred downloading the file.\n{e}'
            )
            return None

        if res.status_code != 200:
            print(
                f'[{Fore.RED}-{Style.RESET_ALL}] An error has occurred downloading the file.'
                f' Response was {res.status_code}')
            return None

        cwe_file = tempfile.TemporaryFile(mode="w+t")
        cwe_file.write(res.content.decode('utf8'))
        cwe_file.seek(0)

        vulnerability_templates = csv.DictReader(cwe_file)
        for vulnerability_template in vulnerability_templates:
            vulnerability_template = dict(vulnerability_template)

            references = [
                ref.strip()
                for ref in vulnerability_template['references'].split(',')
            ]
            try:
                v = VulnerabilityTemplate(
                    name=vulnerability_template['name'],
                    description=vulnerability_template['description'],
                    severity=vulnerability_template['exploitation'],
                    resolution=vulnerability_template['resolution'],
                    references=references,
                    shipped=True)
                db.session.add(v)
                db.session.flush()
                imported_rows += 1
            except IntegrityError:
                duplicated_rows += 1
                db.session.rollback()
        db.session.commit()

        if imported_rows > 0:
            print(
                f'[{Fore.GREEN}+{Style.RESET_ALL}] {imported_rows} new vulnerability templates were imported'
            )
        else:
            print(
                f'[{Fore.YELLOW}+{Style.RESET_ALL}] {duplicated_rows} vulnerability templates were already imported'
            )