You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/08/09 14:19:26 UTC

[tomcat] 02/02: Fix https://github.com/apache/tomcat/pull/187 Avoid NPE

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 8539fc8a8bebc52c513313f07403abb1dde92037
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri Aug 9 15:17:06 2019 +0100

    Fix https://github.com/apache/tomcat/pull/187 Avoid NPE
    
    Avoid a NullPointerException in the CrawlerSessionManagerValve if no
    ROOT Context is deployed and a request does not map to any of the other
    deployed Contexts.
    Patch provided by Jop Zinkweg.
---
 .../valves/CrawlerSessionManagerValve.java         |  2 +-
 .../valves/TestCrawlerSessionManagerValve.java     | 41 ++++++++++++++++++----
 webapps/docs/changelog.xml                         |  6 ++++
 3 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
index 773ef09..597aa98 100644
--- a/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
+++ b/java/org/apache/catalina/valves/CrawlerSessionManagerValve.java
@@ -265,7 +265,7 @@ public class CrawlerSessionManagerValve extends ValveBase {
         if (isHostAware) {
             result.append('-').append(host.getName());
         }
-        if (isContextAware) {
+        if (isContextAware && context != null) {
             result.append(context.getName());
         }
         return result.toString();
diff --git a/test/org/apache/catalina/valves/TestCrawlerSessionManagerValve.java b/test/org/apache/catalina/valves/TestCrawlerSessionManagerValve.java
index 86a86b5..a6eda04 100644
--- a/test/org/apache/catalina/valves/TestCrawlerSessionManagerValve.java
+++ b/test/org/apache/catalina/valves/TestCrawlerSessionManagerValve.java
@@ -98,6 +98,18 @@ public class TestCrawlerSessionManagerValve {
     }
 
     @Test
+    public void testCrawlerMultipleContextsContextAware() throws Exception {
+        CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
+        valve.setCrawlerUserAgents(valve.getCrawlerUserAgents());
+        valve.setHostAware(true);
+        valve.setContextAware(true);
+        valve.setNext(EasyMock.createMock(Valve.class));
+
+        verifyCrawlingContext(valve, "/examples");
+        verifyCrawlingContext(valve, null);
+    }
+
+    @Test
     public void testCrawlersSessionIdIsRemovedAfterSessionExpiry() throws IOException, ServletException {
         CrawlerSessionManagerValve valve = new CrawlerSessionManagerValve();
         valve.setCrawlerIps("216\\.58\\.206\\.174");
@@ -127,7 +139,20 @@ public class TestCrawlerSessionManagerValve {
     private void verifyCrawlingLocalhost(CrawlerSessionManagerValve valve, String hostname)
             throws IOException, ServletException {
         HttpSession session = createSessionExpectations(valve, true);
-        Request request = createRequestExpectations("127.0.0.1", session, true, hostname, "tomcatBot 1.0");
+        Request request = createRequestExpectations("127.0.0.1", session, true, hostname, "/examples", "tomcatBot 1.0");
+
+        EasyMock.replay(request, session);
+
+        valve.invoke(request, EasyMock.createMock(Response.class));
+
+        EasyMock.verify(request, session);
+    }
+
+
+    private void verifyCrawlingContext(CrawlerSessionManagerValve valve, String contextPath)
+            throws IOException, ServletException {
+        HttpSession session = createSessionExpectations(valve, true);
+        Request request = createRequestExpectations("127.0.0.1", session, true, "localhost", contextPath, "tomcatBot 1.0");
 
         EasyMock.replay(request, session);
 
@@ -151,14 +176,15 @@ public class TestCrawlerSessionManagerValve {
 
 
     private Request createRequestExpectations(String ip, HttpSession session, boolean isBot) {
-        return createRequestExpectations(ip, session, isBot, "localhost", "something 1.0");
+        return createRequestExpectations(ip, session, isBot, "localhost", "/examples", "something 1.0");
     }
 
-    private Request createRequestExpectations(String ip, HttpSession session, boolean isBot, String hostname, String userAgent) {
+    private Request createRequestExpectations(String ip, HttpSession session, boolean isBot, String hostname,
+            String contextPath, String userAgent) {
         Request request = EasyMock.createMock(Request.class);
         EasyMock.expect(request.getRemoteAddr()).andReturn(ip);
         EasyMock.expect(request.getHost()).andReturn(simpleHostWithName(hostname));
-        EasyMock.expect(request.getContext()).andReturn(simpleContextWithName());
+        EasyMock.expect(request.getContext()).andReturn(simpleContextWithName(contextPath));
         IExpectationSetters<HttpSession> setter = EasyMock.expect(request.getSession(false))
                 .andReturn(null);
         if (isBot) {
@@ -175,9 +201,12 @@ public class TestCrawlerSessionManagerValve {
         return host;
     }
 
-    private Context simpleContextWithName() {
+    private Context simpleContextWithName(String contextPath) {
+        if (contextPath == null) {
+            return null;
+        }
         Context context = EasyMock.createMock(Context.class);
-        EasyMock.expect(context.getName()).andReturn("/examples");
+        EasyMock.expect(context.getName()).andReturn(contextPath);
         EasyMock.replay(context);
         return context;
     }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d8a9200..3e260f7 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -79,6 +79,12 @@
         <bug>63550</bug>: Only try the <code>alternateURL</code> in the
         <code>JNDIRealm</code> if one has been specified. (markt)
       </fix>
+      <fix>
+        Avoid a <code>NullPointerException</code> in the
+        <code>CrawlerSessionManagerValve</code> if no ROOT Context is deployed
+        and a request does not map to any of the other deployed Contexts. Patch
+        provided by Jop Zinkweg. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org