Commit graph

1400 commits

Author SHA1 Message Date
Protobuf Team Bot
35cd01f9fe Updating version.json and repo version numbers to: 35.1 2026-06-11 09:26:38 -07:00
Jie Luo
4469e3840b
bazel 9 tests for csharp, hpb, objc, php, python, rust and upb (#27598)
PiperOrigin-RevId: 918164613
cherry pick f9e028f
2026-05-26 12:51:55 -07:00
Protobuf Team Bot
d52a96a589 Updating version.json and repo version numbers to: 35.1-dev 2026-05-19 12:53:04 -07:00
Protobuf Team Bot
e59364c38e Updating version.json and repo version numbers to: 35.0 2026-05-19 12:53:03 -07:00
Protobuf Team Bot
b20539c1f2 Updating version.json and repo version numbers to: 35.0-dev 2026-04-15 13:03:36 -07:00
Protobuf Team Bot
8a74921d1c Updating version.json and repo version numbers to: 35.0-rc1 2026-04-15 13:03:35 -07:00
Sakura501
8abff6bb4b fix Python text_format by adding an optional recursion depth limit (#26604)
## Summary
- fix Python `text_format` by adding an optional recursion depth limit
- add `max_recursion_depth` to `Parse`, `Merge`, `ParseLines`, and `MergeLines`
- keep the default behavior unchanged for compatibility
- enforce the configured limit consistently across root parsing, nested submessages, and expanded `Any`
- add regression tests for explicit opt-in depth enforcement and the default compatibility path

## Context
Python `text_format` parsing recursively descends through `_MergeField()` and `_MergeMessageField()`, and can also recurse through expanded `Any` payloads. Without a shared parser-side depth guard, deeply nested input can recurse until Python raises `RecursionError` instead of a controlled protobuf `ParseError`.

For applications that parse untrusted textproto, that makes deep nesting a denial-of-service primitive.

## Fix
This change adds an optional `max_recursion_depth` parameter to the public Python text format parsing entry points.

When the option is set, parsing tracks message depth through a shared helper and raises `ParseError` once the configured limit is exceeded. When the option is not set, parsing keeps the historical unbounded behavior so existing callers remain compatible.

## Testing
- `BAZEL_NO_APPLE_CPP_TOOLCHAIN=1 bazel test //python:text_format_test --test_output=errors`

Closes #26604

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/26604 from Sakura-501:fix-python-text-format-unbounded-recursion 235abcd492
PiperOrigin-RevId: 898014428
2026-04-10 20:33:59 -07:00
Protobuf Team Bot
f9821fcf4f Fix python SyntaxWarning
\( is not a valid escape sequence.

PiperOrigin-RevId: 895607647
2026-04-06 19:05:19 -07:00
Joshua Haberman
28e451233d Fix data race in CMessage lazy initialization for Python freethreading.
This change introduces a `LazyUniquePtr` class to manage the lazy initialization of `composite_fields` and `child_submessages` within `CMessage`. When Python's GIL is disabled, `LazyUniquePtr` uses atomic operations to ensure thread-safe initialization of these maps, preventing data races when multiple threads concurrently access fields that trigger their creation. A new test case is added to reproduce and verify the fix for a race condition in `GetFieldValue`.

We are forced to use placement new and placement delete for the `LazyUniquePtr` members, because the `CMessage` struct is currently not properly constructed or destroyed.  This makes the code a bit awkward, but changing the construction/destruction of CMessage seemed like too much to bite off in this CL.

PiperOrigin-RevId: 891703488
2026-03-30 07:23:10 -07:00
Protobuf Team Bot
7adc4b672d Change field_mask.py to avoid recursion
This makes it more robust in the face of deep fieldmasks.

Fixes https://github.com/protocolbuffers/protobuf/issues/26489

PiperOrigin-RevId: 889184207
2026-03-25 05:26:34 -07:00
Protobuf Team Bot
5ea7e147ea Internal change
PiperOrigin-RevId: 888231953
2026-03-23 12:46:13 -07:00
Joshua Haberman
8c1a9a4b01 Fixed data race in Python Free Threading by removing unnecessary SetHasBitForRepeated() call.
This CL concerns the following functions in C++:

```c++
class Reflection {
  const Message& GetRepeatedMessage(const Message& message,
                                    const FieldDescriptor* field,
                                    int index) const;

  Message* MutableRepeatedMessage(Message* message,
                                  const FieldDescriptor* field,
                                  int index) const;
}
```

Suppose a Python program contains the following code:

```
def foo(msg: MyMessage):
  submsg = msg.repeated_foo[2]  # (A)
  if also_mutate:
    submsg.abc = 1              # (B)
```

For line (A), we currently call `MutableRepeatedMessage()` in C++ to
obtain the submessage pointer, because the resulting `submsg` object in
Python is a conceptually mutable object that will require a non-const
pointer if/when the program hits line (B).

The `MutableRepeatedMessage(Message* msg, ...)` function in C++
currently mutates `msg` by setting the hasbit of the repeated field. But
this seems unnecessary, as the function requires that the requested
sub-message already exists; it does not create a new message. So it
should not be necessary to touch the hasbit of the message, and a TGP
confirms that all tests pass if we remove this call.

Once the `SetHasBitForRepeated()` call is removed, the `MutableRepeatedMessage()`
function no longer actually mutates the `msg` argument. This makes it
effectively safe to call concurrently (ie. it will no longer trigger
Undefined Behavior in C++, and the TSAN errors will go away), which
is why it fixes the free threading test. But it leaves us in an odd
state where we are still passing a non-`const` pointer to the same
message to two functions concurrently, which is not allowed under a
normal thread-compatible contract.

An alternate solution would be to call `GetRepeatedMessage()` instead,
and `const_cast<>` away the const in the returned `const Message&`.
But this is also violates the contract: users should not be casting
away `const`.

The root cause of this odd situation is that
`MutableRepeatedMessage(Message* msg)` requires a non-`const` `msg` not
because it actually *mutates* `msg`, but because it is trying to
propagate the const-ness of `msg` to all of its children.  The proto API
generally guarantees that a const message prevents mutation of not just
the top-level message, but of the entire tree of messages. If someone
passes you a `const` message pointer, that is supposed to render the
entire tree of messages under it immutable through that pointer.

What we wish we could express in `MutableRepeatedMessage(Message* msg)`
is: "this function requires that `msg` is mutable, but this function
will not actually mutate it, and is therefore safe to call concurrently."
But there is no way of expressing this in C++.
PiperOrigin-RevId: 886830933
2026-03-20 09:29:13 -07:00
Protobuf Team Bot
3edd61508b Fix type annotation for FindAllExtensionNumbers() to be a list rather than the more general Iterator.
DescriptorPool has a strict expectation that this return type must be a list.

PiperOrigin-RevId: 885275177
2026-03-17 16:44:08 -07:00
Protobuf Team Bot
cbe6403761 Add type hints to descriptor_database.py.
This change adds type annotations to the methods and instance variables within the DescriptorDatabase class, as well as to the helper function _ExtractSymbols. Forward references are used for types from descriptor_pb2.

PiperOrigin-RevId: 884793323
2026-03-16 21:43:11 -07:00
Joshua Haberman
3b0cfbf0a9 Fixed data race when accessing Python fields in free threaded builds.
To do this, we need to make CMessage use thread-safe maps for its sub-object caches.

PiperOrigin-RevId: 884515030
2026-03-16 10:28:49 -07:00
Protobuf Team Bot
9b38d6d9a1 Add decoding error details for UPB proto parsing failures.
PiperOrigin-RevId: 883263178
2026-03-13 11:42:08 -07:00
Joshua Haberman
ab14c0f8ad Fixed a bug in msg.MergeFrom(msg2) in Python.
In `FixupMessageAfterMerge()`, the loop over fields was exiting the entire function if a mutable field was found, but this skips other fields that may need fixing up.

PiperOrigin-RevId: 882766354
2026-03-12 14:04:14 -07:00
Hong Shin
e7465f100a py generate_docs: simplify copyright header
PiperOrigin-RevId: 882703990
2026-03-12 11:53:27 -07:00
Jie Luo
059dc7ed32 Fix NULL byte handling issue in Python Protobuf find symbols in pool
PiperOrigin-RevId: 882122977
2026-03-11 11:54:48 -07:00
Rachel Goldfinger
bd2543656e Fix arch tests by adding file to the Docker images
PiperOrigin-RevId: 881524967
2026-03-10 11:06:34 -07:00
Keith Smiley
f08d703292 Add support for bazel 9.x (#26201)
This doesn't flip the default bazel version, but instead makes this repo
compatible with the current version and bazel 9.x. The primary changes
for 9.x support are adding new load statements for things that were
previously built in. This cascaded into a few dep updates to pull in
their missing load statement fixes.

Closes #26201

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/26201 from keith:ks/add-support-for-bazel-9.x e8192d86b3c838f6c88d4a4adfebd07162debc0d
PiperOrigin-RevId: 878654773
2026-03-04 13:59:16 -08:00
Protobuf Team Bot
1c2af13202 Add missing upb_XXXDef_FindXXXByNameWithSize functions and index methods by name
PiperOrigin-RevId: 878471014
2026-03-04 07:06:24 -08:00
Mark Barolak
d875ed69b6 Cleanup:
* Remove unneeded "google/protobuf/stubs/common.h" includes
* Migrate uses of kuint64max over to the C++ standard std::numeric_limits<uint64_t>::max()

PiperOrigin-RevId: 875122434
2026-02-25 06:09:54 -08:00
Joshua Haberman
0ed798a0d2 Fixed race condition in free threaded builds related to the descriptor cache.
Prior to this CL, it was possible for the following sequence to occur:

|Thread 1|Thread 2|
|--------|--------|
|`obj = NewDescriptor(desc)`||
|`InsertCache(desc, obj)`||
|`Py_DECREF(obj)` (to 0)||
|`Dealloc(obj) {`||
||`LookupCache(desc) -> obj`|
||`Py_INCREF(obj)`|
|`  DeleteFromCache(obj)`||
|`}`||
||`Py_DECREF(obj)`|
||`Dealloc(obj)`|

This could lead to double-`Dealloc()` calls on a single object.  These calls could race, leading to TSAN failures.

We should look deeper into whether `GcTraverse()` and `GcClear()` still need critical sections.

PiperOrigin-RevId: 874084218
2026-02-23 08:30:44 -08:00
Joshua Haberman
1ea2c4bab5 Updated release builds to use Bazel 8 and platforms.
Bazel 8 no longer supports legacy toolchains, so all of our release toolchains have been updated to use platforms.

#test-continuous

PiperOrigin-RevId: 870017275
2026-02-13 21:47:47 -08:00
Jie Luo
b4c3fecb85 Add recursion guards for the following nested messages:
- map field for pure Python
- message_set_extension for Pure Python
- message_set_extension for UPB Python
https://github.com/protocolbuffers/protobuf/issues/25335

PiperOrigin-RevId: 868863633
2026-02-11 15:13:37 -08:00
Jie Luo
f10c1de25f Protobuf Python UPB Free Threading support.
Add obj_cache lock to pass current free threading tests on python upb.

PiperOrigin-RevId: 864903528
2026-02-03 09:04:20 -08:00
Aviral Garg
d2b001626d Fix Any recursion depth bypass in Python json_format.ParseDict (#25239)
This fixes a security vulnerability where nested google.protobuf.Any messages could bypass the max_recursion_depth limit, potentially leading to denial of service via stack overflow.

The root cause was that _ConvertAnyMessage() was calling itself recursively via methodcaller() for nested well-known types, bypassing the recursion depth tracking in ConvertMessage().

The fix routes well-known type parsing through ConvertMessage() to ensure proper recursion depth accounting for all message types including nested Any.

Fixes #25070

Closes #25239

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/25239 from aviralgarg05:fix-any-recursion-depth-bypass 3cbbcbea142593d3afd2ceba2db14b05660f62f4
PiperOrigin-RevId: 862740421
2026-01-29 10:17:18 -08:00
Mike Kruskal
0a3f90bc16 Use the toolchain protoc by default in internal rules.
#test-continuous

While we'd like to burn these down, they're still necessary at least for WKT to avoid cyclic dependencies.  This solution at least enabled prebuilt protoc for languages other than Java/Kotlin.

Fixes #25453

PiperOrigin-RevId: 862290462
2026-01-28 10:21:18 -08:00
Joshua Haberman
3a3560bb87 Removed the third_party/upb/upb/bazel directory.
We are absorbing its contents into other directories. This will reduce the 3:1 merge to `upb/bazel` in GitHub to 2:1 (soon to be 1:1).

PiperOrigin-RevId: 860299666
2026-01-23 16:35:18 -08:00
Mike Kruskal
e67b9cece7
Merge pull request #25438 from protocolbuffers/main-202601221720
Updating version.json and repo version numbers to: 35.0-dev
2026-01-22 10:01:36 -08:00
Etienne Pot
ea78297e0a Supports exporting int as int
PiperOrigin-RevId: 859620391
2026-01-22 08:35:26 -08:00
Protobuf Team Bot
8227cf1630 Updating version.json and repo version numbers to: 35-dev 2026-01-22 07:25:02 -08:00
Protobuf Team Bot
086093946d Automated Code Change
PiperOrigin-RevId: 858933397
2026-01-20 23:25:49 -08:00
Ionel Gog
7fd90b5314 Remove ref cycles introduced by self-calling nested functions.
PiperOrigin-RevId: 858670904
2026-01-20 11:11:02 -08:00
Oleh Prypin
46061cb99d Prevent crashes when creating objects during interpreter shutdown
(The crashes happen only since Python 3.13)

In particular:

* Skip adding to the weak map if `Py_IsFinalizing()` is true. Calling `PyUpb_WeakMap_Add` may be caused by user code that creates protos inside `__del__`!

* `PyUpb_DescriptorPool_Get` and subsequently `PyUpb_ObjCache_Get` may *also* be called if users cause `PyUpb_RepeatedContainer_GetOrCreateWrapper` to be called. So make it nullable and skip the assertion if currently shutting down.

This follows a prior attempted fix d57d2708b3 but expands the scope of it.

PiperOrigin-RevId: 856589514
2026-01-15 03:45:00 -08:00
Jie Luo
bbc9dd9e8a Drop Python 3.9 support
Python 3.9 reached its official End-of-Life (EOL) on October 31, 2025
https://devguide.python.org/versions/

PiperOrigin-RevId: 854341311
2026-01-09 14:20:23 -08:00
Jie Luo
c301c2ca28 Breaking Change: Remove deprecated UseDeprecatedLegacyJsonFieldConflicts()
https://protobuf.dev/news/2025-09-19/#cpp-remove-apis

PiperOrigin-RevId: 853477445
2026-01-07 17:35:56 -08:00
Mikita Belahlazau
4f076d951f Add metadata annotations for generated Python protobuf symbols.
The pyi generator now includes Kythe annotations for:
*   Extension field constants (e.g., `EXTENSION_FIELD`).
*   Field number constants (e.g., `STRING_FIELD_FIELD_NUMBER`).
*   `Create` methods in generated Stubby client classes.

These annotations allow the Python indexer to link these generated symbols back to their definitions in the `.proto` files.

PiperOrigin-RevId: 852971617
2026-01-06 16:05:56 -08:00
Protobuf Team Bot
ae67a4c195 Internal version update.
PiperOrigin-RevId: 852733884
2026-01-06 05:27:44 -08:00
Chris Kennelly
a70115f33f Breaking change: Add [[nodiscard]] to many APIs.
This covers two types of failures:
* Methods that are logically const and failure to consume the result indicates a bug
  (an unnecessary call, etc.)
* Methods that return significant errors (failure to parse, etc.) that should not be
  unintentionally ignored.

PiperOrigin-RevId: 852313694
2026-01-05 08:41:35 -08:00
Rachel Goldfinger
0404e66d8b Add conformance test for edition unstable
PiperOrigin-RevId: 850054867
2025-12-29 07:41:37 -08:00
Mike Kruskal
232f409e96 Avoid PyObject_CallMethod with pending errors, which always asserts.
This can happen from ignored errors during build (e.g. missing option dependencies).

PiperOrigin-RevId: 847929958
2025-12-22 17:10:01 -08:00
Protobuf Team Bot
4f74970c83 Automated Code Change
PiperOrigin-RevId: 846019877
2025-12-17 19:38:00 -08:00
Jie Luo
b76faa921f Breaking Change: Remove deprecated FieldDescriptor::label() in OSS. Use is_repeated() or is_required() instead
https://protobuf.dev/news/2025-09-19/#cpp-remove-apis

PiperOrigin-RevId: 845462045
2025-12-16 15:43:37 -08:00
Protobuf Team Bot
c4fb46840a Automated Code Change
PiperOrigin-RevId: 845123384
2025-12-16 00:29:08 -08:00
Protobuf Team Bot
60f0834f1c Fixed case of referencing invalid pointer.
When running __new__ MessageMeta, the cache access can crash because it's missing a validation on the object it is given.

PiperOrigin-RevId: 844820889
2025-12-15 10:14:13 -08:00
Chris Kennelly
d326c1d6da Prepare to make many APIs [[nodiscard]].
PiperOrigin-RevId: 844770264
2025-12-15 07:53:39 -08:00
Protobuf Team Bot
4459a20205 Support more chars in type URLs in the Python text-format parser.
Change the Python text-format parser to allow for more characters and formats
in the type URL prefixes of expanded Any protos. This follows a recent
change to the text-format spec which we are now closely following.

Refs:
- [1] https://protobuf.dev/reference/protobuf/textformat-spec/#characters
- [2] https://protobuf.dev/reference/protobuf/textformat-spec/#field-
PiperOrigin-RevId: 844614988
2025-12-14 23:31:47 -08:00
Jie Luo
5b116fe2f1 Breaking change: Raise errors in OSS when assign bool to int/enum field in Python Proto.
https://protobuf.dev/news/2025-09-19/#python-reject-bool-enum-int

PiperOrigin-RevId: 843305319
2025-12-11 11:28:56 -08:00