Skip to content

Commit

Permalink
More defensive coding around some XML activities that are triggered b…
Browse files Browse the repository at this point in the history
…y web applications and are therefore at potential risk of a memory leak.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk@1589997 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Apr 25, 2014
1 parent eb70282 commit 6246d83
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions java/org/apache/jasper/xmlparser/ParserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.security.AccessController;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -29,6 +30,8 @@
import org.apache.tomcat.util.descriptor.DigesterFactory;
import org.apache.tomcat.util.descriptor.LocalResolver;
import org.apache.tomcat.util.descriptor.XmlErrorHandler;
import org.apache.tomcat.util.security.PrivilegedGetTccl;
import org.apache.tomcat.util.security.PrivilegedSetTccl;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
Expand Down Expand Up @@ -92,7 +95,23 @@ public TreeNode parseXMLDocument(String location, InputSource is)
Document document = null;

// Perform an XML parse of this document, via JAXP
ClassLoader original;
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedGetTccl pa = new PrivilegedGetTccl();
original = AccessController.doPrivileged(pa);
} else {
original = Thread.currentThread().getContextClassLoader();
}
try {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedSetTccl pa =
new PrivilegedSetTccl(ParserUtils.class.getClassLoader());
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(
ParserUtils.class.getClassLoader());
}

DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
Expand Down Expand Up @@ -132,6 +151,13 @@ public TreeNode parseXMLDocument(String location, InputSource is)
} catch (IOException io) {
throw new JasperException
(Localizer.getMessage("jsp.error.parse.xml", location), io);
} finally {
if (Constants.IS_SECURITY_ENABLED) {
PrivilegedSetTccl pa = new PrivilegedSetTccl(original);
AccessController.doPrivileged(pa);
} else {
Thread.currentThread().setContextClassLoader(original);
}
}

// Convert the resulting document to a graph of TreeNodes
Expand Down

0 comments on commit 6246d83

Please sign in to comment.