Skip to content

fix(api): answer 400 instead of 500 for deeply nested JSON strings - #17115

Open
svader0 wants to merge 5 commits into
DefectDojo:devfrom
svader0:fix/tag-list-deep-json
Open

svader0 wants to merge 5 commits into
DefectDojo:devfrom
svader0:fix/tag-list-deep-json

Conversation

@svader0

@svader0 svader0 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A few API fields accept JSON as a string and parse it with json.loads. If the string is a very deeply nested array, such as "[" * 100000 + "]" * 100000, the request fails with a 500.

On Python 3.14, json.loads raises RecursionError only when the real C stack runs out, so the depth that fails depends on the host. With a large stack, the string parses, and Postgres then fails with stack depth limit exceeded when the value goes into a JSON column. So catching RecursionError alone is not enough.

This PR adds parse_json_string in dojo/utils.py. It refuses a string nested deeper than 10000 levels before it calls json.loads. It also turns a RecursionError into the same JSONDecodeError. 10000 is the same limit as Go's encoding/json, and Postgres jsonb accepts that depth. Brackets inside JSON strings do not count. The depth scan only runs when the string holds more than 10000 [ and { characters.

These fields now use it and answer a 400:

  • TagListSerializerField.to_internal_value (dojo/api_v2/serializers.py): the tags field on every serializer that uses it. It answers the field's invalid_json error.
  • RequestResponseSerializerField.to_internal_value (dojo/finding/api/serializer.py): req_resp on POST /api/v2/findings/<id>/request_response/. It answers the field's invalid_json error.
  • JIRAProjectSerializer.validate (dojo/jira/api/serializers.py): custom_fields, close_transition_fields and reopen_transition_fields. They answer the existing Invalid JSON: ... error.

It also fixes an empty req_resp string. That code passed a list to the JSON parser, which raised TypeError and answered a 500. It now answers the same as an empty list.

Test results

  • unittests/test_utils.py: TestParseJsonString (depth 10000 parses, 10001 fails, nested objects, brackets and escaped quotes inside strings, RecursionError).
  • unittests/test_tags.py: TagTests.test_finding_patch_tags_deeply_nested_string_is_invalid_json
  • unittests/test_rest_framework.py: FindingRequestResponseTest.test_request_response_post_deeply_nested_string_is_invalid_json, FindingRequestResponseTest.test_request_response_post_empty_string_matches_empty_list, JiraProjectTest.test_json_field_deeply_nested_string_is_invalid_json

The API tests send depth 10001 and depth 100000. Without the fix, depth 10001 gets the wrong error (or a 201 for JIRA) and depth 100000 gets a 500. I also ran test_tags, test_utils, test_rest_framework, test_apiv2_limit_reqresp, test_jira_config_engagement and test_jira_helper: 976 tests, OK. ruff check . with ruff==0.16.10 passes.

Not in this PR: if the request body itself is deeply nested (not a string), DRF's JSONParser can raise the same RecursionError, or the value can reach a JSON column and fail in Postgres. That needs its own fix.

Documentation

No documentation change. The API keeps the same error messages 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.

@dryrunsecurity

dryrunsecurity Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

DryRun Security

This pull request contains three critical findings where the user 'svader0' modified sensitive codepaths in dojo/api_v2/serializers.py, dojo/jira/api/serializers.py, and dojo/utils.py without being on the allowed authors list.

🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/api_v2/serializers.py (drs_1e4c1a65)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/api_v2/serializers.py' matches configured sensitive codepath pattern 'dojo/api_v2/*.py' and was modified by 'svader0' (commit 97f1ad3) who is not in the allowed authors list.
🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/jira/api/serializers.py (drs_51d3fd76)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/jira/api/serializers.py' matches configured sensitive codepath pattern 'dojo/jira/**/*.py' and was modified by 'svader0' (commit 9a15b93) who is not in the allowed authors list.
🔴 Configured Sensitive Codepath Modified by Non-Allowed Author in dojo/utils.py (drs_d5b3e202)
Vulnerability Configured Sensitive Codepath Modified by Non-Allowed Author
Description File 'dojo/utils.py' matches configured sensitive codepath pattern 'dojo/utils.py' and was modified by 'svader0' (commit a4cefe2) 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.

A tag list sent in string form goes through json.loads, and the field
caught only ValueError. A deeply nested array makes the C decoder raise
RecursionError instead, so the request failed with a 500. Catch it too,
so the answer is the field's invalid_json 400.
…IRA fields

The request_response req_resp field and the JIRA project custom_fields,
close_transition_fields and reopen_transition_fields run json.loads on a
string and catch only ValueError or JSONDecodeError. A deeply nested
array makes the C decoder raise RecursionError, so the request failed
with a 500. Catch it too, the same as the tag list field.
Python 3.14 raises RecursionError from json.loads only when the real C
stack runs out, so the depth that fails depends on the host. A fixed
limit of 10000 levels matches Go's encoding/json and stays inside what
Postgres jsonb accepts.
The tag list, req_resp and JIRA project JSON fields now parse their
string form with parse_json_string. Catching RecursionError alone gave
a 400 on some hosts and a 500 on others: with a large stack, depth
100000 parsed and Postgres then failed with stack depth limit exceeded.
The tests now also send depth 10001, which parses on any normal stack,
so they fail the same way everywhere without the limit.
An empty req_resp string set data to a list and then passed that list to
the JSON parser, which raised TypeError and answered a 500.
@svader0
svader0 force-pushed the fix/tag-list-deep-json branch from cd9f373 to e418d84 Compare December 5, 2026 16:10

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant