https://bz.apache.org/bugzilla/show_bug.cgi?id=70375

            Bug ID: 70375
           Summary: WebappClassLoaderBase: getResources() rejects names
                    with a leading '/' since 9.0.124, getResource() still
                    accepts them
           Product: Tomcat 9
           Version: 9.0.124
          Hardware: All
                OS: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: Catalina
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: -----

Since 9.0.124, WebappClassLoaderBase.getResources(name) returns an empty
enumeration when name starts with '/', while getResource(name) with the same
name still returns the resource. In 9.0.123 both returned it.


1. getResource() and getResources() now disagree

For a file WEB-INF/classes/test.txt:

  Call                         9.0.123   9.0.124
  getResources("test.txt")     1 URL     1 URL
  getResources("/test.txt")    1 URL     empty
  getResource("/test.txt")     URL       URL

The same holds for resources in JARs in WEB-INF/lib and for .class resources.

Whether a leading '/' should be accepted is a separate question (the JRE class
loaders do not accept it, and main now rejects it everywhere). Within one
release, though, getResource() and getResources() should agree on whether a
name is valid.


2. The change is a backport side effect, not listed in the changelog

The change is commit 88206c72e6 on 9.0.x ("Fix NPE in
WebappClassLoaderBase.getResources() for leading-slash names"), which adds to
findResources():

    if (name == null || name.startsWith("/")) {
        return Collections.emptyEnumeration();
    }

Its commit message describes it as a fix for an NPE introduced by the resource
name rework in eb91c3e64b. That rework made findResource(), findResources() and
getResourceAsStream() return null for names starting with '/', and removed the
leading-slash branch of nameToPath(). It is only on main: its changelog entry
sits above the "Entries for backport and removal before 12.0.0-M1 below this
line" marker, and it was not backported.

On 9.0.x findResources() never returned null for these names, so there was no
NPE to fix. The backport instead introduced the leading-slash rejection on its
own, and only in findResources(). findResource(), getResourceAsStream() and
nameToPath() still accept a leading '/', which explains the table above.

The 9.0.124 changelog has no entry for this behaviour change.


3. Impact

Applications that pass leading-slash names to getResources(), directly or
through a framework, worked on 9.0.123 and stop finding the resources on
9.0.124, usually without an error.

Example: Apache Struts resolves <include file="..."/> in struts.xml with
getResources() on the name as written. Includes written as <include
file="/config/struts/user.xml"/> loaded on 9.0.123 and are skipped on 9.0.124
(Struts logs this at DEBUG only), so the actions they define are missing at
runtime. Struts' own examples use names without the leading '/', so these
configurations depended on Tomcat's lenient behaviour, but that behaviour
changed in a patch release without notice.

A similar case was bug 70049: a resource lookup change in 9.0.118 made
getResources() throw for some names and broke MyBatis, and from 9.0.119,
10.1.56 and 11.0.23 it returns an empty enumeration instead, so existing
applications kept working.


Steps to reproduce

1. Create a webapp with a file WEB-INF/classes/test.txt.
2. Add a JSP:

<%@ page contentType="text/plain" import="java.util.Collections" %><%
ClassLoader cl = Thread.currentThread().getContextClassLoader();
out.println("getResources: " +
Collections.list(cl.getResources("/test.txt")).size());
out.println("getResource:  " + cl.getResource("/test.txt"));
%>

3. Deploy it on 9.0.123 and on 9.0.124 and request the JSP.

On 9.0.123: "getResources: 1" and a URL.
On 9.0.124: "getResources: 0", while "getResource:" still prints a URL.

Reproduced with the official binary distributions of 9.0.123 and 9.0.124,
default conf/ (only the ports changed), the webapp deployed as an exploded ROOT
directory.


Other branches

>From the source at the release tags (not tested at runtime), the same backport
is in 10.1.x (48c8c06645, first released in 10.1.62) and 11.0.x (17c52f2447,
first released in 11.0.28), with the same result: findResources() rejects a
leading '/', nameToPath() still accepts it.


Suggested resolution

One of:

- On 9.0.x, 10.1.x and 11.0.x, revert the findResources() check from the
backport, restoring the behaviour of 9.0.123 / 10.1.61 / 11.0.27, where
getResources() and getResource() agree. The added tests
testFindResourcesLeadingSlash and testGetResourcesLeadingSlash assert the new
behaviour and would need to go too.

- If rejecting leading-slash names is intended for these branches, apply it to
findResource() and getResourceAsStream() as well (as eb91c3e64b does on main),
and list it in the changelog as a behaviour change.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to