← Java / Spring · Contents

JAVA-083 / Advanced

Input-driven class loading through reflection

A08:2021 · CWE-470 Use of Externally-Controlled Input to Select Classes

Why is this a problem?

Arbitrary class constructors or methods are invoked.

How to fix it

java
Map<String, Supplier<Handler>> REG = Map.of("pdf", PdfHandler::new);
Handler h = Optional.ofNullable(REG.get(type)).orElseThrow().get();

Further reading