django-DefectDojo icon indicating copy to clipboard operation
django-DefectDojo copied to clipboard

Kiuwan CSV Import fails

Open flmarkus opened this issue 3 years ago • 3 comments

Bug description When importing an Kiuwan CSV report via command line the import fails, because of several issues:

  • The parser expects a column named Vulnerability type, but it is called Software characteristic in the CSV (See parser)
  • The parser expects the column Line number to be an integer, but in kiuwan this column is optional

Steps to reproduce Steps to reproduce the behavior:

  1. Save the sample kiuwan export from below
  2. Create a project with a new engagement and get engagement id
  3. Upload via cli: curl --fail --location --request POST "https://my-defectdojo-instance.com/api/v2/import-scan/" --header "Authorization: Token <my-token>" --form "scan_date=\"2023-07-04\"" --form "minimum_severity=\"Info\"" --form "active=\"true\"" --form "verified=\"true\"" --form "scan_type=\"Kiuwan Scan\"" --form "engagement=\"<engagement-id>\"" --form "[email protected]" --form "tags=\"SAST\"" --form "test_title=\"KIUWAN SCAN\""
  4. The call fails with error code 500. More info is written to the log.

Expected behavior The import should succeed without issues.

Deployment method (select with an X)

  • [ ] Docker Compose
  • [ X] Kubernetes
  • [ ] GoDojo

Environment information

  • DefectDojo Version v. 2.23.1

Logs

[04/Jul/2023 12:18:06] ERROR [dojo.api_v2.exception_handler:32] 'Vulnerability type'
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/app/dojo/api_v2/views.py", line 2302, in perform_create
serializer.save(push_to_jira=push_to_jira)
File "/app/dojo/api_v2/serializers.py", line 1674, in save
test, finding_count, closed_finding_count, test_import = importer.import_scan(scan, scan_type, engagement, lead, environment,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dojo/importers/importer/importer.py", line 309, in import_scan
parsed_findings = parser.get_findings(scan, test)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dojo/tools/kiuwan/parser.py", line 58, in get_findings
findingdict['description'] = "**Vulnerability type** : " + row['Vulnerability type'] + "\n\n" + \
~~~^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'Vulnerability type'
[04/Jul/2023 10:14:11] ERROR [dojo.api_v2.exception_handler:32] Field 'line' expected a number but got ''.
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 2018, in get_prep_value
return int(value)
^^^^^^^^^^
ValueError: invalid literal for int() with base 10: ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/dojo/api_v2/serializers.py", line 1674, in save
test, finding_count, closed_finding_count, test_import = importer.import_scan(scan, scan_type, engagement, lead, environment,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dojo/importers/importer/importer.py", line 339, in import_scan
new_findings = self.process_parsed_findings(test, parsed_findings, scan_type, user, active=active,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dojo/decorators.py", line 48, in __wrapper__
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/celery/local.py", line 188, in __call__
return self._get_current_object()(*a, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/celery/app/task.py", line 392, in __call__
return self.run(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/dojo/importers/importer/importer.py", line 105, in process_parsed_findings
item.save(dedupe_option=False)
File "/app/dojo/models.py", line 2853, in save
super(Finding, self).save(*args, **kwargs)
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 812, in save
self.save_base(
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 863, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 1006, in _save_table
results = self._do_insert(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/base.py", line 1047, in _do_insert
return manager._insert(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1791, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1659, in execute_sql
for sql, params in self.as_sql():
^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1583, in as_sql
value_rows = [
^
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1584, in <listcomp>
[
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1585, in <listcomp>
self.prepare_value(field, self.pre_save_val(field, obj))
File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1523, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 925, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 920, in get_db_prep_value
value = self.get_prep_value(value)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 2020, in get_prep_value
raise e.__class__(
ValueError: Field 'line' expected a number but got ''.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/app/dojo/api_v2/views.py", line 2302, in perform_create
serializer.save(push_to_jira=push_to_jira)
File "/app/dojo/api_v2/serializers.py", line 1703, in save
raise Exception(ve)
Exception: Field 'line' expected a number but got ''.

Sample scan files A sample kiuwan CSV export that will cause this issue:

Rule code,Rule,Priority,Software characteristic,Language,Effort,File,Line number,Line text,Source file,Source line number,Source line text,Muted,Normative,Status,CWE Scope,Framework
OPT.PLSQL.GEN_PLSQL.VAR2,"Define variables as VARCHAR2, nor as VARCHAR",Very High,Efficiency,PL-SQL,03m,src/main/resources/lessons/challenges/db/migration/V2018_09_26_1__users.sql,3,"  userid varchar(250),",,,,No,,none,,
OPT.PLSQL.GEN_PLSQL.VAR2,"Define variables as VARCHAR2, nor as VARCHAR",Very High,Efficiency,PL-SQL,03m,src/main/resources/lessons/challenges/db/migration/V2018_09_26_1__users.sql,4,"  email varchar(30),",,,,No,,none,,
OPT.PLSQL.GEN_PLSQL.VAR2,"Define variables as VARCHAR2, nor as VARCHAR",Very High,Efficiency,PL-SQL,03m,src/main/resources/lessons/challenges/db/migration/V2018_09_26_1__users.sql,5,  password varchar(30),,,,No,,none,,
OPT.JAVA.PB.NumMaxClassByPackage,Avoid an excessive number of classes per package/namespace,Normal,Maintainability,Java,4h 00,src/it/java/org/owasp/webgoat/AccessControlIntegrationTest.java,,*Line of code not available (optional),,,,No,,none,,
OPT.JAVA.PB.NumMaxClassByPackage,Avoid an excessive number of classes per package/namespace,Normal,Maintainability,Java,4h 00,src/main/java/org/owasp/webgoat/container/lessons/Assignment.java,,*Line of code not available (optional),,,,No,,none,,
OPT.JAVA.PB.NumMaxClassByPackage,Avoid an excessive number of classes per package/namespace,Normal,Maintainability,Java,4h 00,src/main/java/org/owasp/webgoat/container/service/EnvironmentService.java,,*Line of code not available (optional),,,,No,,none,,

flmarkus avatar Jul 04 '23 13:07 flmarkus

For now I could do some tweaks to the CSV file to get it imported correctly. But this is a hacky workaround and not a permanent solution.

Just in case anybody is interested in this workaround as well: sed -i -e '1 s/,Software characteristic,/,Vulnerability type,/' report.csv sed -i -e 's/,,\*Line of code not available (optional),/,0,\*Line of code not available (optional),/' report_temp.csv

flmarkus avatar Jul 04 '23 13:07 flmarkus

Thanks for fixing the Kiuwan Parser @FelixHernandez! I can confirm, the fix works as expected.

Tested with DefectDojo SaaS Version 2.30.0

flmarkus avatar Jan 04 '24 12:01 flmarkus

I think the information I provided was a little bit misleading: There are several export types of kiuwan reports. Sorry for this. Now the description contains mixed information and for vulnerabilities the Vulnerability type is missing.

I created a new issue to fix this: #9307

flmarkus avatar Jan 10 '24 10:01 flmarkus