Commit graph

370 commits

Author SHA1 Message Date
Protobuf Team Bot
ae1faeedfd Use natural String comparison instead of UTF-8 ByteString comparison for sorting map keys in JsonFormat.
This only changes the sort order in an obscure edge case of map-keys which sort differently in Utf8 and Utf16, meaning a mix of certain characters that are "very high code point but still single char" and "utf16 surrogate pairs".

PiperOrigin-RevId: 922768203
2026-05-28 07:45:14 -07:00
Protobuf Team Bot
61dfcbc7cf Optimize JsonFormat by handling the newline pretty printing better.
PiperOrigin-RevId: 922127998
2026-05-27 07:56:12 -07:00
Protobuf Team Bot
bca685147b Optimize JavaProto JSON string escaping handling.
PiperOrigin-RevId: 921615825
2026-05-26 12:27:10 -07:00
Protobuf Team Bot
40825a1df7 Add option to enable strict JSON parsing in Protobuf Java JsonFormat.
JsonFormat used 'reader.setLenient(false)' to try to get spec JSON behavior out of GSON. However, this mode was still not actually spec JSON. In ~2024 GSON deprecated setLenient(), made that behavior LEGACY_STRICT and added a new level STRICT which is intended to be spec.

Switching unilaterally would be a breaking change for Protobuf: this change just adds the option for users to ask us to use GSON in strict mode. At a later date we will consider flipping the default of this (letting people opt into LEGACY_STRICT if they want to).

PiperOrigin-RevId: 921432331
2026-05-26 06:39:41 -07:00
Mindflare
25e7005814 java: limit numeric string length before BigDecimal parsing (#26908)
## Summary

`BigDecimal(String)` has O(N²) time complexity for N-digit strings on JDK versions before 18 ([JDK-8291514](https://bugs.openjdk.org/browse/JDK-8291514)). Five JSON parser methods — `parseInt32`, `parseInt64`, `parseUint32`, `parseUint64`, and `parseDouble` — pass user-controlled strings directly to `new BigDecimal()` without length validation.

A single JSON numeric value with 1,000,000 digits takes ~13 seconds to parse on JDK 17. This can be used to DoS any service that parses protobuf JSON messages with numeric fields from untrusted input.

### Benchmark (JDK 17, x86-64 Linux)

| Digits | BigDecimal construction time |
|--------|-----|
| 1,000 | 1.8 ms |
| 10,000 | 6.3 ms |
| 100,000 | 133 ms |
| 1,000,000 | **13.1 seconds** |

### Fix

Added a `parseBigDecimal()` helper that rejects strings longer than 1000 characters before constructing `BigDecimal`. This is generous — valid protobuf numeric values never exceed ~350 characters (Double.MAX_VALUE in non-scientific notation is ~309 digits).

### Affected JDK versions

- JDK 8, 11, 17 (all current LTS releases): **Vulnerable** — no built-in string length limit in BigDecimal
- JDK 18+: JDK itself limits BigDecimal string input to 1100 characters by default (JDK-8291514), but the protobuf-level check is still worthwhile as defense-in-depth

### Test

Added `testParserRejectOverlyLongNumericStrings` covering all 5 affected field types.

Closes #26908

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/26908 from MindflareX:fix/java-bigdecimal-length-check a461d0edc6
PiperOrigin-RevId: 904988136
2026-04-24 06:12:50 -07:00
Protobuf Team Bot
34cb33b197 Improve FieldMaskTree behavior in the face of extremely 'long' field paths (ones with a large number of periods)
PiperOrigin-RevId: 898950610
2026-04-13 06:00:20 -07:00
Protobuf Team Bot
94c68efaaa This change does not affect OSS
PiperOrigin-RevId: 876331033
2026-02-27 10:58:09 -08:00
Protobuf Team Bot
365bde3e5f Introduce the ability to print extensions within Any proto by passing an ExtensionRegistry to the Printer.
This method is held back from the public API for now.

PiperOrigin-RevId: 875795864
2026-02-26 10:59:01 -08:00
Protobuf Team Bot
b843769d72 Introduce the ability to print fully qualified extension names and allow passing an ExtensionRegistry to the Parser.
This fixes as issue where extension names were incorrectly printed using their short name which prevents parsing and can lead to ambiguous or duplicate json keys.

This feature is gated by a boolean options printingFullyQualifiedExtensionNames and printingShortExtensionNames for testing. These methods are held back from the public API for now.

PiperOrigin-RevId: 875322649
2026-02-25 13:56:32 -08:00
Protobuf Team Bot
57093a8bd5 Avoid toBigIntegerExact in JsonFormat to avoid degenerate parse behavior in the face of large exponents.
https://github.com/protocolbuffers/protobuf/issues/26032

PiperOrigin-RevId: 874768607
2026-02-24 13:41:47 -08:00
Protobuf Team Bot
bbcc021add Add tests for JsonFormat extension printing.
This change adds base tests case for printing proto2 extensions. It highlights a known issue with parsing extensions as short names and duplicated field names.

PiperOrigin-RevId: 873022900
2026-02-20 12:23:08 -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
Sandy Zhang
7beda33399 Clean up dead dist_files targets.
PiperOrigin-RevId: 851412528
2026-01-02 12:19:11 -08:00
Protobuf Team Bot
33b16e8302 Correctly apply JSON recursion limit when parsing an Any-of-Any.
Without this check, an any-of-any-of-any-of-... wouldn't apply the recursion check as intended and cound be arbitrarily deep.

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

PiperOrigin-RevId: 850485466
2025-12-30 12:41:45 -08:00
Protobuf Team Bot
c224a3606f Use Locale.ROOT for all of our formatted error messages.
PiperOrigin-RevId: 846398642
2025-12-18 13:45:47 -08:00
Patrice Arruda
f66ad439e1 refactor(protobuf): remove redundant typecast in Timestamps.normalizedTimestamp (#24671)
Removed unnecessary typecast in the normalizedTimestamp method.

Modified: java/util/src/main/java/com/google/protobuf/util/Timestamps.java

Closes #24671

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/24671 from patricearruda84:remove_redundant_check 6491986c2f
PiperOrigin-RevId: 839898550
2025-12-03 13:47:35 -08:00
Angie Lin
1270e456f4 Fix quote escaping in JsonFormat.print() for FieldMasks.
https://github.com/protocolbuffers/protobuf/issues/24083

PiperOrigin-RevId: 824592626
2025-10-27 11:06:28 -07:00
Protobuf Team Bot
a2b034a9b1 Update to use the forward-looking proper noun "ProtoJSON" to refer to the format which was formerly known as "Proto3 JSON"
PiperOrigin-RevId: 823566944
2025-10-24 09:54:02 -07:00
Protobuf Team Bot
c518f2564e Add Values.of(Map<String, Value> values).
Also update Javadoc to include {@link} syntax and fix typo.

PiperOrigin-RevId: 806318745
2025-09-12 09:43:30 -07:00
Protobuf Team Bot
1932ccbd74 Cut guava dep from the Java Protobuf-Util package.
In a subsequent change we should be able to cut the Guava dep from our Maven package.

https://github.com/protocolbuffers/protobuf/issues/21173

PiperOrigin-RevId: 803086278
2025-09-04 10:40:18 -07:00
Protobuf Team Bot
5768acddf2 Remove protobuf-util usages of guava except annotations.
The annotations will be handled in a subsequent change.

The change to use java.lang.String.split() instead of Guava's Splitter does introduce behavior changes in the case of handling of degenerate/malformed FieldMask paths (paths like `a///`). The handling of malformed paths shape are already arbitrary and inconsistent between the different methods in this file, and callers should not construct such paths. In a future change we may consider validating and throwing an IllegalArgumentException on such malformed paths more explicitly.

https://github.com/protocolbuffers/protobuf/issues/21173

PiperOrigin-RevId: 795126095
2025-08-14 11:34:28 -07:00
Kurt Alfred Kluever
1ae6b6bb9c Validate Duration instances produced by the static factories.
PiperOrigin-RevId: 792163897
2025-08-07 08:11:28 -07:00
Kurt Alfred Kluever
a6dcb2a0e0 Rollback parts of cl/786407535, which accidentally had a behavior change --- it added validity checks to some Duration static factories.
PiperOrigin-RevId: 786681101
2025-07-24 06:34:04 -07:00
Kurt Alfred Kluever
c5b6f75ef0 Inline calls to Guava's Int/LongMath.checkedFoo() (use Math.fooExact() instead).
PiperOrigin-RevId: 786407535
2025-07-23 13:42:32 -07:00
Protobuf Team Bot
1999135e8e Rename maven install back to maven in MODULE.bazel
Fixes https://github.com/protocolbuffers/protobuf/issues/21177

This PR essentially reverts https://github.com/protocolbuffers/protobuf/pull/18641, which claimed

> Since protobuf is not contributing to user's JARs

This is not true since targets like `@com_google_protobuf//:protobuf_java` are meant to be consumed by other projects, therefore protobuf should not use a private maven install namespace. Otherwise, it leads to duplicated maven jars and classpath conflicts. See https://github.com/protocolbuffers/protobuf/issues/21177 and https://github.com/bazel-contrib/rules_jvm_external/issues/916#issuecomment-3045506487

The original warning message caused by multiple modules contributing to `maven` can be suppressed with https://github.com/bazel-contrib/rules_jvm_external/pull/1393, which will be available in rules_jvm_external 6.8.

Closes https://github.com/protocolbuffers/protobuf/pull/22544

PiperOrigin-RevId: 782032198
2025-07-11 10:47:35 -07:00
Protobuf Team Bot
84c5b6314a Add a FieldMaskUtil#trim overload that accepts TrimOptions and allows retaining unset primitive field state.
Existing FieldMaskUtil#trim method behavior remains the same: unset primitive fields specified in the field mask will be explicitly set to their default values after trimming.

PiperOrigin-RevId: 754114689
2025-05-02 13:31:38 -07:00
Protobuf Team Bot
74e9800455 Remove erroneous claim that JsonFormat only supports Proto3 on the JavaDoc of the JsonFormat entry point.
PiperOrigin-RevId: 753603078
2025-05-01 08:40:10 -07:00
Liam Miller-Cushon
fd33160979 Fix an eagerly evaluated preconditions string
This avoids calling `String.format` eagerly, and only formats the error message if the preconditions check fails.

PiperOrigin-RevId: 746652266
2025-04-11 18:22:43 -07:00
Sandy Zhang
f4713e28c6 Add protobuf_maven artifacts to protobuf_maven_dev as well so they can still be referenced correctly using the dev namespace for dev-only targets.
Otherwise, protobuf_maven_dev may provide a different version from a transitive dep, which can result in the wrong version being selected (e.g. when used for bazel-generated maven artifacts).

Also updates protobuf_util_bundle to use protobuf_maven instead of protobuf_maven_dev which is probably more appropriate since its control the deps in the pom.xml file for maven users (though this is functionally the same rn).

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

PiperOrigin-RevId: 738887165
2025-03-20 11:57:29 -07:00
Sandy Zhang
4c7b7a971c Restore custom protobuf maven namespaces to avoid polluting main maven namespace for non-dev dependencies as well.
Fixes #20674

PiperOrigin-RevId: 738081837
2025-03-18 11:31:17 -07:00
Protobuf Team Bot
1df3ddea58 Automated rollback of commit 1d4b6bac6e.
PiperOrigin-RevId: 735780482
2025-03-11 09:22:22 -07:00
Stefan Haustein
7a5905db8e Add a time-j2kt-native target.
PiperOrigin-RevId: 734154761
2025-03-06 08:33:24 -08:00
Mike Kruskal
e045fc60cf Split maven dependencies into dev vs local (#20549)
Closes #20549

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/20549 from protocolbuffers:maven-split a8b30b4f96
PiperOrigin-RevId: 733361548
2025-03-04 09:44:39 -08:00
Protobuf Team Bot
28de0ebcdd Run buildifier
Adds missing loads for Java rules.
Remove unused loads.
Some other minor fixes.

PiperOrigin-RevId: 728701378
2025-02-19 09:27:02 -08:00
Éamonn McManus
aab6b1f7ba Use toUnsignedString() to convert uint32 and uint64 to strings.
This method has existed since Java 8, released in 2014.

Also adjust the tests so that they verify unsigned integer values that are too big for the signed counterpart (for example, 4294967295 which fits in `uint32` but not `int32`).

We don't use the parallel `Long.parseUnsignedLong` because the existing code handles inputs like `"1.0"`.

PiperOrigin-RevId: 723945649
2025-02-06 08:33:23 -08:00
Adam Cozzette
0d42a969e3 Replace protobuf_unittest package with proto2_unittest
For historical reasons, our internal copy of the codebase uses
`proto2_unittest` while the open source one uses `protobuf_unittest`. This adds
a lot of friction to syncing between the two, and there's no great reason to
keep maintaining this difference, so let's just go with `proto2_unittest`.

PiperOrigin-RevId: 721951543
2025-01-31 17:06:33 -08:00
Protobuf Team Bot
1d4b6bac6e Internal
PiperOrigin-RevId: 718943413
2025-01-23 11:22:51 -08:00
Protobuf Team Bot
c8ad774756 Fix JRuby conformance issue by loosening Java's JSONFormatter implementation to be more accepting of DynamicMessage instances of well-known types that have a descriptor with a different object identity but the same full name.
PiperOrigin-RevId: 714314780
2025-01-10 20:00:47 -08:00
Mike Kruskal
6a672208d6 Internal changes
PiperOrigin-RevId: 713388656
2025-01-08 13:04:39 -08:00
Kurt Alfred Kluever
0e6a310156 Add a unit test demonstrating broken Timestamps.parse() behavior.
PiperOrigin-RevId: 698179372
2024-11-19 16:18:34 -08:00
Protobuf Team Bot
afb20af442 Add j2cl and j2objc targets for protobuf/current/javatests/com/google/protobuf/util TIME_SRCS
Because not everything is supported in j2cl and j2objc some of the methods had to be marked as incompatible (reflection , String.format...)

PiperOrigin-RevId: 691368238
2024-10-30 04:33:22 -07:00
Deanna Garcia
f6cddd2cd7 Remove deprecation warnings for Timestamp and Duration add/subtract/between that we do not yet have alternatives to.
Fixes https://github.com/protocolbuffers/protobuf/issues/18925

PiperOrigin-RevId: 691117251
2024-10-29 12:35:15 -07:00
Sandy Zhang
ee5aa499af Fix unexpected NumberFormatException in Durations.parse() by replacing with documented thrown ParseException.
This fixes parsing of invalid second value long to throw the correct exception. Most users should already be handling ParseExceptions e.g. for invalid nanos

PiperOrigin-RevId: 686281774
2024-10-15 16:43:30 -07:00
Joe Schafer
44660b306a Rename maven to protobuf_maven in MODULE.bazel (#18641)
The default name for the rules_jvm_external.maven rule is "maven". When not set, it defaults to "maven". For root modules also using rules_jvm_external, the name clash causes a warning:

```
DEBUG: $TMP/external/rules_jvm_external~/private/extensions/maven.bzl:154:14:
The maven repository 'maven' is used in two different bazel modules,
originally in '<my_workspace>' and now in 'protobuf'
```

Summarizing @shs96c in [1]:

> The common maven repo name allows rulesets to contribute to the user's JARs.
> However, this implies that maven is for the end user, not for transitive
> dependencies. If a ruleset needs private dependencies, it should use a custom
> namespace rather than the maven namespace.

Since protobuf is not contributing to user's JARs, we'll use a custom namespace. There's precedent for using a custom namespace for library modules:

- rules_jvm_external uses `rules_jvm_external_deps` instead of `maven`.
- rules_kotlin uses `kotlin_rules_maven` instead of `maven`.

[1]: https://github.com/bazel-contrib/rules_jvm_external/issues/916#issuecomment-1645527584

Fixes #16839.

Closes #18641

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/18641 from jschaf:joe/protobuf-maven bd2c62f311
PiperOrigin-RevId: 684625084
2024-10-10 17:02:43 -07:00
Protobuf Team Bot
0d7fd35086 Re-export includingDefaultValueFields in deprecated state for important Cloud customer.
PiperOrigin-RevId: 684555053
2024-10-10 13:28:38 -07:00
Anuraag (Rag) Agrawal
461004e82d java: remove legacy note in docstring about calling includingDefaultValueFields with no args (#18569)
There is no no-args version of the method anymore.

Closes #18569

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/18569 from anuraaga:patch-2 2221ef5ae0
PiperOrigin-RevId: 684079433
2024-10-09 15:43:31 -07:00
Protobuf Team Bot
4fbb0c5eed Return constant Value objects for true, false, and ""
These instances are all identical.

PiperOrigin-RevId: 678755099
2024-09-25 10:34:13 -07:00
Nathan Baulch
e4cbc79ab4 Fix minor typos (#17682)
Just thought I'd contribute some typo fixes that keep tripping up CI/CD checks in my projects. Nothing controversial (hopefully), just 174 simple fixes.

Use the following command to get a quick and dirty summary of the specific corrections made:
```shell
git diff HEAD^! --word-diff-regex='\w+' -U0 \
  | grep -E '\[\-.*\-\]\{\+.*\+\}' \
  | sed -r 's/.*\[\-(.*)\-\]\{\+(.*)\+\}.*/\1 \2/' \
  | sort | uniq -c | sort -n
```

FWIW, the top typos are:
* trimed → trimmed (37)
* substract → subtract (7)
* qualifed → qualified (7)
* extesion → extension (6)
* mising → missing (5)
* btye → byte (4)
* likey → likely (4)
* candicate → candidate (3)
* decriptor → descriptor (3)
* inherting → inheriting (3)
* colletion → collection (3)
* caluclated → calculated (3)
* unititialized → uninitialized (3)
* implemting → implementing (3)
* binrary → binary (3)
* descripor → descriptor (3)
* negitive → negative (3)

Closes #17682

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17682 from NathanBaulch:typos d41762d137
PiperOrigin-RevId: 677074418
2024-09-20 20:50:06 -07:00
Deanna Garcia
f5ffaf9063 Remove unused pom.xml files.
We mostly use generated pom files in our release currently, so we can delete all the files that aren't used and the tools to update them.

Note, java/bom/pom.xml java/pom.xml and java/protoc/pom.xml are all still used at release and java/kotlin/pom.xml is used for documentation so all of those need to stay for now.

PiperOrigin-RevId: 659664012
2024-08-05 13:41:00 -07:00