Skip to content

fix(api): answer 400 for a deeply nested JSON body and an empty req_resp - #17116

Open
svader0 wants to merge 1 commit into
DefectDojo:devfrom
svader0:fix/json-body-deep-nesting
Open

svader0 wants to merge 1 commit into
DefectDojo:devfrom
svader0:fix/json-body-deep-nesting

Conversation

@svader0

@svader0 svader0 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Description

This PR fixes two API requests that returned a 500 instead of a 400.

  1. A JSON request body that is a very deeply nested array, for example {"tags": + "[" * 100000 + "]" * 100000 + }. DRF's JSONParser calls json.load and catches only ValueError. The C JSON decoder raises RecursionError for this body, so the error escaped on every JSON endpoint. The new DojoJSONParser (dojo/api_v2/parsers.py) turns RecursionError into the same ParseError that DRF uses for bad JSON. REST_FRAMEWORK["DEFAULT_PARSER_CLASSES"] now lists it in place of JSONParser, and keeps FormParser and MultiPartParser. The existing exception handler then answers a 400 with {"message": "JSON request content is malformed"}.
  2. An empty req_resp string on POST /api/v2/findings/<id>/request_response/. RequestResponseSerializerField.to_internal_value set data = [] and then called json.loads([]), which raised TypeError. It now returns [] early, the same as TagListSerializerField does for a blank string.

This is the follow-up that #17115 names. The two PRs touch nearby lines but merge cleanly in either order.

Test results

Two new tests in unittests/test_rest_framework.py, FindingRequestResponseTest:

  • test_deeply_nested_json_body_is_parse_error: before the fix, 400 != 500 with RecursionError in the log. After the fix it passes, and the finding title stays unchanged.
  • test_request_response_post_empty_string_is_empty_list: before the fix, 200 != 500 with TypeError in the log. After the fix it passes, and no pair is created.

I also ran unittests.test_tags and the FindingsTest, FindingRequestResponseTest, ImportScanTest, ReimportScanTest, ProductTest and JiraProjectTest classes with and without the fix. The fix changes no other result. ruff check with ruff==0.16.10 passes.

Documentation

No documentation change. The API uses the error message it already uses for bad JSON.

Checklist

  • Make sure to rebase your PR against the very latest dev.
  • Submit all PRs, features and bug fixes alike, against the dev branch.
  • Give a meaningful name to your PR, as it may end up being used in the release notes.
  • Your code is Ruff compliant (see ruff.toml).
  • Your code is python 3.13 compliant.
  • If this is a new feature and not a bug fix, you've included the proper documentation in the docs at https://github.com/DefectDojo/django-DefectDojo/tree/dev/docs as part of this PR.
  • Model changes must include the necessary migrations in the dojo/db_migrations folder.
  • Add applicable tests to the unit tests.
  • Add the proper label to categorize your PR.

DRF's JSONParser calls json.load and catches only ValueError. A request
body that is a deeply nested array makes the C decoder raise
RecursionError, so every JSON endpoint answered a 500. DojoJSONParser
turns RecursionError into the same ParseError, and it is now the default
JSON parser. The exception handler already maps that to a 400.

RequestResponseSerializerField set an empty req_resp string to [] and
then called json.loads([]), which raised TypeError and gave a 500. It now
returns [] early, the same as TagListSerializerField.
@svader0 svader0 added this to the 3.6.0 milestone Nov 30, 2026
@github-actions github-actions Bot added the settings_changes Needs changes to settings.py based on changes in settings.dist.py included in this PR label Nov 30, 2026
@dryrunsecurity

Copy link
Copy Markdown

DryRun Security

This pull request contains a critical finding where the sensitive file 'dojo/api_v2/parsers.py' was modified by an unauthorized author, 'svader0'. Although the issue is flagged as failing, it is not currently set to block the merge.

🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/api_v2/parsers.py (drs_9046d68c)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/api_v2/parsers.py' matches configured sensitive codepath pattern 'dojo/api_v2/*.py' and was modified by 'svader0' (commit b440617) who is not in the allowed authors list.

We've notified @mtesauro.


Comment to provide feedback on these findings.

Report false positive: @dryrunsecurity fp [FINDING ID] [FEEDBACK]
Report low-impact: @dryrunsecurity nit [FINDING ID] [FEEDBACK]

Example: @dryrunsecurity fp drs_90eda195 This code is not user-facing

All finding details can be found in the DryRun Security Dashboard.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apiv2 bugfix settings_changes Needs changes to settings.py based on changes in settings.dist.py included in this PR unittests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant