...
Noncompliant Code Example
Accepting tainted inputs from untrusted code can A method that passes untrusted inputs to the Class.forName()
method might permit an attacker to load access classes with escalated privileges. The single argument Class.forName() method is another API method that uses its immediate caller's class loader to load a requested class. Untrusted code can misuse this API to indirectly manufacture classes that have the same privileges as those of the attacker's immediate caller.
Code Block | |||||
---|---|---|---|---|---|
| |||||
public Class loadClass(String className) { // className may be the name of a privileged or even a malicious class Class c =return Class.forName(className); } |
Compliant Solution
...
(Hardcoded Name)
Limit the visibility of the method that uses this API. Do not operate on tainted inputs. This compliant solution hard-codes the class's name.
Code Block | ||||
---|---|---|---|---|
| ||||
public Class loadClass() { c =return Class.forName("Foo"); // Explicitly hardcode } |
Noncompliant Code Example
...
Code Block | ||
---|---|---|
| ||
public Connection getConnection() { // ... return DriverManager.getConnection(url, username, password); } |
Compliant Solution
Ensure that instances of objects created using the unsafe methods are not returned to untrusted code. It is preferable to reduce the accessibility of methods that perform sensitive operations and define wrapper methods that are accessible from untrusted code.
...