Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileUpload Base64 extension fix #11203

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import logging
import os
import re
from datetime import datetime

Expand Down Expand Up @@ -803,20 +802,8 @@ class Meta:

def validate(self, data):
if file := data.get("file"):
ext = os.path.splitext(file.name)[1] # [0] returns path+filename
valid_extensions = settings.FILE_UPLOAD_TYPES
if ext.lower() not in valid_extensions:
if accepted_extensions := f"{', '.join(valid_extensions)}":
msg = (
"Unsupported extension. Supported extensions are as "
f"follows: {accepted_extensions}"
)
else:
msg = (
"File uploads are prohibited due to the list of acceptable "
"file extensions being empty"
)
raise ValidationError(msg)
# the clean will validate the file extensions and raise a Validation error if the extensions are not accepted
FileUpload(title=file.name, file=file).clean()
return data
return None

Expand Down
23 changes: 23 additions & 0 deletions dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import warnings
from datetime import datetime
from pathlib import Path
from uuid import uuid4

import hyperlink
Expand Down Expand Up @@ -741,6 +742,28 @@ def get_accessible_url(self, obj, obj_id):

return f"access_file/{self.id}/{obj_id}/{obj_type}"

def clean(self):
if not self.title:
self.title = "<No Title>"

valid_extensions = settings.FILE_UPLOAD_TYPES

# why does this not work with self.file....
if self.file:
file_name = self.file.url
else:
file_name = self.title
if Path(file_name).suffix.lower() not in valid_extensions:
if accepted_extensions := f"{', '.join(valid_extensions)}":
msg = (
_("Unsupported extension. Supported extensions are as follows: %s") % accepted_extensions
)
else:
msg = (
_("File uploads are prohibited due to the list of acceptable file extensions being empty")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting and a good catch. I'm not sure I would have thought of this case

)
raise ValidationError(msg)


class Product_Type(models.Model):

Expand Down
11 changes: 10 additions & 1 deletion dojo/tools/generic/json_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from dojo.models import Endpoint, Finding
import base64

from django.core.files.base import ContentFile

from dojo.models import Endpoint, FileUpload, Finding
from dojo.tools.parser_test import ParserTest


Expand Down Expand Up @@ -103,6 +107,11 @@ def _get_test_json(self, data):
endpoint = Endpoint(**endpoint_item)
finding.unsaved_endpoints.append(endpoint)
if unsaved_files:
for unsaved_file in unsaved_files:
data = base64.b64decode(unsaved_file.get("data"))
title = unsaved_file.get("title", "<No title>")
FileUpload(title=title, file=ContentFile(data)).clean()

finding.unsaved_files = unsaved_files
if finding.cve:
finding.unsaved_vulnerability_ids = [finding.cve]
Expand Down
16 changes: 16 additions & 0 deletions unittests/scans/generic/test_with_image_no_ext.json

Large diffs are not rendered by default.

Loading