...
This noncompliant code example accepts a class object from untrusted code, creates a new instance of the class using the permissions of the immediate caller getInstance()
and returns the created instance back to untrusted code.
Code Block | ||
---|---|---|
| ||
public class Trusted {
public Object getInstance(Class<?> c) throws InstantiationException, IllegalAccessException {
return c.newInstance();
}
}
|
Compliant Solution
This compliant solution reduces the accessibility of getInstance()
to package-private so that untrusted code cannot obtain the newly created instance.
Code Block | ||
---|---|---|
| ||
public class Trusted {
Object getInstance(Class<?> c) throws InstantiationException, IllegalAccessException {
return c.newInstance();
}
}
|
Noncompliant Code Example
This noncompliant code example shows a vulnerability present in several versions of the Tomcat HTTP web server (fixed in v 6.0.20) that allows untrusted web applications to override the default XML parser used by the system to process web.xml, context.xml and tld files of other web applications deployed on the Tomcat instance. Consequently, untrusted web applications that install a parser can view and/or alter these files under limited circumstances.
...
Similarly, the contextDigester
processing is also broken in the affected versions.
Compliant Solution
This compliant solution uses an init()
method to create the webDigester
.
...
The explicit webDigester.getParser()
call causes the newInstance()
method to be invoked using the container's class loader instead of the WebAppClassLoader. This is because the flag useContextClassLoader
is set during initialization which captures the container's class loader at that time to define the Digester
(the context class loader is the container's class loader at this point). Later, even if the Tomcat server still uses the WebappClassLoader to create the parser instance when attempting to process the web.xml and other files, the explicit call to getParser()
in init()
ensures that the default parser is set during prior initialization and is impossible to replace. Because this is a one-time setting, future attempts to change the parser are futile.
Compliant Solution
Do not accept Class
, ClassLoader
or Thread
instances from untrusted code. If inevitable, safely acquire these instances by ensuring they come from trusted sources. Additionally, make sure to discard tainted inputs from untrusted code. Likewise, objects returned by the affected methods should not be propagated back to the untrusted code.
...