You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Philzen (via GitHub)" <gi...@apache.org> on 2024/01/04 11:16:53 UTC

[PR] Amend description to reflect correct named user result with trailing slash [commons-io]

Philzen opened a new pull request, #553:
URL: https://github.com/apache/commons-io/pull/553

   Similar to #551, the trailing slash is actually preserved in the result here.
   
   Confirmed to have this behaviour in 2.4 and latest 2.15.1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #553:
URL: https://github.com/apache/commons-io/pull/553#discussion_r1441847412


##########
src/main/java/org/apache/commons/io/FilenameUtils.java:
##########
@@ -669,7 +669,7 @@ public static String getFullPath(final String fileName) {
      * ~            --&gt; ~
      * ~/           --&gt; ~
      * ~user        --&gt; ~user
-     * ~user/       --&gt; ~user
+     * ~user/       --&gt; ~user/

Review Comment:
   The existing javadoc and API is very unclear. I do not understand "Gets the full path from a full fileName, which is the prefix + path, and also excluding the final directory separator."
   
   The argument is not a filename. There is no such thing as a "full path" in normal parlance. Bug filed. https://issues.apache.org/jira/browse/IO-836
   
   I also agree that if this method turns ~user/ into ~user/ that's a bug. It should produce ~user.
   
   C:\ should also (80% certainty) turn into C:, not C:\
   
   I further expect there are additional bugs in this method involving Unix shell conventions and platform dependencies, but I'd have to dig into the rest of the class to understand exactly what those are.
   
   What a tasy can of worms you've opened. :-)
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #553:
URL: https://github.com/apache/commons-io/pull/553#issuecomment-1877263402

   Note that we should not treat ~ in any special way since it is a Unix-like shell utility and not a file system functionality.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "govind-gupta-tfs (via GitHub)" <gi...@apache.org>.
govind-gupta-tfs commented on PR #553:
URL: https://github.com/apache/commons-io/pull/553#issuecomment-3380782150

   `package com.thermofisher.gsd.archive;
   
   import org.apache.commons.lang3.Validate;
   import java.net.URLDecoder;
   import java.nio.charset.StandardCharsets;
   import java.nio.file.InvalidPathException;
   import java.nio.file.Path;
   import java.util.Locale;
   import java.util.Set;
   
   public class PathValidator {
   
       private static final Set<String> RESERVED_NAMES = Set.of(
               "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
               "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6",
               "LPT7", "LPT8", "LPT9"
       );
   
       public static void validateSafePathComponent(String component) {
           // --- 1. Basic Checks (Using Apache Commons Lang) ---
           Validate.notBlank(component, "Path component must not be blank");
           Validate.isTrue(component.length() <= 255, "Path component too long");
   
           // Pre-decode raw dangerous percent encodings
           String lower = component.toLowerCase(Locale.ROOT);
           if (lower.contains("%2f") || lower.contains("%5c") || lower.contains("%00")) {
               throw new IllegalArgumentException("Encoded separator/null not allowed: " + component);
           }
   
           // --- 2. Decoding ---
           String decoded;
           try {
               decoded = URLDecoder.decode(component, StandardCharsets.UTF_8);
           } catch (IllegalArgumentException ex) {
               throw new IllegalArgumentException("Invalid percent encoding: " + component);
           }
   
           // --- 3. Path Traversal/Normalization Check (Using Path API) ---
           Path p;
           try {
               p = Path.of(decoded);
           } catch (InvalidPathException e) {
               throw new IllegalArgumentException("Invalid path syntax: " + component);
           }
   
           // Critical Path Traversal check: must be a single name and not contain '..' or '.'
           if (p.isAbsolute() || p.getNameCount() != 1 || !p.normalize().equals(p)) {
               throw new IllegalArgumentException("Traversal/dot segments or absolute path not allowed: " + component);
           }
   
           String name = p.getFileName().toString();
   
           // --- 4. Final Name/Custom Policy Checks ---
   
           // Basic character / separator checks
           if (name.contains("/") || name.contains("\\") || name.indexOf('\0') >= 0 || name.contains("+")) {
               throw new IllegalArgumentException("Unsafe characters (separators/null/plus): " + component);
           }
   
           // Control chars
           for (int i = 0; i < name.length(); i++) {
               char c = name.charAt(i);
               if (c < 0x20 || c == 0x7F) {
                   throw new IllegalArgumentException("Control character present: " + component);
               }
           }
   
           // Dot-only or reserved dot forms & Trailing space/dot
           if (name.equals(".") || name.equals("..") || name.matches("\\.{2,}") ||
                   name.endsWith(" ") || name.endsWith(".")) {
               throw new IllegalArgumentException("Invalid dot sequence or trailing space/dot: " + component);
           }
   
           // Allowlist
           if (!name.matches("[A-Za-z0-9._-]+")) {
               throw new IllegalArgumentException("Disallowed characters: " + component);
           }
   
           // Windows reserved device names
           if (RESERVED_NAMES.contains(name.toUpperCase(Locale.ROOT))) {
               throw new IllegalArgumentException("Reserved device name: " + component);
           }
       }
   }`
   
   
   @garydgregory  can we use the above code to avoid path injection in user provided paths in windows machine. 
   It can further be optimized for other OS


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "Philzen (via GitHub)" <gi...@apache.org>.
Philzen commented on PR #553:
URL: https://github.com/apache/commons-io/pull/553#issuecomment-2074997446

   @elharo @garydgregory i totally see the points you've raised and agree it may be worth tackling the current behaviour in a bugfix.
   
   However, until that happens, i believe it will be worthwhile to sync the JavaDoc with the current behavior to minimize bad surprises when consuming the library.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #553:
URL: https://github.com/apache/commons-io/pull/553#discussion_r1441847412


##########
src/main/java/org/apache/commons/io/FilenameUtils.java:
##########
@@ -669,7 +669,7 @@ public static String getFullPath(final String fileName) {
      * ~            --&gt; ~
      * ~/           --&gt; ~
      * ~user        --&gt; ~user
-     * ~user/       --&gt; ~user
+     * ~user/       --&gt; ~user/

Review Comment:
   The existing javadoc and API is very unclear. I do not understand "Gets the full path from a full fileName, which is the prefix + path, and also excluding the final directory separator."
   
   The argument is not a filename. There is no such thing as a "full path" in normal parlance. Bug filed. https://issues.apache.org/jira/browse/IO-836
   
   I also agree that if this method turns ~user/ into ~user/ that's a bug. It should produce ~user.
   
   C:\ should also (80% certainty) turn into C:, not C:\
   
   I further expect there are additional bugs in this method involving Unix shell conventions and platform dependencies, but I'd have to dig into the rest of the class to understand exactly what those are.
   
   What a tasty can of worms you've opened. :-)
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description of FilenameUtils#getFullPathNoEndSeparator(String) to reflect correct named user result with trailing slash [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #553:
URL: https://github.com/apache/commons-io/pull/553#issuecomment-2075044939

   Hello @Philzen 
   It looks to me like git master already contains the Javadoc change. What about all the callers of the API?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description to reflect correct named user result with trailing slash [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #553:
URL: https://github.com/apache/commons-io/pull/553#discussion_r1441830149


##########
src/main/java/org/apache/commons/io/FilenameUtils.java:
##########
@@ -669,7 +669,7 @@ public static String getFullPath(final String fileName) {
      * ~            --&gt; ~
      * ~/           --&gt; ~
      * ~user        --&gt; ~user
-     * ~user/       --&gt; ~user
+     * ~user/       --&gt; ~user/

Review Comment:
   This causes the Javadoc to contradict itself with the above's `excluding the final directory separator`.
   The question is whether Windows's `"C:\"` unchanged output is a special case or a bug.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Amend description to reflect correct named user result with trailing slash [commons-io]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #553:
URL: https://github.com/apache/commons-io/pull/553#discussion_r1441830149


##########
src/main/java/org/apache/commons/io/FilenameUtils.java:
##########
@@ -669,7 +669,7 @@ public static String getFullPath(final String fileName) {
      * ~            --&gt; ~
      * ~/           --&gt; ~
      * ~user        --&gt; ~user
-     * ~user/       --&gt; ~user
+     * ~user/       --&gt; ~user/

Review Comment:
   This causes the Javadoc to contradict itself with the above's `excluding the final directory separator`.
   The question is whether Windows's `"C:\"` unchanged output is a special case or a bug.
   CC: @elharo 
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org