Bug 66388 - Classpath resource having a name containing a space can't be read anymore by ClassLoader.getResources(String name)
Summary: Classpath resource having a name containing a space can't be read anymore by ...
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 9
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 9.0.70
Hardware: PC Linux
: P2 regression with 15 votes (vote)
Target Milestone: -----
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
: 66409 (view as bug list)
Depends on:
Blocks:
 
Reported: 2022-12-13 21:02 UTC by Réda Housni Alaoui
Modified: 2023-01-04 09:39 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Réda Housni Alaoui 2022-12-13 21:02:49 UTC
We are trying to upgrade from Tomcat 9.0.69 to 9.0.70.

Booting our application on 9.0.70 fails because we are trying to read a the content of a classpath resource (sql file) having name containing a space.
The file name is: 'com/aqme/product/database/migration/sql/_443/V443_17_36495__some name.sql'

I think this is caused by https://github.com/apache/tomcat/commit/2a03ff1b778e6f4d05a60c3a9597b42ed4176aed#diff-019a2ddbffcf645220f667a8344672adca68f474f437195916ff157adb955fadR129 aka "Refactor code base to replace use of URL constructors. While they are deprecated in Java 20 onwards, the reasons for deprecation are valid for all versions so move away from them now." .

According to my test, the URI constructor considers a space character as illegal:


>import java.net.URISyntaxException;

>class Scratch {
>	public static void main(String[] args) throws URISyntaxException {
>		new >java.net.URI("com/aqme/product/database/migration/sql/_443/V443_17_36495__some name.sql");
>	}
>}

>Exception in thread "main" java.net.URISyntaxException: Illegal character in >path at index 65: >com/aqme/product/database/migration/sql/_443/V443_17_36495__some name.sql
>	at java.base/java.net.URI$Parser.fail(URI.java:2974)
>	at java.base/java.net.URI$Parser.checkChars(URI.java:3145)
>	at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3227)
>	at java.base/java.net.URI$Parser.parse(URI.java:3186)
>	at java.base/java.net.URI.<init>(URI.java:623)
>	at Scratch.main(scratch_11.java:5)

So my guess is that since https://github.com/apache/tomcat/commit/2a03ff1b778e6f4d05a60c3a9597b42ed4176aed#diff-019a2ddbffcf645220f667a8344672adca68f474f437195916ff157adb955fadR129 any resource name containing a space will be unreachable.

Is it forbidden (from the Java Spec) to ask a ClassLoader for a resource name containing a space? If yes, could you please push back this breaking change to a new Tomcat major version?
Comment 1 slash 2022-12-13 21:08:03 UTC
Just a quick word to add that this code change was also pushed to tomcat 10.1.3 and so it affect both the tomcat 9.0 and tomcat 10.1 branches.
Comment 2 Réda Housni Alaoui 2022-12-13 21:23:44 UTC
Please note that the classloader involved in junit automatic tests (I suppose a java native one) is able to retrieve the resource URI without any issue with the space character:

> @Test
>  void test() throws URISyntaxException {
>	  System.out.println(getClass()
>			  .getResource(
>					  "/com/aqme/product/database/migration/sql/_443/V443_17_36495__some name.sql")
>			  .toURI());
>  }

produces:

> file:/home/rhousni/projects/center/core/common/target/classes/com/cos/center/database/migration/sql/_443/V443_17_36495__add_equipment_misc_and%20tag_in_inventory.sql

> Process finished with exit code 0

As you can see, the space has been replaced with '%20'
Comment 3 Mark Thomas 2022-12-14 12:51:43 UTC
The following trivial JSP works as expected:

<%
java.net.URL u = this.getClass().getClassLoader().getResource("test/a a.txt");
%>
<p><%=u.toString()%></p>

Please provided the simplest test case that demonstrates the issue you are seeing.
Comment 4 Réda Housni Alaoui 2022-12-14 15:35:37 UTC
Hi Mark Thomas, 

Please find the simplest reproducer at https://github.com/Cosium/tomcat-bug-66388/tree/2ee61392f805895c8e6cda61a5ab518a74de3084 .
Comment 5 Mark Thomas 2022-12-14 16:50:17 UTC
Tx. That clarifies things. The issue is when resources are packaged in JARs. Looking now...
Comment 6 Mark Thomas 2022-12-14 18:28:03 UTC
Fixed in:
- 11.0.x for 11.0.0-M2 onwards
- 10.1.x for 10.1.5 onwards
-  9.0.x for  9.0.71 onwards
-  8.5.x for  8.5.85 onwards
Comment 7 Remy Maucherat 2023-01-04 09:39:12 UTC
*** Bug 66409 has been marked as a duplicate of this bug. ***