Skip to content

Commit f03843c

Browse files
committed
Add validation for the LoadSBOMsForm #241
Signed-off-by: tdruez <[email protected]>
1 parent 4851d11 commit f03843c

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

product_portfolio/forms.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
# See https://aboutcode.org for more information about AboutCode FOSS projects.
77
#
88

9+
import json
10+
911
from django import forms
1012
from django.core.exceptions import ValidationError
1113
from django.db import transaction
@@ -38,10 +40,10 @@
3840
from dje.forms import DataspacedAdminForm
3941
from dje.forms import DataspacedModelForm
4042
from dje.forms import DefaultOnAdditionLabelMixin
41-
from dje.forms import StrictSubmit
4243
from dje.forms import Group
4344
from dje.forms import JSONListField
4445
from dje.forms import OwnerChoiceField
46+
from dje.forms import StrictSubmit
4547
from dje.forms import autocomplete_placeholder
4648
from dje.mass_update import DejacodeMassUpdateForm
4749
from dje.models import History
@@ -675,13 +677,37 @@ def submit(self, product, user):
675677
)
676678

677679

680+
def validate_sbom_file(value):
681+
"""Validator for SBOM JSON file."""
682+
filename = value.name.lower()
683+
if not filename.endswith(".json"):
684+
return
685+
686+
try:
687+
file_content = value.read().decode("utf-8")
688+
json_data = json.loads(file_content)
689+
except (json.JSONDecodeError, UnicodeDecodeError):
690+
raise ValidationError(_("Invalid JSON file. Please provide a properly formatted JSON."))
691+
finally:
692+
value.seek(0) # Reset file pointer after reading
693+
694+
if headers := json_data.get("headers", []):
695+
tool_name = headers[0].get("tool_name", "")
696+
if "scan" in tool_name.lower():
697+
raise ValidationError(
698+
'Your file appears to be a ScanCode scan results. '
699+
'You want to use the "Import ScanCode scan results" action instead.'
700+
)
701+
702+
678703
class LoadSBOMsForm(BaseProductImportFormView):
679704
project_type = ScanCodeProject.ProjectType.LOAD_SBOMS
680705
pipeline_name = "load_sbom"
681706

682707
input_file = SmartFileField(
683708
label=_("SBOM file or zip archive"),
684709
extensions=["json", "ABOUT", "zip"],
710+
validators=[validate_sbom_file],
685711
required=True,
686712
)
687713

product_portfolio/templates/product_portfolio/import_from_scan.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ <h1 class="header-title">
2424
</div>
2525
</div>
2626

27-
{% include 'includes/messages_alert.html' %}
28-
2927
<div class="row">
3028
<div class="col-9">
29+
{% include 'includes/messages_alert.html' %}
30+
3131
<div class="alert alert-primary" role="alert">
3232
When you upload your <strong>JSON-formatted ScanCode output results</strong>, DejaCode will:
3333
<ul class="mb-0 mt-2">

product_portfolio/templates/product_portfolio/import_manifests_form.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ <h1 class="header-title">
2121
</div>
2222
</div>
2323

24-
{% include 'includes/messages_alert.html' %}
25-
2624
<div class="row">
2725
<div class="col-9">
26+
{% include 'includes/messages_alert.html' %}
27+
2828
<div class="alert alert-success">
2929
<div>
3030
Supports resolving packages for:

product_portfolio/templates/product_portfolio/load_sboms_form.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ <h1 class="header-title">
2121
</div>
2222
</div>
2323

24-
{% include 'includes/messages_alert.html' %}
25-
2624
<div class="row">
2725
<div class="col-9">
26+
{% include 'includes/messages_alert.html' %}
27+
2828
<div class="alert alert-success">
2929
<div>
3030
DejaCode supports the following SBOM formats:

0 commit comments

Comments
 (0)