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

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


The following commit(s) were added to refs/heads/8.5.x by this push:
     new b52978a  Fix https://github.com/apache/tomcat/pull/187 Avoid NPE
b52978a is described below

commit b52978a793f7ab52651c1b816dd6f57ea8d366ed
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 1bef60c..439afa6 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 ec783d8..631c979 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 4da9efc..22c0edf 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -95,6 +95,12 @@
         for patterns used with the <code>RewriteValve</code> with the
         description in the documentation. (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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to