subbudvk commented on code in PR #1021:
URL: https://github.com/apache/opennlp/pull/1021#discussion_r3143411023


##########
opennlp-api/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java:
##########
@@ -19,22 +19,90 @@
 
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import opennlp.tools.commons.Internal;
 
 /**
  * The {@link ExtensionLoader} is responsible to load extensions to the 
OpenNLP library.
  * <p>
+ * Only classes whose fully-qualified name starts with a registered package 
prefix are
+ * permitted. The default allowed prefix is {@code opennlp.}, which covers all 
built-in
+ * factories and serializers.
+ * <p>
+ * To allow custom extension classes from other packages, either:
+ * <ul>
+ *   <li>Call {@link #registerAllowedPackage(String)} programmatically before 
loading
+ *       any model that uses the custom class.</li>
+ *   <li>Set the system property {@code OPENNLP_EXT_ALLOWED_PACKAGES} to a
+ *       comma-separated list of package prefixes at JVM startup, e.g.
+ *       {@code -DOPENNLP_EXT_ALLOWED_PACKAGES=com.acme.nlp.,com.other.}.</li>
+ * </ul>
+ * <p>
  * <b>Note:</b>
  * Do not use this class, internal use only!
  */
 @Internal
 public class ExtensionLoader {
 
+  /**
+   * System property for supplying additional allowed package prefixes.
+   * Value is a comma-separated list, e.g. {@code com.acme.nlp.,com.other.}.
+   * <p>
+   * This property is read once at class-load time. If it cannot be set via
+   * {@code -D} at JVM startup (e.g. in embedded or test scenarios), call
+   * {@link #registerAllowedPackage(String)} before loading any model that
+   * uses a custom factory or serializer.
+   */
+  public static final String ALLOWED_PACKAGES_PROPERTY = 
"OPENNLP_EXT_ALLOWED_PACKAGES";
+
+  /**
+   * Package prefixes whose classes are permitted to be instantiated as 
extensions.
+   * Seeded from {@code opennlp.} plus any prefixes in {@link 
#ALLOWED_PACKAGES_PROPERTY}.
+   */
+  private static final Set<String> ALLOWED_PREFIXES = initAllowedPrefixes();
+
+  private static Set<String> initAllowedPrefixes() {
+    Set<String> prefixes = new 
CopyOnWriteArraySet<>(Collections.singleton("opennlp."));
+    String prop = System.getProperty(ALLOWED_PACKAGES_PROPERTY);

Review Comment:
   Done, thanks.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to