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

Request/Response API CRUD Endpoints #11365

Merged
merged 19 commits into from
Dec 19, 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
42 changes: 42 additions & 0 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import collections
import json
import logging
Expand Down Expand Up @@ -45,6 +46,7 @@
Answer,
Answered_Survey,
App_Analysis,
BurpRawRequestResponse,
Check_List,
ChoiceAnswer,
ChoiceQuestion,
Expand Down Expand Up @@ -388,6 +390,46 @@ class BurpRawRequestResponseSerializer(serializers.Serializer):
req_resp = RequestResponseSerializerField(required=True)


class BurpRawRequestResponseMultiSerializer(serializers.ModelSerializer):
burpRequestBase64 = serializers.CharField()
burpResponseBase64 = serializers.CharField()

def to_representation(self, data):
return {
"id": data.id,
"finding": data.finding.id,
"burpRequestBase64": data.burpRequestBase64.decode("utf-8"),
"burpResponseBase64": data.burpResponseBase64.decode("utf-8"),
}

def validate(self, data):
b64request = data.get("burpRequestBase64", None)
b64response = data.get("burpResponseBase64", None)
finding = data.get("finding", None)
# Make sure all fields are present
if not b64request or not b64response or not finding:
msg = "burpRequestBase64, burpResponseBase64, and finding are required."
raise ValidationError(msg)
# Verify we have true base64 decoding
try:
base64.b64decode(b64request, validate=True)
base64.b64decode(b64response, validate=True)
except Exception as e:
msg = "Inputs need to be valid base64 encodings"
raise ValidationError(msg) from e
# Encode the data in utf-8 to remove any bad characters
data["burpRequestBase64"] = b64request.encode("utf-8")
data["burpResponseBase64"] = b64response.encode("utf-8")
# Run the model validation - an ValidationError will be raised if there is an issue
BurpRawRequestResponse(finding=finding, burpRequestBase64=b64request, burpResponseBase64=b64response).clean()

return data

class Meta:
model = BurpRawRequestResponse
fields = "__all__"


class MetaSerializer(serializers.ModelSerializer):
product = serializers.PrimaryKeyRelatedField(
queryset=Product.objects.all(),
Expand Down
18 changes: 18 additions & 0 deletions dojo/api_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,24 @@ def get_queryset(self):
return Note_Type.objects.all().order_by("id")


class BurpRawRequestResponseViewSet(
DojoModelViewSet,
):
serializer_class = serializers.BurpRawRequestResponseMultiSerializer
queryset = BurpRawRequestResponse.objects.none()
filter_backends = (DjangoFilterBackend,)
filterset_fields = ["finding"]

def get_queryset(self):
results = BurpRawRequestResponse.objects.all()
empty_value = b""
results = results.exclude(
burpRequestBase64__exact=empty_value,
burpResponseBase64__exact=empty_value,
)
return results.order_by("id")


# Authorization: superuser
class NotesViewSet(
mixins.UpdateModelMixin,
Expand Down
2 changes: 2 additions & 0 deletions dojo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from dojo.api_v2.views import (
AnnouncementViewSet,
AppAnalysisViewSet,
BurpRawRequestResponseViewSet,
ConfigurationPermissionViewSet,
CredentialsMappingViewSet,
CredentialsViewSet,
Expand Down Expand Up @@ -156,6 +157,7 @@
v2_api.register(r"product_type_groups", ProductTypeGroupViewSet, basename="product_type_group")
v2_api.register(r"regulations", RegulationsViewSet, basename="regulations")
v2_api.register(r"reimport-scan", ReImportScanView, basename="reimportscan")
v2_api.register(r"request_response_pairs", BurpRawRequestResponseViewSet, basename="request_response_pairs")
v2_api.register(r"risk_acceptance", RiskAcceptanceViewSet, basename="risk_acceptance")
v2_api.register(r"roles", RoleViewSet, basename="role")
v2_api.register(r"sla_configurations", SLAConfigurationViewset, basename="sla_configurations")
Expand Down
2 changes: 0 additions & 2 deletions unittests/test_apiv2_methods_and_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Benchmark_Product_Summary,
Benchmark_Requirement,
Benchmark_Type,
BurpRawRequestResponse,
Choice,
Contact,
FileAccessToken,
Expand Down Expand Up @@ -92,7 +91,6 @@ def setUp(self):
Product_Line,
Report_Type,
CWE,
BurpRawRequestResponse,
FileAccessToken,
UserAnnouncement,
BannerConf,
Expand Down
25 changes: 25 additions & 0 deletions unittests/test_rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from dojo.api_v2.views import (
AnnouncementViewSet,
AppAnalysisViewSet,
BurpRawRequestResponseViewSet,
ConfigurationPermissionViewSet,
CredentialsMappingViewSet,
CredentialsViewSet,
Expand Down Expand Up @@ -3049,3 +3050,27 @@ def __init__(self, *args, **kwargs):
self.test_type = TestType.STANDARD
self.deleted_objects = 1
BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs)


class BurpRawRequestResponseTest(BaseClass.BaseClassTest):
fixtures = ["dojo_testdata.json"]

def __init__(self, *args, **kwargs):
self.endpoint_model = BurpRawRequestResponse
self.endpoint_path = "request_response_pairs"
self.viewname = "request_response_pairs"
self.viewset = BurpRawRequestResponseViewSet
self.payload = {
"finding": 2,
"burpRequestBase64": "cmVxdWVzdAo=",
"burpResponseBase64": "cmVzcG9uc2UK",
}

self.update_fields = {
"finding": 2,
"burpRequestBase64": "cmVxdWVzdCAtIGVkaXRlZAo=",
"burpResponseBase64": "cmVzcG9uc2UgLSBlZGl0ZWQK",
}
self.test_type = TestType.STANDARD
self.deleted_objects = 1
BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs)
Loading