Repository navigation
Resolve a trailing . or .. segment in FileUtils.normalize - #366
Merged
slachiewicz merged 1 commit intoNov 19, 2026
Merged
Conversation
normalize only collapsed "/./" and "/../", so a path ending in "/." or "/.." came back unresolved: "/foo/bar/.." stayed as is and "/foo/../.." became "/.." instead of null. A prefix check on the result then accepts a path that names a parent of the prefix. Give rooted paths that end in a dot segment the trailing slash the loops look for, as Tomcat's RequestUtil.normalize does, and drop it again afterwards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this change?
FileUtils.normalizeonly collapses/./and/../, so a dot segment at the end of the path is never resolved.normalize("/foo/bar/..")returns/foo/bar/.., andnormalize("/foo/../..")returns/..although the javadoc promisesnullonce the..s go past the root (/foo/../../, with the trailing slash, already givesnull). A caller that uses the result for a containment check,normalize(root + userPath).startsWith(root), acceptsuserPath = "a/../.."even though the path names the parent ofroot(CWE-22). Some downstream code works around it by appending a slash before the call, for example agave'sLocal.resolvePath.Rooted paths that end in
/.or/..now get the trailing slash the existing loops look for, and it is dropped again afterwards. Tomcat'sRequestUtil.normalizehas the same step; the code here matches the older version of that method.Repro: with
root = "/srv/repo/",FileUtils.normalize(root + "a/../..")returns/srv/repo/..and passesstartsWith(root)before the change. After it the result is/srv, which fails the check. The five new rows inFileUtilsTest.normalizefail without the fix and pass with it, andmvn verify javadoc:javadocis green on JDK 8, 21 and 25 (macOS).Related issue
None.
Anything reviewers should look at closely?
/foo/.now returns/foo,/foo/bar/..returns/foo,/foo/..returns/, and/..and/foo/../..returnnull. Inputs that do not end in a dot segment give the same result as before./get the new step. A relative path such asa/..is returned unchanged, because resolving it would hit the existinglastIndexOfbranch that throwsStringIndexOutOfBoundsExceptionfor a..with no/in front of it (normalize("a/../b")already throws today). Drive-letter paths likeC:/foo/..are unchanged too.