Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V5322. OWASP. Possible reflection injec…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V5322. OWASP. Possible reflection injection. Potentially tainted data is used to select class or method.

Mar 28 2025

The analyzer has detected that a class is selected via the reflection mechanism without its validation.

This vulnerability can be categorized under the OWASP Top 10 2021 classification as follows:

The example:

public void applyAction(HttpServletRequest request) {
  String action = request.getParameter("action");
  String className = action + "Controller";

  Class<?> actionClass = Class.forName(className); // <=

  Object actionClassObject = actionClass.newInstance();
  Method applyMethod = actionClass.getMethod("apply");

  applyMethod.invoke(actionClassObject);
}

In this example, the server processes a request for an action specified as a string in the request parameter. The action is implemented as a certain class with the apply method. However, there is no validation to ensure which specific class is used. In this case, attackers can manipulate the request to select the wrong class, leading to unauthorized actions. This can cause program logic failures or security issues.

In the worst-case scenario, if attackers change classpath, they can use reflection to inject malicious code.

To harden security, developers should validate input.

The fixed code:

private List<String> ALLOWED_TYPES = new ArrayList<>();

public void validateAndApplyAction(HttpServletRequest request) {
  String action = request.getParameter("action");
  String className = action + "Controller";

  if (!ALLOWED_TYPES.contains(className)) {  // <=
    return;
  }

  Class<?> actionClass = Class.forName(className);

  Object actionClassObject = actionClass.newInstance();
  Method applyMethod = actionClass.getMethod("apply");
  applyMethod.invoke(actionClassObject);
}

It is also possible for a wrong method to be selected instead of a wrong class:

public void useMethodWithName(HttpServletRequest request) {
    String methodName = request.getParameter("methodName");
    Method method = this.getClass().getMethod(methodName); // <=
    method.invoke(this);
}

To prevent the wrong method from being selected, developers can apply the same approach:

private List<String> ALLOWED_METHODS = new ArrayList<>();

public void useSafeMethodWithName(HttpServletRequest request) {
    String methodName = request.getParameter("methodName");
    if (ALLOWED_METHODS.contains(methodName)) {                // <=
        Method method = this.getClass().getMethod(methodName);
        method.invoke(this);
    }
}

This diagnostic is classified as:

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam