Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/org/apache/commons/cli/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public interface Converter<T, E extends Exception> {
/**
* Converts a String to a {@link Class}. Calls {@link Class#forName(String, boolean, ClassLoader)} with {@code initialize} set to {@code false} so that
* resolving the name does not by itself trigger the class's static initializer.
* <p>
* The value can name any class visible to the class loader of this library. Do not apply this converter, the default for options of type {@link Class},
* to values from an untrusted source.
* </p>
*/
Converter<Class<?>, ClassNotFoundException> CLASS = s -> Class.forName(s, false, Converter.class.getClassLoader());

Expand All @@ -66,6 +70,10 @@ public interface Converter<T, E extends Exception> {

/**
* Converts a class name to an instance of the class. Uses the Class converter to find the class and then call the default constructor.
* <p>
* Creating the instance initializes the named class and runs its public no-argument constructor, so the value decides which code runs. Do not apply this
* converter, the default for options of type {@link Object}, to values from an untrusted source.
* </p>
*
* @see #CLASS
*/
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more
* </tr>
* <tr>
* <td>b@</td>
* <td>-b [class name]</td>
* <td>-b [class name] (creates object via public no-argument constructor)</td>
* </tr>
* <tr>
* <td>c&gt;</td>
* <td>-c [file name]</td>
* </tr>
* <tr>
* <td>d+</td>
* <td>-d [class name] (creates object via empty constructor)</td>
* <td>-d [class name]</td>
* </tr>
* <tr>
* <td>e%</td>
Expand All @@ -69,6 +69,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more
* </pre>
*
* <p>
* When a converted option value is retrieved, for example through {@link CommandLine#getParsedOptionValue(String)}, the {@code @} and {@code +} codes
* load the class named by the option value, and {@code @} also creates an instance by calling its public no-argument constructor. Do not use these codes
* for values from an untrusted source.
* </p>
*
* <p>
* TODO These need to break out to OptionType and also to be pluggable.
* </p>
*/
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/apache/commons/cli/TypeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class TypeHandler {

/**
* Returns the class whose name is {@code className}.
* <p>
* Do not pass a class name from an untrusted source, see {@link Converter#CLASS}.
* </p>
*
* @param className The class name.
* @return The class if it is found.
Expand Down Expand Up @@ -124,6 +127,10 @@ public static Number createNumber(final String string) throws ParseException {

/**
* Creates an Object from the class name and empty constructor.
* <p>
* This initializes the named class and runs its public no-argument constructor. Do not pass a class name from an untrusted source, see
* {@link Converter#OBJECT}.
* </p>
*
* @param className The argument value.
* @return The initialized object.
Expand Down
Loading