|
| 1 | +From b210265f2b4c05e396e4590feb0c38ae6ae0cca4 Mon Sep 17 00:00:00 2001 |
| 2 | +From: aviralgarg05 <gargaviral99@gmail.com> |
| 3 | +Date: Fri, 9 Jan 2026 20:59:10 +0530 |
| 4 | +Subject: [PATCH] Fix Any recursion depth bypass in Python |
| 5 | + json_format.ParseDict |
| 6 | + |
| 7 | +This fixes a security vulnerability where nested google.protobuf.Any messages |
| 8 | +could bypass the max_recursion_depth limit, potentially leading to denial of |
| 9 | +service via stack overflow. |
| 10 | + |
| 11 | +The root cause was that _ConvertAnyMessage() was calling itself recursively |
| 12 | +via methodcaller() for nested well-known types, bypassing the recursion depth |
| 13 | +tracking in ConvertMessage(). |
| 14 | + |
| 15 | +The fix routes well-known type parsing through ConvertMessage() to ensure |
| 16 | +proper recursion depth accounting for all message types including nested Any. |
| 17 | + |
| 18 | +Fixes #25070 |
| 19 | + |
| 20 | +Closes #25239 |
| 21 | + |
| 22 | +Upstream Patch Reference:https://github.com/protocolbuffers/protobuf/commit/c4eda3e58680528147a4cc7e2b3c9044f795c9c9 |
| 23 | +--- |
| 24 | + .../python/google/protobuf/json_format.py | 21 ++++++++++++++++--- |
| 25 | + 1 file changed, 18 insertions(+), 3 deletions(-) |
| 26 | + |
| 27 | +diff --git a/third_party/protobuf/python/google/protobuf/json_format.py b/third_party/protobuf/python/google/protobuf/json_format.py |
| 28 | +index 4d76d021..63f21cbb 100644 |
| 29 | +--- a/third_party/protobuf/python/google/protobuf/json_format.py |
| 30 | ++++ b/third_party/protobuf/python/google/protobuf/json_format.py |
| 31 | +@@ -459,9 +459,11 @@ _INT_OR_FLOAT = six.integer_types + (float,) |
| 32 | + class _Parser(object): |
| 33 | + """JSON format parser for protocol message.""" |
| 34 | + |
| 35 | +- def __init__(self, ignore_unknown_fields, descriptor_pool): |
| 36 | ++ def __init__(self, ignore_unknown_fields, descriptor_pool, max_recursion_depth=100): |
| 37 | + self.ignore_unknown_fields = ignore_unknown_fields |
| 38 | + self.descriptor_pool = descriptor_pool |
| 39 | ++ self.max_recursion_depth = max_recursion_depth |
| 40 | ++ self.recursion_depth = 0 |
| 41 | + |
| 42 | + def ConvertMessage(self, value, message): |
| 43 | + """Convert a JSON object into a message. |
| 44 | +@@ -473,6 +475,17 @@ class _Parser(object): |
| 45 | + Raises: |
| 46 | + ParseError: In case of convert problems. |
| 47 | + """ |
| 48 | ++ # Increment recursion depth at message entry. The max_recursion_depth limit |
| 49 | ++ # is exclusive: a depth value equal to max_recursion_depth will trigger an |
| 50 | ++ # error. For example, with max_recursion_depth=5, nesting up to depth 4 is |
| 51 | ++ # allowed, but attempting depth 5 raises ParseError. |
| 52 | ++ self.recursion_depth += 1 |
| 53 | ++ if self.recursion_depth > self.max_recursion_depth: |
| 54 | ++ raise ParseError( |
| 55 | ++ 'Message too deep. Max recursion depth is {0}'.format( |
| 56 | ++ self.max_recursion_depth |
| 57 | ++ ) |
| 58 | ++ ) |
| 59 | + message_descriptor = message.DESCRIPTOR |
| 60 | + full_name = message_descriptor.full_name |
| 61 | + if _IsWrapperMessage(message_descriptor): |
| 62 | +@@ -481,6 +494,7 @@ class _Parser(object): |
| 63 | + methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self) |
| 64 | + else: |
| 65 | + self._ConvertFieldValuePair(value, message) |
| 66 | ++ self.recursion_depth -= 1 |
| 67 | + |
| 68 | + def _ConvertFieldValuePair(self, js, message): |
| 69 | + """Convert field value pairs into regular message. |
| 70 | +@@ -612,8 +626,9 @@ class _Parser(object): |
| 71 | + if _IsWrapperMessage(message_descriptor): |
| 72 | + self._ConvertWrapperMessage(value['value'], sub_message) |
| 73 | + elif full_name in _WKTJSONMETHODS: |
| 74 | +- methodcaller( |
| 75 | +- _WKTJSONMETHODS[full_name][1], value['value'], sub_message)(self) |
| 76 | ++ # For well-known types (including nested Any), use ConvertMessage |
| 77 | ++ # to ensure recursion depth is properly tracked |
| 78 | ++ self.ConvertMessage(value['value'], sub_message) |
| 79 | + else: |
| 80 | + del value['@type'] |
| 81 | + self._ConvertFieldValuePair(value, sub_message) |
| 82 | +-- |
| 83 | +2.45.4 |
| 84 | + |
0 commit comments