From fedcd62136ee291ef4acc82e32e84e287e44419a Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 1 Dec 2023 09:01:02 -0800 Subject: [PATCH] Prepare methods in RuntimeVersion for Protobuf Java cross-domain version validation. No changes to Protobuf Java gencode yet. PiperOrigin-RevId: 587025016 --- .../com/google/protobuf/RuntimeVersion.java | 37 +++++++++++++++---- .../google/protobuf/RuntimeVersionTest.java | 26 ++++++++++--- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java b/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java index a0bc325d20..815bb9ed18 100644 --- a/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java +++ b/java/core/src/main/java/com/google/protobuf/RuntimeVersion.java @@ -7,6 +7,8 @@ package com.google.protobuf; +import java.util.logging.Logger; + /** * Provides the version of this Protobuf Java runtime, and methods for Protobuf Java gencode to * validate that versions are compatible. Fields and methods in this class should be only accessed @@ -29,6 +31,31 @@ public final class RuntimeVersion { public static final String SUFFIX = "-dev"; private static final String VERSION_STRING = versionString(MAJOR, MINOR, PATCH, SUFFIX); + /** + * Validates that the gencode is in the same domain as the runtime. + * + *

This method will be directly called by the google-internal gencode to verify no cross-domain + * usages. + * + * @param gencodeDomain the domain where Protobuf Java code was generated. + * @throws ProtobufRuntimeVersionException if gencodeDomain is not the same as DOMAIN. + */ + public static void validateProtobufGencodeDomain(RuntimeDomain gencodeDomain) { + // Check the environmental variable, and temporarily disable validation if it's set to true. + String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK"); + if ((disableFlag != null && disableFlag.equals("true"))) { + return; + } + + if (gencodeDomain != DOMAIN) { + throw new ProtobufRuntimeVersionException( + String.format( + "Mismatched Protobuf Gencode/Runtime domains: gencode %s, runtime %s. Cross-domain" + + " usage of Protobuf is not supported.", + gencodeDomain, DOMAIN)); + } + } + /** * Validates that the gencode version is compatible with this runtime version according to * https://protobuf.dev/support/cross-version-runtime-guarantee/. @@ -37,7 +64,7 @@ public final class RuntimeVersion { * *

This method is only for Protobuf Java gencode; do not call it elsewhere. * - * @param domain the domain where Protobuf Java code was generated. Currently ignored. + * @param domain the domain where Protobuf Java code was generated. * @param major the major version of Protobuf Java gencode. * @param minor the minor version of Protobuf Java gencode. * @param patch the micro/patch version of Protobuf Java gencode. @@ -47,18 +74,14 @@ public final class RuntimeVersion { public static void validateProtobufGencodeVersion( RuntimeDomain domain, int major, int minor, int patch, String suffix) { - // Check the environmental variable, and temporarily disable poison pills if it's set to true. - String disableFlag = java.lang.System.getenv("TEMORARILY_DISABLE_PROTOBUF_VERSION_CHECK"); - if (disableFlag != null && disableFlag.equals("true")) { - return; - } - // Check that version numbers are valid. if (major < 0 || minor < 0 || patch < 0) { throw new ProtobufRuntimeVersionException( "Invalid gencode version: " + versionString(major, minor, patch, suffix)); } + validateProtobufGencodeDomain(domain); + String gencodeVersionString = versionString(major, minor, patch, suffix); // Check that runtime major version is the same as the gencode major version. if (major != MAJOR) { diff --git a/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java b/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java index 53b6757c15..c30c9a6b7f 100644 --- a/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java +++ b/java/core/src/test/java/com/google/protobuf/RuntimeVersionTest.java @@ -24,12 +24,26 @@ public final class RuntimeVersionTest { RuntimeVersion.ProtobufRuntimeVersionException.class, () -> RuntimeVersion.validateProtobufGencodeVersion( - RuntimeVersion.DOMAIN, - -1, - RuntimeVersion.MINOR, - RuntimeVersion.PATCH, - RuntimeVersion.SUFFIX)); - assertThat(thrown).hasMessageThat().contains("Invalid gencode version: -1"); + RuntimeVersion.DOMAIN, 1, -2, -3, "")); + assertThat(thrown).hasMessageThat().contains("Invalid gencode version: 1.-2.-3"); + } + + @Test + public void versionValidation_crossDomainDisallowed() { + + RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.GOOGLE_INTERNAL; + RuntimeVersion.ProtobufRuntimeVersionException thrown = + assertThrows( + RuntimeVersion.ProtobufRuntimeVersionException.class, + () -> RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain)); + assertThat(thrown).hasMessageThat().contains("Mismatched Protobuf Gencode/Runtime domains"); + } + + @Test + public void versionValidation_sameDomainAllowed() { + + RuntimeVersion.RuntimeDomain gencodeDomain = RuntimeVersion.RuntimeDomain.PUBLIC; + RuntimeVersion.validateProtobufGencodeDomain(gencodeDomain); } @Test