You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/03/28 19:59:53 UTC

[logging-log4j2] branch release-2.x updated (abd83e6 -> 1efbb98)

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

rgoers pushed a change to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


    from abd83e6  Avoid NullPointerException when StackWalker returns null.
     new 0c0fa89  LOG4J2-2761: For absolute URIs don't fail on wrongly formatted file URIs
     new 1efbb98  LOG4J2-2761 - Fix Exceptions when whitespace is in the file path and Java security manager is used.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/logging/log4j/core/util/FileUtils.java  | 53 +++++++++-------------
 .../logging/log4j/core/util/FileUtilsTest.java     | 46 +++++++++----------
 src/changes/changes.xml                            |  3 ++
 3 files changed, 46 insertions(+), 56 deletions(-)


[logging-log4j2] 02/02: LOG4J2-2761 - Fix Exceptions when whitespace is in the file path and Java security manager is used.

Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 1efbb98e26fefc78efca9c87862dbc4d44f9ad68
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sat Mar 28 12:59:38 2020 -0700

    LOG4J2-2761 - Fix Exceptions when whitespace is in the file path and Java security manager is used.
---
 .../apache/logging/log4j/core/util/FileUtils.java  | 11 ++----
 .../logging/log4j/core/util/FileUtilsTest.java     | 46 ++++++++++------------
 src/changes/changes.xml                            |  3 ++
 3 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
index 27fa8ef..a50a20d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
@@ -81,10 +81,7 @@ public final class FileUtils {
                     return file;
                 }
                 final String path = uri.getPath();
-                file = new File(path);
-                if (file.exists()) {
-                    return file;
-                }
+                return new File(path);
             } catch (final Exception ex) {
                 LOGGER.warn("Invalid URI {}", uri);
             }
@@ -106,7 +103,7 @@ public final class FileUtils {
 
     /**
      * Asserts that the given directory exists and creates it if necessary.
-     *
+     * 
      * @param dir the directory that shall exist
      * @param createDirectoryIfNotExisting specifies if the directory shall be created if it does not exist.
      * @throws java.io.IOException thrown if the directory could not be created.
@@ -125,10 +122,10 @@ public final class FileUtils {
             throw new IOException("File " + dir + " exists and is not a directory. Unable to create directory.");
         }
     }
-
+    
     /**
      * Creates the parent directories for the given File.
-     *
+     * 
      * @param file
      * @throws IOException
      */
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/FileUtilsTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/FileUtilsTest.java
index 4294423..17568dc 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/FileUtilsTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/FileUtilsTest.java
@@ -45,45 +45,41 @@ public class FileUtilsTest {
     }
 
     @Test
-    public void testFileFromUriWithSpacesAndPlusCharactersInName() throws Exception {
-        final String config = "target/test-classes/s%20p%20a%20c%20e%20s/log4j%2Bconfig%2Bwith%2Bplus%2Bcharacters.xml";
-        final URI uri = new URI(config);
+    public void testAbsoluteFileFromUriWithPlusCharactersInName() throws Exception {
+        final String config = "target/test-classes/log4j+config+with+plus+characters.xml";
+        final URI uri = new File(config).toURI();
         final File file = FileUtils.fileFromUri(uri);
         assertEquals(LOG4J_CONFIG_WITH_PLUS, file.getName());
         assertTrue("file exists", file.exists());
     }
 
-    /**
-     * Helps figure out why {@link #testFileFromUriWithPlusCharactersInName()} fails in Jenkins but asserting different
-     * parts of the implementation of {@link FileUtils#fileFromUri(URI)}.
-     */
     @Test
-    public void testFileExistsWithPlusCharactersInName() throws Exception {
+    public void testAbsoluteFileFromUriWithSpacesInName() throws Exception {
+        final String config = "target/test-classes/s p a c e s/log4j+config+with+plus+characters.xml";
+        final URI uri = new File(config).toURI();
+        final File file = FileUtils.fileFromUri(uri);
+        assertEquals(LOG4J_CONFIG_WITH_PLUS, file.getName());
+        assertTrue("file exists", file.exists());
+    }
+
+    @Test
+    public void testAbsoluteFileFromJBossVFSUri() throws Exception {
         final String config = "target/test-classes/log4j+config+with+plus+characters.xml";
-        final File file = new File(config);
+        final String uriStr = new File(config).toURI().toString().replaceAll("^file:", "vfsfile:");
+        assertTrue(uriStr.startsWith("vfsfile:"));
+        final URI uri = URI.create(uriStr);
+        final File file = FileUtils.fileFromUri(uri);
         assertEquals(LOG4J_CONFIG_WITH_PLUS, file.getName());
         assertTrue("file exists", file.exists());
-        //
-        final URI uri1 = new URI(config);
-        assertNull(uri1.getScheme());
-        //
-        final URI uri2 = new File(uri1.getPath()).toURI();
-        assertNotNull(uri2);
-        assertTrue("URI \"" + uri2 + "\" does not end with \"" + LOG4J_CONFIG_WITH_PLUS + "\"", uri2.toString()
-                .endsWith(LOG4J_CONFIG_WITH_PLUS));
-        //
-        final String fileName = uri2.toURL().getFile();
-        assertTrue("File name \"" + fileName + "\" does not end with \"" + LOG4J_CONFIG_WITH_PLUS + "\"",
-                fileName.endsWith(LOG4J_CONFIG_WITH_PLUS));
     }
 
     @Test
-    public void testFileFromUriWithPlusCharactersConvertedToSpacesIfFileDoesNotExist() throws Exception {
-        final String config = "NON-EXISTING-PATH/this+file+does+not+exist.xml";
+    public void testFileFromUriWithSpacesAndPlusCharactersInName() throws Exception {
+        final String config = "target/test-classes/s%20p%20a%20c%20e%20s/log4j%2Bconfig%2Bwith%2Bplus%2Bcharacters.xml";
         final URI uri = new URI(config);
         final File file = FileUtils.fileFromUri(uri);
-        assertEquals("this file does not exist.xml", file.getName());
-        assertFalse("file does not exist", file.exists());
+        assertEquals(LOG4J_CONFIG_WITH_PLUS, file.getName());
+        assertTrue("file exists", file.exists());
     }
 
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ea39481..845b060 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,6 +30,9 @@
          - "remove" - Removed
     -->
     <release version="2.13.2" date="2020-MM-DD" description="GA Release 2.13.2">
+      <action issue="LOG4J2-2761" dev="rgoers" type="fix" due-to="Uwe Schindler">
+        Fix Exceptions when whitespace is in the file path and Java security manager is used.
+      </action>
       <action issue="LOG4J2-2809" dev="rgoers" type="fix" due-to="Romain Manni-Bucau">
         Avoid NullPointerException when StackWalker returns null.
       </action>


[logging-log4j2] 01/02: LOG4J2-2761: For absolute URIs don't fail on wrongly formatted file URIs

Posted by rg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 0c0fa898198f531adfacda5f88fbc2b2cba31639
Author: Uwe Schindler <uw...@thetaphi.de>
AuthorDate: Thu Mar 26 09:54:17 2020 +0100

    LOG4J2-2761: For absolute URIs don't fail on wrongly formatted file URIs
---
 .../apache/logging/log4j/core/util/FileUtils.java  | 46 ++++++++++------------
 1 file changed, 20 insertions(+), 26 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
index 6fd73c2..27fa8ef 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
@@ -18,12 +18,9 @@ package org.apache.logging.log4j.core.util;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -60,40 +57,37 @@ public final class FileUtils {
      * @return the resulting file object
      */
     public static File fileFromUri(URI uri) {
-        // There MUST be a better way to do this. TODO Search other ASL projects...
-        if (uri == null || (uri.getScheme() != null
-                && (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri.getScheme())))) {
+        if (uri == null) {
             return null;
         }
-        if (uri.getScheme() == null) {
-            File file = new File(uri.toString());
-            if (file.exists()) {
-                return file;
+        if (uri.isAbsolute()) {
+            if (JBOSS_FILE.equals(uri.getScheme())) try {
+                // patch the scheme
+                uri = new URI(PROTOCOL_FILE, uri.getSchemeSpecificPart(), uri.getFragment());
+            } catch (URISyntaxException use) {
+                // should not happen, ignore
+            }
+            try {
+                if (PROTOCOL_FILE.equals(uri.getScheme())) {
+                    return new File(uri);
+                }
+            } catch (final Exception ex) {
+                LOGGER.warn("Invalid URI {}", uri);
             }
+        } else {
+            File file = new File(uri.toString());
             try {
+                if (file.exists()) {
+                    return file;
+                }
                 final String path = uri.getPath();
                 file = new File(path);
                 if (file.exists()) {
                     return file;
                 }
-                uri = new File(path).toURI();
             } catch (final Exception ex) {
                 LOGGER.warn("Invalid URI {}", uri);
-                return null;
-            }
-        }
-        final String charsetName = StandardCharsets.UTF_8.name();
-        try {
-            String fileName = uri.toURL().getFile();
-            if (new File(fileName).exists()) { // LOG4J2-466
-                return new File(fileName); // allow files with '+' char in name
             }
-            fileName = URLDecoder.decode(fileName, charsetName);
-            return new File(fileName);
-        } catch (final MalformedURLException ex) {
-            LOGGER.warn("Invalid URL {}", uri, ex);
-        } catch (final UnsupportedEncodingException uee) {
-            LOGGER.warn("Invalid encoding: {}", charsetName, uee);
         }
         return null;
     }