Uploaded image for project: 'WildFly'
  1. WildFly
  2. WFLY-937

ServletContext.getResource() doesn't work correctly for files containing hash characters

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Major
    • 8.1.0.CR1, 8.1.0.Final
    • JBoss AS7 7.2.0.Final, 8.0.0.CR1
    • Web (Undertow)
    • None
    • Hide

      Deploy the attached sample project to JBoss AS 7.1.3.Final. A ServletContextListener will try to load a problematic file and print the results to the console. The console will show something like this:

      15:34:00,036 INFO  [stdout] (MSC service thread 1-2) =============================================================
      15:34:00,039 INFO  [stdout] (MSC service thread 1-2) url.toString():   jndi:/default-host/tomcat-url-bug/file#1.txt
      15:34:00,040 INFO  [stdout] (MSC service thread 1-2) url.getPath():    /default-host/tomcat-url-bug/file
      15:34:00,040 INFO  [stdout] (MSC service thread 1-2) url.getRef():     1.txt
      15:34:00,041 INFO  [stdout] (MSC service thread 1-2) url.openStream(): java.io.FileNotFoundException
      15:34:00,042 INFO  [stdout] (MSC service thread 1-2) =============================================================
      

      Expected output:

      15:34:10,382 INFO  [stdout] (MSC service thread 1-1) =============================================================
      15:34:10,383 INFO  [stdout] (MSC service thread 1-1) url.toString():   jndi:/default-host/tomcat-url-bug/file%231.txt
      15:34:10,383 INFO  [stdout] (MSC service thread 1-1) url.getPath():    /default-host/tomcat-url-bug/file%231.txt
      15:34:10,384 INFO  [stdout] (MSC service thread 1-1) url.getRef():     null
      15:34:10,384 INFO  [stdout] (MSC service thread 1-1) url.openStream(): SUCCESS
      15:34:10,385 INFO  [stdout] (MSC service thread 1-1) =============================================================
      

      Note: # has to be encoded as %23.

      Show
      Deploy the attached sample project to JBoss AS 7.1.3.Final. A ServletContextListener will try to load a problematic file and print the results to the console. The console will show something like this: 15:34:00,036 INFO [stdout] (MSC service thread 1-2) ============================================================= 15:34:00,039 INFO [stdout] (MSC service thread 1-2) url.toString(): jndi:/ default -host/tomcat-url-bug/file#1.txt 15:34:00,040 INFO [stdout] (MSC service thread 1-2) url.getPath(): / default -host/tomcat-url-bug/file 15:34:00,040 INFO [stdout] (MSC service thread 1-2) url.getRef(): 1.txt 15:34:00,041 INFO [stdout] (MSC service thread 1-2) url.openStream(): java.io.FileNotFoundException 15:34:00,042 INFO [stdout] (MSC service thread 1-2) ============================================================= Expected output: 15:34:10,382 INFO [stdout] (MSC service thread 1-1) ============================================================= 15:34:10,383 INFO [stdout] (MSC service thread 1-1) url.toString(): jndi:/ default -host/tomcat-url-bug/file%231.txt 15:34:10,383 INFO [stdout] (MSC service thread 1-1) url.getPath(): / default -host/tomcat-url-bug/file%231.txt 15:34:10,384 INFO [stdout] (MSC service thread 1-1) url.getRef(): null 15:34:10,384 INFO [stdout] (MSC service thread 1-1) url.openStream(): SUCCESS 15:34:10,385 INFO [stdout] (MSC service thread 1-1) ============================================================= Note: # has to be encoded as %23 .

    Description

      ServletContext.getResource() returns an invalid URL if the file name contains # characters.

      Example:

      URL url = servletContext.getResource("/file#1.txt");
      

      The resulting URL object will be spitted at the # character like this:

      url.toString():   jndi:/default-host/tomcat-url-bug/file#1.txt
      url.getPath():    /default-host/tomcat-url-bug/file
      url.getRef():     1.txt
      

      Calling url.openStream() will throw a FileNotFoundException.

      This is a known Tomcat issue fixed in 7.0.28.

      References:

      https://issues.apache.org/bugzilla/show_bug.cgi?id=53257
      https://issues.apache.org/bugzilla/show_bug.cgi?id=51584

      According to the discussion on the Tomcat issue tracker, there are also problems for other characters which have special meaning in URLs.

      Attachments

        Activity

          People

            sdouglas1@redhat.com Stuart Douglas
            christian@kaltepoth.de Christian Kaltepoth
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: