This is an automated email from the ASF dual-hosted git repository.

xiangfu0 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new c3cf244dd65 Restrict the locations ProtoBufCodeGenMessageDecoder loads 
jar files from (#20378)
c3cf244dd65 is described below

commit c3cf244dd6553e8badde16558ee34688509d7c8c
Author: Xiang Fu <[email protected]>
AuthorDate: Wed Nov 25 23:45:34 2026 -0800

    Restrict the locations ProtoBufCodeGenMessageDecoder loads jar files from 
(#20378)
---
 .../custom/ProtoBufCodeGenMessageDecoderTest.java  |  7 ++
 .../protobuf/ProtoBufCodeGenMessageDecoder.java    | 62 +++++++++++++++++
 .../ProtoBufCodeGenMessageDecoderTest.java         | 79 ++++++++++++++++++++++
 3 files changed, 148 insertions(+)

diff --git 
a/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ProtoBufCodeGenMessageDecoderTest.java
 
b/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ProtoBufCodeGenMessageDecoderTest.java
index c8e6014a35a..dc0ce94c83f 100644
--- 
a/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ProtoBufCodeGenMessageDecoderTest.java
+++ 
b/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/custom/ProtoBufCodeGenMessageDecoderTest.java
@@ -25,6 +25,7 @@ import com.google.protobuf.Descriptors;
 import com.google.protobuf.DynamicMessage;
 import java.io.File;
 import java.io.InputStream;
+import java.net.URI;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
@@ -76,6 +77,12 @@ public class ProtoBufCodeGenMessageDecoderTest extends 
CustomDataQueryClusterInt
   private static final String PROTO_JAR_RESOURCE = "sample-samplerecord.jar";
   private static final String PROTO_MESSAGE_TYPE = "SampleRecord";
 
+  // The servers run in this JVM and only load codegen jars from the allowed 
locations
+  static {
+    
System.setProperty(ProtoBufCodeGenMessageDecoder.ALLOWED_JAR_LOCATIONS_PROPERTY,
+        new File(URI.create(getJarFileUri())).getParent());
+  }
+
   private static final String COL_NAME = "name";
   private static final String COL_ID = "id";
   private static final String COL_EMAIL = "email";
diff --git 
a/pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoder.java
 
b/pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoder.java
index b80977c9f79..0d3350b44fe 100644
--- 
a/pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoder.java
+++ 
b/pinot-plugins/pinot-input-format/pinot-protobuf/src/main/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoder.java
@@ -18,26 +18,42 @@
  */
 package org.apache.pinot.plugin.inputformat.protobuf;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.protobuf.Descriptors;
 import com.google.protobuf.Message;
 import java.io.File;
+import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.net.URI;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.Arrays;
 import java.util.Map;
 import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.pinot.plugin.inputformat.protobuf.codegen.MessageCodeGen;
 import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.filesystem.PinotFSFactory;
 import org.apache.pinot.spi.stream.StreamMessageDecoder;
 import org.codehaus.janino.SimpleCompiler;
 
 
+/// Decodes protobuf messages with an extractor generated and compiled from 
the message class in a jar file.
+///
+/// The jar is loaded into the JVM, so it must come from a location the 
operator of the process has allowed through
+/// the JVM system property [#ALLOWED_JAR_LOCATIONS_PROPERTY]. The decoder 
cannot be used when the property is not
+/// set. The property is deliberately a JVM system property rather than a 
Pinot config, so that it can only be set
+/// by whoever starts the process.
+///
+/// Instances are not thread-safe.
 public class ProtoBufCodeGenMessageDecoder implements 
StreamMessageDecoder<byte[]> {
   public static final String PROTOBUF_JAR_FILE_PATH = "jarFile";
   public static final String PROTO_CLASS_NAME = "protoClassName";
+  /// JVM system property holding the comma-separated locations (directories 
or URI prefixes, e.g.
+  /// `/opt/pinot/protobuf,s3://my-bucket/protobuf`) the jar file may be 
loaded from.
+  public static final String ALLOWED_JAR_LOCATIONS_PROPERTY = 
"pinot.protobuf.codegen.decoder.allowed.jar.locations";
   private Method _decodeMethod;
 
   @Override
@@ -49,6 +65,7 @@ public class ProtoBufCodeGenMessageDecoder implements 
StreamMessageDecoder<byte[
         "Protocol Buffer Message class name must be provided");
     String protoClassName = props.getOrDefault(PROTO_CLASS_NAME, "");
     String jarPath = props.getOrDefault(PROTOBUF_JAR_FILE_PATH, "");
+    checkJarLocationAllowed(jarPath);
     ClassLoader protoMessageClsLoader = loadClass(jarPath);
     Descriptors.Descriptor descriptor = 
getDescriptorForProtoClass(protoMessageClsLoader, protoClassName);
     String codeGenCode = new MessageCodeGen().codegen(descriptor, 
fieldsToRead);
@@ -75,6 +92,51 @@ public class ProtoBufCodeGenMessageDecoder implements 
StreamMessageDecoder<byte[
     return decode(payload, destination);
   }
 
+  /// Fails unless the jar file is under one of the locations in 
[#ALLOWED_JAR_LOCATIONS_PROPERTY].
+  @VisibleForTesting
+  static void checkJarLocationAllowed(String jarPath)
+      throws IOException {
+    String allowedLocations = 
System.getProperty(ALLOWED_JAR_LOCATIONS_PROPERTY);
+    Preconditions.checkState(StringUtils.isNotBlank(allowedLocations),
+        "ProtoBufCodeGenMessageDecoder is disabled. To enable it, set the JVM 
system property: %s to the "
+            + "comma-separated locations the jar file may be loaded from", 
ALLOWED_JAR_LOCATIONS_PROPERTY);
+    String jarLocation = normalizeLocation(jarPath);
+    for (String allowedLocation : StringUtils.split(allowedLocations, ',')) {
+      if (StringUtils.isBlank(allowedLocation)) {
+        continue;
+      }
+      String prefix = normalizeLocation(allowedLocation.trim());
+      if (!prefix.endsWith("/")) {
+        prefix += "/";
+      }
+      if (jarLocation.startsWith(prefix)) {
+        return;
+      }
+    }
+    throw new IllegalStateException(
+        String.format("Protocol Buffer schema jar file: %s is not under any of 
the locations allowed by: %s", jarPath,
+            ALLOWED_JAR_LOCATIONS_PROPERTY));
+  }
+
+  /// Returns the location as a normalized string of scheme, authority and 
decoded path. Local paths are
+  /// canonicalized so that relative segments and symbolic links cannot be 
used to leave an allowed directory; remote
+  /// paths with relative segments are rejected.
+  private static String normalizeLocation(String location)
+      throws IOException {
+    URI uri = URI.create(location);
+    String scheme = uri.getScheme();
+    String path = uri.getPath();
+    Preconditions.checkState(StringUtils.isNotEmpty(path), "Invalid location: 
%s", location);
+    if (scheme == null || scheme.equals(PinotFSFactory.LOCAL_PINOT_FS_SCHEME)) 
{
+      return new File(path).getCanonicalFile().toURI().toString();
+    }
+    for (String segment : StringUtils.split(path, '/')) {
+      Preconditions.checkState(!segment.equals("..") && !segment.equals("."),
+          "Relative path segments are not allowed in location: %s", location);
+    }
+    return scheme + "://" + StringUtils.defaultString(uri.getRawAuthority()) + 
path;
+  }
+
   public static ClassLoader loadClass(String jarFilePath) {
     try {
       File file = ProtoBufUtils.getFileCopiedToLocal(jarFilePath);
diff --git 
a/pinot-plugins/pinot-input-format/pinot-protobuf/src/test/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoderTest.java
 
b/pinot-plugins/pinot-input-format/pinot-protobuf/src/test/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoderTest.java
index 6f7829237f3..c5810c65a51 100644
--- 
a/pinot-plugins/pinot-input-format/pinot-protobuf/src/test/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoderTest.java
+++ 
b/pinot-plugins/pinot-input-format/pinot-protobuf/src/test/java/org/apache/pinot/plugin/inputformat/protobuf/ProtoBufCodeGenMessageDecoderTest.java
@@ -22,23 +22,102 @@ import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Sets;
 import com.google.protobuf.ByteString;
 import com.google.protobuf.Descriptors;
+import java.io.File;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import org.apache.pinot.spi.data.readers.GenericRow;
 import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import static 
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufCodeGenMessageDecoder.ALLOWED_JAR_LOCATIONS_PROPERTY;
 import static 
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufCodeGenMessageDecoder.PROTOBUF_JAR_FILE_PATH;
 import static 
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufCodeGenMessageDecoder.PROTO_CLASS_NAME;
 import static 
org.apache.pinot.plugin.inputformat.protobuf.ProtoBufTestDataGenerator.*;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertThrows;
 
 
 public class ProtoBufCodeGenMessageDecoderTest {
+  private static final String SAMPLE_CLASS = 
"org.apache.pinot.plugin.inputformat.protobuf.Sample$SampleRecord";
+
+  @BeforeMethod
+  public void allowTestResourceJars()
+      throws Exception {
+    System.setProperty(ALLOWED_JAR_LOCATIONS_PROPERTY, 
getResourceDir().getPath());
+  }
+
+  @AfterMethod
+  public void clearAllowedJarLocations() {
+    System.clearProperty(ALLOWED_JAR_LOCATIONS_PROPERTY);
+  }
+
+  @Test
+  public void testDisabledWhenAllowedJarLocationsNotSet() {
+    System.clearProperty(ALLOWED_JAR_LOCATIONS_PROPERTY);
+    assertThrows(IllegalStateException.class,
+        () -> setupDecoder("sample.jar", SAMPLE_CLASS, 
getFieldsInSampleRecord()));
+    System.setProperty(ALLOWED_JAR_LOCATIONS_PROPERTY, " ");
+    assertThrows(IllegalStateException.class,
+        () -> setupDecoder("sample.jar", SAMPLE_CLASS, 
getFieldsInSampleRecord()));
+  }
+
+  @Test
+  public void testJarOutsideAllowedLocationsRejected()
+      throws Exception {
+    File resourceDir = getResourceDir();
+    File allowedDir = Files.createTempDirectory("allowed").toFile();
+    File siblingDir = new File(allowedDir.getPath() + "-sibling");
+    try {
+      System.setProperty(ALLOWED_JAR_LOCATIONS_PROPERTY, allowedDir.getPath() 
+ ", s3://bucket/protobuf");
+      // Not under an allowed location
+      assertThrows(IllegalStateException.class,
+          () -> setupDecoder("sample.jar", SAMPLE_CLASS, 
getFieldsInSampleRecord()));
+      // Relative segments leaving the allowed location
+      assertThrows(IllegalStateException.class, () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed(
+          allowedDir.toURI() + "../" + resourceDir.getName() + "/sample.jar"));
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("s3://bucket/protobuf/../other/sample.jar"));
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("s3://bucket/protobuf/%2e%2e/other/sample.jar"));
+      // Directory sharing the allowed location as a name prefix
+      assertThrows(IllegalStateException.class,
+          () -> ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed(new 
File(siblingDir, "sample.jar").getPath()));
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("s3://bucket/protobuf-other/sample.jar"));
+      // Different bucket or scheme
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("s3://other/protobuf/sample.jar"));
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("gs://bucket/protobuf/sample.jar"));
+      // Symbolic link leaving the allowed location
+      File link = new File(allowedDir, "link.jar");
+      Files.createSymbolicLink(link.toPath(), new File(resourceDir, 
"sample.jar").toPath());
+      assertThrows(IllegalStateException.class,
+          () -> 
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed(link.getPath()));
+
+      // Allowed
+      ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed(new 
File(allowedDir, "sample.jar").getPath());
+      ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed(
+          new File(allowedDir, "nested/sample.jar").toURI().toString());
+      
ProtoBufCodeGenMessageDecoder.checkJarLocationAllowed("s3://bucket/protobuf/nested/sample.jar");
+    } finally {
+      new File(allowedDir, "link.jar").delete();
+      allowedDir.delete();
+    }
+  }
+
+  private File getResourceDir()
+      throws Exception {
+    return new 
File(getClass().getClassLoader().getResource("sample.jar").toURI()).getParentFile();
+  }
+
   @Test
   public void testComplexClass()
       throws Exception {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to