You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by rz...@apache.org on 2026/07/16 13:35:01 UTC
(opennlp) branch allowlist-class-loading created (now 345fac17c)
This is an automated email from the ASF dual-hosted git repository.
rzo1 pushed a change to branch allowlist-class-loading
in repository https://gitbox.apache.org/repos/asf/opennlp.git
at 345fac17c OPENNLP-1890: Using allowlist based class loading in StringInterners
This branch includes the following new commits:
new 02d5ea6dd OPENNLP-1890: Using allowlist based class loading in StreamFactoryRegistry
new 345fac17c OPENNLP-1890: Using allowlist based class loading in StringInterners
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(opennlp) 02/02: OPENNLP-1890: Using allowlist based class loading in StringInterners
Posted by rz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rzo1 pushed a commit to branch allowlist-class-loading
in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit 345fac17cab3c913a8fceb29334bbd81514dc6e4
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Thu Jul 16 15:31:54 2026 +0200
OPENNLP-1890: Using allowlist based class loading in StringInterners
---
.../tools/util/jvm/CHMStringDeduplicator.java | 2 +-
.../opennlp/tools/util/jvm/CHMStringInterner.java | 2 +-
.../opennlp/tools/util/jvm/HMStringInterner.java | 2 +-
.../opennlp/tools/util/jvm/JvmStringInterner.java | 2 +-
.../opennlp/tools/util/jvm/NoOpStringInterner.java | 2 +-
.../opennlp/tools/util/jvm/StringInterners.java | 12 ++--
.../tools/util/jvm/StringInternersTest.java | 74 ++++++++++++++++++++++
7 files changed, 86 insertions(+), 10 deletions(-)
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringDeduplicator.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringDeduplicator.java
index d8a0a50d9..bf0148976 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringDeduplicator.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringDeduplicator.java
@@ -35,7 +35,7 @@ import opennlp.tools.commons.ThreadSafe;
*/
@Internal
@ThreadSafe
-class CHMStringDeduplicator implements StringInterner {
+public class CHMStringDeduplicator implements StringInterner {
private final int prob;
private final Map<String, String> map;
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringInterner.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringInterner.java
index 597cae26a..812c343f1 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringInterner.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/CHMStringInterner.java
@@ -32,7 +32,7 @@ import opennlp.tools.commons.ThreadSafe;
*/
@Internal
@ThreadSafe
-class CHMStringInterner implements StringInterner {
+public class CHMStringInterner implements StringInterner {
private final Map<String, String> map;
public CHMStringInterner() {
map = new ConcurrentHashMap<>();
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/HMStringInterner.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/HMStringInterner.java
index 2cef86ff8..84f816863 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/HMStringInterner.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/HMStringInterner.java
@@ -30,7 +30,7 @@ import opennlp.tools.commons.Internal;
* https://shipilev.net/jvm/anatomy-quarks/10-string-intern/</a>
*/
@Internal
-class HMStringInterner implements StringInterner {
+public class HMStringInterner implements StringInterner {
private final Map<String, String> map;
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/JvmStringInterner.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/JvmStringInterner.java
index 26567b4d0..a6b040f3c 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/JvmStringInterner.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/JvmStringInterner.java
@@ -31,7 +31,7 @@ import opennlp.tools.commons.ThreadSafe;
*/
@Internal
@ThreadSafe
-class JvmStringInterner implements StringInterner {
+public class JvmStringInterner implements StringInterner {
/**
* {@inheritDoc}
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/NoOpStringInterner.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/NoOpStringInterner.java
index 8398191c8..1bb8111a6 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/NoOpStringInterner.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/NoOpStringInterner.java
@@ -25,7 +25,7 @@ import opennlp.tools.commons.ThreadSafe;
*/
@Internal
@ThreadSafe
-class NoOpStringInterner implements StringInterner {
+public class NoOpStringInterner implements StringInterner {
/**
* {@inheritDoc}
diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/StringInterners.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/StringInterners.java
index 295def2d6..d65c99b8c 100644
--- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/StringInterners.java
+++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/util/jvm/StringInterners.java
@@ -17,17 +17,21 @@
package opennlp.tools.util.jvm;
-import java.lang.reflect.Constructor;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import opennlp.tools.util.ext.ExtensionLoader;
+
/**
* Provides string interning utility methods. Interning mechanism can be configured via the
* system property {@code opennlp.interner.class} by specifying an implementation via its
* fully qualified classname. It needs to implement {@link StringInterner}.
* <p>
* If not specified by the user, the default interner is {@link CHMStringInterner}.
+ * <p>
+ * The implementation is loaded via the {@link ExtensionLoader}. Custom implementations
+ * must provide a public no-arg constructor and reside in an allowed package,
+ * see {@link ExtensionLoader#registerAllowedPackage(String)}.
*/
public class StringInterners {
@@ -40,9 +44,7 @@ public class StringInterners {
CHMStringInterner.class.getCanonicalName());
try {
- final Class<?> clazz = Class.forName(clazzName);
- final Constructor<?> cons = clazz.getDeclaredConstructor();
- INTERNER = (StringInterner) cons.newInstance();
+ INTERNER = ExtensionLoader.instantiateExtension(StringInterner.class, clazzName);
LOGGER.debug("Using '{}' as String interner implementation.", clazzName);
} catch (Exception e) {
throw new RuntimeException("Could not load specified String interner implementation: '"
diff --git a/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/util/jvm/StringInternersTest.java b/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/util/jvm/StringInternersTest.java
new file mode 100644
index 000000000..0522ebf11
--- /dev/null
+++ b/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/util/jvm/StringInternersTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.util.jvm;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import opennlp.tools.util.ext.ExtensionLoader;
+import opennlp.tools.util.ext.ExtensionNotLoadedException;
+
+public class StringInternersTest {
+
+ @Test
+ void testIntern() {
+ final String interned = StringInterners.intern("opennlp");
+ Assertions.assertSame(interned, StringInterners.intern(new String("opennlp".toCharArray())));
+ }
+
+ /**
+ * Positive: all built-in interner implementations can be loaded via the
+ * ExtensionLoader, as done by {@link StringInterners} when configured through
+ * the {@code opennlp.interner.class} system property.
+ */
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "opennlp.tools.util.jvm.CHMStringInterner",
+ "opennlp.tools.util.jvm.CHMStringDeduplicator",
+ "opennlp.tools.util.jvm.HMStringInterner",
+ "opennlp.tools.util.jvm.JvmStringInterner",
+ "opennlp.tools.util.jvm.NoOpStringInterner"})
+ void testBuiltInInternersLoadable(String clazzName) {
+ final StringInterner interner =
+ ExtensionLoader.instantiateExtension(StringInterner.class, clazzName);
+ Assertions.assertNotNull(interner);
+ }
+
+ /**
+ * Negative: an interner class in a non-allowed package is rejected by
+ * the ExtensionLoader allowlist before Class.forName() is called.
+ */
+ @Test
+ void testInternerFromBlockedPackageThrows() {
+ Assertions.assertThrows(ExtensionNotLoadedException.class, () ->
+ ExtensionLoader.instantiateExtension(StringInterner.class, "com.malicious.Interner"));
+ }
+
+ /**
+ * Negative: an opennlp.* class that does NOT implement StringInterner is
+ * rejected by the isAssignableFrom type check.
+ */
+ @Test
+ void testInternerOfWrongTypeThrows() {
+ Assertions.assertThrows(ExtensionNotLoadedException.class, () ->
+ ExtensionLoader.instantiateExtension(StringInterner.class,
+ "opennlp.tools.util.jvm.StringInterners"));
+ }
+}
(opennlp) 01/02: OPENNLP-1890: Using allowlist based class loading in StreamFactoryRegistry
Posted by rz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rzo1 pushed a commit to branch allowlist-class-loading
in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit 02d5ea6dde2810c3098b15d50e65ed98fee58141
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Thu Jul 16 15:30:00 2026 +0200
OPENNLP-1890: Using allowlist based class loading in StreamFactoryRegistry
---
.../tools/cmdline/StreamFactoryRegistry.java | 27 ++++---
.../tools/cmdline/StreamFactoryRegistryTest.java | 82 ++++++++++++++++++++++
2 files changed, 94 insertions(+), 15 deletions(-)
diff --git a/opennlp-core/opennlp-formats/src/main/java/opennlp/tools/cmdline/StreamFactoryRegistry.java b/opennlp-core/opennlp-formats/src/main/java/opennlp/tools/cmdline/StreamFactoryRegistry.java
index 09cfef02a..14972980b 100644
--- a/opennlp-core/opennlp-formats/src/main/java/opennlp/tools/cmdline/StreamFactoryRegistry.java
+++ b/opennlp-core/opennlp-formats/src/main/java/opennlp/tools/cmdline/StreamFactoryRegistry.java
@@ -17,7 +17,6 @@
package opennlp.tools.cmdline;
-import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
@@ -72,6 +71,8 @@ import opennlp.tools.formats.nkjp.NKJPSentenceSampleStreamFactory;
import opennlp.tools.formats.ontonotes.OntoNotesNameSampleStreamFactory;
import opennlp.tools.formats.ontonotes.OntoNotesPOSSampleStreamFactory;
import opennlp.tools.formats.ontonotes.OntoNotesParseSampleStreamFactory;
+import opennlp.tools.util.ext.ExtensionLoader;
+import opennlp.tools.util.ext.ExtensionNotLoadedException;
/**
* Registry for {@link ObjectStreamFactory object stream factories}.
@@ -208,10 +209,16 @@ public final class StreamFactoryRegistry {
/**
* Returns a factory which reads format named {@code formatName} and
* instantiates streams producing objects of {@code sampleClass} class.
+ * <p>
+ * If no factory is registered for {@code formatName}, an attempt is made to
+ * interpret {@code formatName} as the fully qualified class name of an
+ * {@link ObjectStreamFactory} implementation and load it via the
+ * {@link ExtensionLoader}. Such a class must reside in an allowed package,
+ * see {@link ExtensionLoader#registerAllowedPackage(String)}.
*
* @param sampleClass class of the objects, produced by the streams instantiated by the factory
* @param formatName name of the format, if null, assumes OpenNLP format
- * @return factory instance
+ * @return factory instance, or {@code null} if no factory could be found or loaded
*/
@SuppressWarnings("unchecked")
public static <T,P> ObjectStreamFactory<T,P> getFactory(Class<T> sampleClass, String formatName) {
@@ -227,19 +234,9 @@ public final class StreamFactoryRegistry {
}
else {
try {
- Class<?> factoryClazz = Class.forName(formatName);
-
- // TODO: Need to check if it can produce the desired output
- // Otherwise there will be class cast exceptions later in the flow
-
- try {
- return (ObjectStreamFactory<T,P>) factoryClazz.getDeclaredConstructor().newInstance();
- } catch (InstantiationException | NoSuchMethodException |
- InvocationTargetException | IllegalAccessException e) {
- return null;
- }
-
- } catch (ClassNotFoundException e) {
+ return (ObjectStreamFactory<T,P>)
+ ExtensionLoader.instantiateExtension(ObjectStreamFactory.class, formatName);
+ } catch (ExtensionNotLoadedException e) {
return null;
}
}
diff --git a/opennlp-core/opennlp-formats/src/test/java/opennlp/tools/cmdline/StreamFactoryRegistryTest.java b/opennlp-core/opennlp-formats/src/test/java/opennlp/tools/cmdline/StreamFactoryRegistryTest.java
new file mode 100644
index 000000000..839b4f6b7
--- /dev/null
+++ b/opennlp-core/opennlp-formats/src/test/java/opennlp/tools/cmdline/StreamFactoryRegistryTest.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.cmdline;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import opennlp.tools.tokenize.TokenSample;
+import opennlp.tools.util.ObjectStream;
+
+public class StreamFactoryRegistryTest {
+
+ public static class DummyFactory implements ObjectStreamFactory<String, Object> {
+
+ public DummyFactory() {
+ }
+
+ @Override
+ public Class<Object> getParameters() {
+ return Object.class;
+ }
+
+ @Override
+ public ObjectStream<String> create(String[] args) {
+ return null;
+ }
+ }
+
+ @Test
+ void testGetRegisteredFactory() {
+ ObjectStreamFactory<TokenSample, ?> factory =
+ StreamFactoryRegistry.getFactory(TokenSample.class, StreamFactoryRegistry.DEFAULT_FORMAT);
+ Assertions.assertNotNull(factory);
+ }
+
+ /**
+ * Positive: an unregistered format name referencing an opennlp.* factory class
+ * is loaded via the ExtensionLoader. opennlp.* is in the default allowlist and
+ * the type matches ObjectStreamFactory.
+ */
+ @Test
+ void testGetFactoryViaClassNameAllowedOpennlpPackage() {
+ ObjectStreamFactory<String, Object> factory =
+ StreamFactoryRegistry.getFactory(String.class, DummyFactory.class.getName());
+ Assertions.assertNotNull(factory);
+ Assertions.assertInstanceOf(DummyFactory.class, factory);
+ }
+
+ /**
+ * Negative: a format name referencing a non-opennlp.* class is rejected by
+ * the ExtensionLoader allowlist before Class.forName() is called.
+ */
+ @Test
+ void testGetFactoryViaClassNameBlockedPackageReturnsNull() {
+ Assertions.assertNull(StreamFactoryRegistry.getFactory(String.class, "com.malicious.Exploit"));
+ }
+
+ /**
+ * Negative: a format name referencing an opennlp.* class that does NOT implement
+ * ObjectStreamFactory is rejected by the isAssignableFrom type check.
+ */
+ @Test
+ void testGetFactoryViaClassNameWrongTypeReturnsNull() {
+ Assertions.assertNull(StreamFactoryRegistry.getFactory(String.class,
+ "opennlp.tools.tokenize.SimpleTokenizer"));
+ }
+}