BNaveen787221 (Community Member) asked a question.

Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')

Object newJavaBean = Class.forName((new StringBuilder(String.valueOf(packageName))).append(".").append(raw).toString()).newInstance();

 

 

in the above line code showing above security vulnerability


  • Hi @BNaveen787221 (Community Member)​ ,

     

    Veracode Static Analysis reports a flaw of CWE-470: Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection') when it can see data from outside of the application (for example from an HTTP Request but also from a file or database) going into the use of reflection.

    The concern is that an attacker would be able to run unintended code on your system which could lead to system compromise.

     

    Veracode Static Analysis will only automatically close this flaw if no dynamic data is used with the reflection APIs.

    In your example this would be 'packageName' and 'raw'.

     

    We recommend validating this dynamic data by using a list of hardcoded allowed values.

     

    For example:

     

    String[] allowedPackageNames = new String[]{"com.veracode"};

    String validatedPackageName = "com.veracode"; // Default packageName

    for (String allowedPackageName: allowedPackageNames) {

    if (allowedPackageNames.equals(packageName)) {

      validatedPackageName = allowedPackageName;

      }

    }

     

    String[] allowedClassNames = new String[]{"EmployeeBean"};

    String validatedClassName = "EmployeeBean"; // Default className

    for (String allowedClassName: allowedClassNames) {

    if (allowedClassNames.equals(raw)) {

      validatedClassName = allowedClassName;

      }

    }

     

    Object newJavaBean = Class.forName(

    (new StringBuilder(

    String.valueOf(validatedPackageName)))

    .append(".")

    .append(validatedClassName).toString())

    .newInstance();

     

    Thank you,

    Boy Baukema

    Expand Post

Topics (4)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.