Opened 53 minutes ago
#37418 new Bug
forms.JSONField crashes with RecursionError on deeply nested input
| Reported by: | Philip Sørensen | Owned by: | |
|---|---|---|---|
| Component: | Forms | Version: | 6.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Philip Sørensen | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
forms.JSONField.to_python() and bound_data() only catch json.JSONDecodeError. When the submitted value is nested more deeply than the recursion limit, json.loads() raises RecursionError instead, so the form crashes (HTTP 500) rather than showing "Enter a valid JSON.". About 4 KB of input is enough, well within DATA_UPLOAD_MAX_MEMORY_SIZE:
>>> from django import forms
>>> forms.JSONField().clean("[" * 100_000 + "]" * 100_000)
RecursionError: maximum recursion depth exceeded while decoding a JSON array from a unicode string
This affects any form or ModelForm with a JSONField, including the admin. The contrib.postgres HStoreField form field has the same pattern.
Patch: also catch RecursionError in those three places, with regression tests.