-
Notifications
You must be signed in to change notification settings - Fork 399
feat(python): add configurable size guardrails #3429
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
Merged
chaokunyang
merged 8 commits into
apache:main
from
eyad-hazem-elmorsy:feat/add-python-size-guardrails
Mar 6, 2026
+209
−7
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e6e2c50
feat(python): add configurable size guardrails
eyad-hazem-elmorsy e8ec07e
rename serialization limits and remove string max length
eyad-hazem-elmorsy 2e7af63
remove the use of max_binary_size and make tests minimal
eyad-hazem-elmorsy acab3db
Merge branch 'main' into feat/add-python-size-guardrails
eyad-hazem-elmorsy 12910ef
Merge branch 'main' into feat/add-python-size-guardrails
eyad-hazem-elmorsy ebe10a8
Merge branch 'main' into feat/add-python-size-guardrails
eyad-hazem-elmorsy 7011705
add max_binary_size field to buffer class
eyad-hazem-elmorsy f0f8556
pass max_binary_size to buffer in fory instance and provide tests
eyad-hazem-elmorsy 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
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
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
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 |
|---|---|---|
|
|
@@ -1078,6 +1078,8 @@ cdef class Fory: | |
| cdef public bint is_peer_out_of_band_enabled | ||
| cdef int32_t max_depth | ||
| cdef int32_t depth | ||
| cdef public int32_t max_collection_size | ||
| cdef public int32_t max_binary_size | ||
| cdef object _output_stream | ||
|
|
||
| def __init__( | ||
|
|
@@ -1090,6 +1092,8 @@ cdef class Fory: | |
| max_depth: int = 50, | ||
| field_nullable: bool = False, | ||
| meta_compressor=None, | ||
| max_collection_size: int = 1_000_000, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also have max_binary_size as parameter too, and pass it to Buffer in fory instance |
||
| max_binary_size: int = 64 * 1024 * 1024, | ||
| ): | ||
| """ | ||
| Initialize a Fory serialization instance. | ||
|
|
@@ -1128,6 +1132,17 @@ cdef class Fory: | |
| field_nullable: Treat all dataclass fields as nullable regardless of | ||
| Optional annotation. | ||
|
|
||
| max_collection_size: Maximum allowed size for collections (lists, sets, tuples) | ||
| and maps (dicts) during deserialization. This limit is used to prevent | ||
| out-of-memory attacks from malicious payloads that claim extremely large | ||
| collection sizes, as collections preallocate memory based on the declared | ||
| size. Raises an exception if exceeded. Default is 1,000,000. | ||
|
|
||
| max_binary_size: Maximum allowed size in bytes for binary data reads during | ||
| deserialization (default: 64 MB). Raises an exception if a single binary | ||
| read exceeds this limit, preventing out-of-memory attacks from malicious | ||
| payloads that claim extremely large binary sizes. | ||
|
|
||
| Example: | ||
| >>> # Python-native mode with reference tracking | ||
| >>> fory = Fory(ref=True) | ||
|
|
@@ -1149,14 +1164,16 @@ cdef class Fory: | |
| self.type_resolver = TypeResolver(self, meta_share=compatible, meta_compressor=meta_compressor) | ||
| self.serialization_context = SerializationContext(fory=self, scoped_meta_share_enabled=compatible) | ||
| self.type_resolver.initialize() | ||
| self.buffer = Buffer.allocate(32) | ||
| self.max_binary_size = max_binary_size | ||
| self.buffer = Buffer.allocate(32, max_binary_size=max_binary_size) | ||
| self.buffer_callback = None | ||
| self._buffers = None | ||
| self._unsupported_callback = None | ||
| self._unsupported_objects = None | ||
| self.is_peer_out_of_band_enabled = False | ||
| self.depth = 0 | ||
| self.max_depth = max_depth | ||
| self.max_collection_size = max_collection_size | ||
| self._output_stream = None | ||
|
|
||
| def register_serializer(self, cls: Union[type, TypeVar], Serializer serializer): | ||
|
|
@@ -1508,7 +1525,7 @@ cdef class Fory: | |
| """ | ||
| try: | ||
| if type(buffer) == bytes: | ||
| buffer = Buffer(buffer) | ||
| buffer = Buffer(buffer, max_binary_size=self.max_binary_size) | ||
| return self._deserialize(buffer, buffers, unsupported_objects) | ||
| finally: | ||
| self.reset_read() | ||
|
|
||
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.
Should also have max_binary_size as parameter too, and pass it to Buffer in fory instance