A class is loaded by a name computed at run time

tx0/java:reflective-class-loadstrictwarningsecurity, cwe-470

What it finds

Class.forName(name) and loader.loadClass(name) with a name computed at run time load whatever class the name says. When the name comes from outside, it can pick a class the program never meant to load, whose static initializer then runs (CWE-470). A fixed map from accepted names to classes keeps the choice under control.

Reported are calls whose class name is not a string literal; in the strict tier, since plugin systems do this on purpose.

From the rule's source, reflective_class_load.rs.

Example from the rule's tests
class Plugins {  Object load(String name, ClassLoader loader) throws Exception {    Class<?> a = Class.forName(name);tx0 reports: the class to load is chosen at run time    Class<?> b = Class.forName("java.util.ArrayList");    Class<?> c = loader.loadClass(name);tx0 reports: the class to load is chosen at run time    return a;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:reflective-class-load" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:reflective-class-load] reason="..."

tx0 explain tx0/java:reflective-class-load shows the same in the terminal. Found a false positive? Report it.