Skip to content

Resolve a trailing . or .. segment in FileUtils.normalize - #366

Merged
slachiewicz merged 1 commit into
codehaus-plexus:masterfrom
kakkar87d:normalize-trailing-dot-segment
Nov 19, 2026
Merged

slachiewicz merged 1 commit into
codehaus-plexus:masterfrom
kakkar87d:normalize-trailing-dot-segment

Conversation

@kakkar87d

Copy link
Copy Markdown
Contributor

What does this change?

FileUtils.normalize only collapses /./ and /../, so a dot segment at the end of the path is never resolved. normalize("/foo/bar/..") returns /foo/bar/.., and normalize("/foo/../..") returns /.. although the javadoc promises null once the ..s go past the root (/foo/../../, with the trailing slash, already gives null). A caller that uses the result for a containment check, normalize(root + userPath).startsWith(root), accepts userPath = "a/../.." even though the path names the parent of root (CWE-22). Some downstream code works around it by appending a slash before the call, for example agave's Local.resolvePath.

Rooted paths that end in /. or /.. now get the trailing slash the existing loops look for, and it is dropped again afterwards. Tomcat's RequestUtil.normalize has 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 passes startsWith(root) before the change. After it the result is /srv, which fails the check. The five new rows in FileUtilsTest.normalize fail without the fix and pass with it, and mvn verify javadoc:javadoc is green on JDK 8, 21 and 25 (macOS).

Related issue

None.

Anything reviewers should look at closely?

  • Behaviour change: /foo/. now returns /foo, /foo/bar/.. returns /foo, /foo/.. returns /, and /.. and /foo/../.. return null. Inputs that do not end in a dot segment give the same result as before.
  • Only paths starting with / get the new step. A relative path such as a/.. is returned unchanged, because resolving it would hit the existing lastIndexOf branch that throws StringIndexOutOfBoundsException for a .. with no / in front of it (normalize("a/../b") already throws today). Drive-letter paths like C:/foo/.. are unchanged too.

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.
@slachiewicz
slachiewicz self-requested a review November 19, 2026 11:28
@slachiewicz
slachiewicz merged commit f28e4e4 into codehaus-plexus:master Nov 19, 2026
9 checks passed
@slachiewicz slachiewicz added the bug Something isn't working label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants