-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Security: Mitigate Recursion-based DoS in ListField and DictField #9858
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
Open
mahdirahimi1999
wants to merge
6
commits into
encode:main
Choose a base branch
from
mahdirahimi1999:fix/prevent-recursion-limit-fields
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+635
−7
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
acc3fd7
Add max_depth constraint to ListField and DictField
mahdirahimi1999 6ad1096
Add max_depth parameter to prevent DoS from deeply nested data
mahdirahimi1999 8ff5b53
Fix max_depth docs to clarify level-based counting with examples
mahdirahimi1999 fc5c1a6
Merge branch 'main' into fix/prevent-recursion-limit-fields
mahdirahimi1999 60d4c60
Merge branch 'main' into fix/prevent-recursion-limit-fields
mahdirahimi1999 af7d5f4
Fix ListField.bind() to preserve explicit max_depth
mahdirahimi1999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -471,12 +471,13 @@ Requires either the `Pillow` package or `PIL` package. The `Pillow` package is | |
|
|
||
| A field class that validates a list of objects. | ||
|
|
||
| **Signature**: `ListField(child=<A_FIELD_INSTANCE>, allow_empty=True, min_length=None, max_length=None)` | ||
| **Signature**: `ListField(child=<A_FIELD_INSTANCE>, allow_empty=True, min_length=None, max_length=None, max_depth=None)` | ||
|
|
||
| * `child` - A field instance that should be used for validating the objects in the list. If this argument is not provided then objects in the list will not be validated. | ||
| * `allow_empty` - Designates if empty lists are allowed. | ||
| * `min_length` - Validates that the list contains no fewer than this number of elements. | ||
| * `max_length` - Validates that the list contains no more than this number of elements. | ||
| * `max_depth` - Validates that nesting depth does not exceed this value. This applies to both the field schema depth and the raw input data depth. Depth of 0 permits the field itself but no nesting. Defaults to `None` (no limit). | ||
|
||
|
|
||
| For example, to validate a list of integers you might use something like the following: | ||
|
|
||
|
|
@@ -495,10 +496,11 @@ We can now reuse our custom `StringListField` class throughout our application, | |
|
|
||
| A field class that validates a dictionary of objects. The keys in `DictField` are always assumed to be string values. | ||
|
|
||
| **Signature**: `DictField(child=<A_FIELD_INSTANCE>, allow_empty=True)` | ||
| **Signature**: `DictField(child=<A_FIELD_INSTANCE>, allow_empty=True, max_depth=None)` | ||
|
|
||
| * `child` - A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated. | ||
| * `allow_empty` - Designates if empty dictionaries are allowed. | ||
| * `max_depth` - Validates that nesting depth does not exceed this value. This applies to both the field schema depth and the raw input data depth. Depth of 0 permits the field itself but no nesting. Defaults to `None` (no limit). | ||
|
|
||
| For example, to create a field that validates a mapping of strings to strings, you would write something like this: | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think adding a default limit for max_depth would be a good idea? Users could still override it if needed, but having a cushion would help even if they don't set a limit. What’s your take on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that, but a default limit might break existing setups that legitimately use deep nesting. Keeping it None by default feels safer for backward compatibility. That said, we should definitely highlight it in the docs as a security best practice.