Skip to content

IO-475: Fix UNC path prefix validation in FilenameUtils#825

Open
MiriamCohenDev wants to merge 5 commits intoapache:masterfrom
MiriamCohenDev:IO-475-fix-unc-prefix-check
Open

IO-475: Fix UNC path prefix validation in FilenameUtils#825
MiriamCohenDev wants to merge 5 commits intoapache:masterfrom
MiriamCohenDev:IO-475-fix-unc-prefix-check

Conversation

@MiriamCohenDev
Copy link

Fixes a bug in FilenameUtils#normalizeNoEndSeparator when handling UNC paths that do not end with a slash.

What:

  • The function normalizeNoEndSeparator incorrectly returned null for valid UNC paths like //server because it treated them as invalid.
  • This was caused by FilenameUtils#getPrefixLength returning -1 for UNC paths without a trailing slash.

How:

  • Updated getPrefixLength to correctly handle UNC paths without a trailing slash, returning the proper prefix length instead of -1.
  • Updated and added unit tests to reflect the correct behavior, since many existing tests assumed null would be returned for these paths.

Why:

  • Previous behavior caused valid UNC paths to be treated as invalid, which could break downstream logic that relies on normalizeNoEndSeparator.
  • This change ensures proper handling of UNC paths and maintains consistency across FilenameUtils methods.

Testing:

  • Updated tests in FilenameUtilsTest to cover UNC paths without trailing slashes.
  • Verified that all tests pass with the new logic.

Files changed:

  • src/main/java/org/apache/commons/io/FilenameUtils.java
  • src/test/java/org/apache/commons/io/FilenameUtilsTest.java

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @MiriamCohenDev

Thank you for your PR. Please see my comments. Run mvn by itself before you push and fix any issues that come up.

@garydgregory
Copy link
Member

Jira ticket is https://issues.apache.org/jira/browse/IO-475

@MiriamCohenDev MiriamCohenDev force-pushed the IO-475-fix-unc-prefix-check branch from 51f04e9 to 090b9f9 Compare January 26, 2026 14:20
@MiriamCohenDev MiriamCohenDev force-pushed the IO-475-fix-unc-prefix-check branch from 090b9f9 to 64038e1 Compare January 26, 2026 14:32
Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @MiriamCohenDev

Thank you for your updates. You missed a few spots. It's simpler to review a PR if you don't delete assertions. Instead, change them to reflect the new behavior.

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my previous comments, and run mvn without anything else and fix:

[INFO] --- checkstyle:3.6.0:check (default-cli) @ commons-io ---
[INFO] There are 16 errors reported by Checkstyle 13.0.0 with /Users/garygregory/git/commons/commons-io/src/conf/checkstyle.xml ruleset.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[940,13] (coding) FinalLocalVariable: Variable 'pos' should be declared final.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[941,16] (coding) FinalLocalVariable: Variable 'hostnamePart' should be declared final.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[942,9] (whitespace) WhitespaceAfter: 'if' is not followed by whitespace.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[942,9] (whitespace) WhitespaceAround: 'if' is not followed by whitespace.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[942,56] (whitespace) WhitespaceAround: '{' is not preceded with whitespace.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[945,9] (blocks) RightCurly: '}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally).
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[946,9] (whitespace) WhitespaceAfter: 'else' is not followed by whitespace.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[946,9] (whitespace) WhitespaceAround: 'else' is not followed by whitespace.
[ERROR] src/main/java/org/apache/commons/io/FilenameUtils.java:[946,13] (whitespace) WhitespaceAround: '{' is not preceded with whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[410,24] (whitespace) WhitespaceAfter: ',' is not followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[457,24] (whitespace) WhitespaceAfter: ',' is not followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[561,54] (whitespace) ParenPad: '(' is followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[780,21] (whitespace) ParenPad: '(' is followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[919,21] (whitespace) ParenPad: '(' is followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[992,21] (whitespace) ParenPad: '(' is followed by whitespace.
[ERROR] src/test/java/org/apache/commons/io/FilenameUtilsTest.java:[1132,21] (whitespace) ParenPad: '(' is followed by whitespace.

@MiriamCohenDev
Copy link
Author

Is that good now?

@garydgregory
Copy link
Member

The build is green and I'll check the patch later today or tomorrow.

@garydgregory
Copy link
Member

Jira ticket is https://issues.apache.org/jira/browse/IO-475

@garydgregory
Copy link
Member

Hello @MiriamCohenDev

It looks to me that there are still bugs.

The PR should include Javadoc updates with new or fixed examples.

The getPrefixLength Javadoc mentions "the length of the file name prefix" but the code is really about the length of path prefix where one usually considers the file name to be the last element of a path. See java.nio.file.Path.getFileName(). That doesn't help with the confusion ;)

A UNC name is:

\\<server-name>\<share-name>\<path>

For example: \\Server\SharedFolder\file.txt

Such that the prefix should be IMO \\Server\SharedFolder\ and not \\Server\

A shared folder can be a remote drive so it would make sense to skip the share name just like we skip a drive letter.

This would mean that our current Javadoc for \\server\a\b\c.txt -> 9 doesn't match what I am describing because a is the share name and b\c.txt is the file path.

I think we might have to leave the Extended (\\?\) UNC format for another day ;)

All of this needs careful consideration!

@MiriamCohenDev
Copy link
Author

I would be happy to better understand what you meant.
I understood from the issue that the problem is that in a path like this, for example: //server
the normalizeNoEndSeparator function returns null even though it is correct
In monitoring the function, I saw that the problem stems from the getPrefixLength function
which, in the event that the paht does not contain an additional backslash after the first two slashes, returns -1
and that is what I actually changed.
According to what you say
what should I change?
What should the function return for this routing:
//server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants