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

            Bug ID: 70360
           Summary: UriUtil.warToJar double-encodes % in the path (%20 →
                    %2520), breaking JAR scanning for WARs in paths with
                    spaces
           Product: Tomcat 11
           Version: 11.0.27
          Hardware: PC
            Status: NEW
          Severity: regression
          Priority: P2
         Component: Util
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: -------

Method UriUtil.warToJar builds the jar: URL with the multi-argument URI
constructor, which always quotes %. A war:file: URL whose path is already
percent-encoded therefore gets encoded twice. In Tomcat 10.1.x the same method
does not re-encode.

I first hit this with a Spring Boot executable WAR. It can be reproduced with
Tomcat alone, no Spring Boot involved.

Minimal reproduction

import java.net.URL;
import org.apache.tomcat.util.buf.UriUtil;

public class WarToJarRepro {
    public static void main(String[] args) throws Exception {
       
org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.getInstance();
        URL war = new
URL("war:file:/C:/some%20dir/app.war*/WEB-INF/lib/a.jar");
        System.out.println("in : " + war);
        System.out.println("out: " + UriUtil.warToJar(war));
    }
}

Run with java -cp tomcat-embed-core-<version>.jar WarToJarRepro.java.

┌─────────┬──────────────────────────────────────────────────────┐
│ Version │ Output                                               │
├─────────┼──────────────────────────────────────────────────────┤
│ 10.1.61 │ jar:file:/C:/some%20dir/app.war!/WEB-INF/lib/a.jar   │
├─────────┼──────────────────────────────────────────────────────┤
│ 11.0.29 │ jar:file:/C:/some%2520dir/app.war!/WEB-INF/lib/a.jar │
└─────────┴──────────────────────────────────────────────────────┘

Observed impact (Spring Boot 4.1.2 executable WAR, embedded Tomcat 11.0.29)
Run with java -jar app.war from C:\some dir\:

WARN org.apache.tomcat.util.scan.StandardJarScanner - Failed to scan JAR
[/WEB-INF/lib/spring-webmvc-7.0.10.jar] from /WEB-INF/lib
java.nio.file.NoSuchFileException: C:\some%20dir\app.war
  at java.base/java.util.jar.JarFile.<init>
  at
org.springframework.boot.loader.net.protocol.jar.UrlJarFileFactory.createJarFileForLocalFile
  at
org.springframework.boot.loader.net.protocol.jar.JarUrlConnection.getJarFile
  at org.apache.tomcat.util.scan.JarFileUrlNestedJar.<init>
  at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:55)
  at org.apache.tomcat.util.scan.StandardJarScanner.process

This repeats for every JAR in /WEB-INF/lib. The consumer decodes the URL once
and tries to open a file literally named some%20dir. A path without spaces
works. The same WAR works with Tomcat 10.1.x.

Root cause

public static URL warToJar(URL warUrl) throws IOException {
    String file = warUrl.getFile();          // already percent-encoded
    ...
    uri = new URI("jar", file, null);        // quotes '%' again
    return uri.toURL();
}

In 10.1.x the method ended with new URL("jar", warUrl.getHost(),
warUrl.getPort(), file).

Variable file is already a valid encoded URI path, so use the single-argument
constructor, which doesn't re-encode. This is the same approach buildJarUrl and
JarFactory.getJarEntryURL already use.

uri = new URI("jar:" + file);

I have confirmed this on 11.0.29, which is the latest 11.x release (but not
available in the form I used for submission). The 11.0.x and main branches
still contain the new URI("jar", file, null) call. I haven't tested other
percent-encoded characters (such as #, [ or non-ASCII); they likely behave the
same.
I searched both GitHub and Bugzilla for an existing report and found none.

-- 
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