Update standard render: use own dicts serializer instead of json.dump#69
Merged
Update standard render: use own dicts serializer instead of json.dump#69
Conversation
| if obj is None: | ||
| return "null" | ||
|
|
||
| if isinstance(obj, bool): |
There was a problem hiding this comment.
Security: The dump_json() function lacks circular reference detection, making it vulnerable to infinite recursion attacks that could crash the application.
📝 Committable Code Suggestion
‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.
Suggested change
| if isinstance(obj, bool): | |
| def dump_json(obj, _seen=None, _depth=0, max_depth=100) -> str: | |
| ''' | |
| Secure version of dump_json with circular reference detection and recursion limits. | |
| Serializes Python objects to JSON with single quotes for strings. | |
| ''' | |
| # Initialize seen set for first call | |
| if _seen is None: | |
| _seen = set() | |
| # Check recursion depth | |
| if _depth > max_depth: | |
| raise RecursionError(f"Maximum recursion depth exceeded ({max_depth})") | |
| # Handle None | |
| if obj is None: | |
| return 'null' | |
| # Handle basic types | |
| if isinstance(obj, (int, float, bool)): | |
| return str(obj).lower() | |
| # Handle strings | |
| if isinstance(obj, str): | |
| # Escape single quotes and backslashes | |
| escaped = obj.replace("\\", "\\\\").replace("'", "\\'") | |
| return f"'{escaped}'" | |
| # Handle lists | |
| if isinstance(obj, (list, tuple)): | |
| items = [] | |
| for item in obj: | |
| items.append(dump_json(item, _seen.copy(), _depth + 1, max_depth)) | |
| return f"[{', '.join(items)}]" | |
| # Handle dictionaries | |
| if isinstance(obj, dict): | |
| # Check for circular references | |
| obj_id = id(obj) | |
| if obj_id in _seen: | |
| raise ValueError("Circular reference detected in object") | |
| _seen.add(obj_id) | |
| items = [] | |
| for key, value in obj.items(): | |
| key_str = dump_json(key, _seen.copy(), _depth + 1, max_depth) | |
| value_str = dump_json(value, _seen.copy(), _depth + 1, max_depth) | |
| items.append(f"{key_str}: {value_str}") | |
| return f"{{{', '.join(items)}}}" | |
| # Handle other objects by converting to string | |
| try: | |
| # Limit string size to prevent DoS | |
| obj_str = str(obj) | |
| if len(obj_str) > 10000: # Reasonable limit | |
| obj_str = obj_str[:10000] + "...(truncated)" | |
| return f"'{obj_str}'" | |
| except Exception as e: | |
| return f"'<Object representation error: {str(e)}>'" | |
StpMax
approved these changes
Oct 16, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Quoting rules for string content of the parser:
But standart render used json.dump. It added more qouted symbols that were parsed incorrectly, for example: