SSerguey Asael Shinder
Java coding notes: the JVM, and writing software that lasts

Serguey Asael Shinder: Lexical or canonical: Maven kept a path by trusting the string, NLTK lost one

· by Serguey Asael Shinder / Serguey Shinder

Two path fixes published in the same week look as if they contradict each other.

In Maven 3.10.1, released on 15 November, pull request 13650 changed how the project root, multiModuleProjectDirectory, is computed. It used to go through File#getCanonicalFile(), which asks the operating system and can follow a symlink, a junction or a mapped network drive. On Windows with JDK 25, a drive H: mapped to a network share came back as H:\colo-01-fs02\..., a path that does not exist, and every module path built on it broke (issue 11167). The fix normalises the path lexically: it tidies the string the user gave and does not ask the OS where it leads.

On 13 November GitHub published GHSA-qg35-44qx-7vrr for NLTK. Its YCOECorpusReader builds two nested readers under psd and pos inside a corpus. The containment check in FileSystemPathPointer.join() is, in the advisory's words, purely lexical and never resolves symlinks. So a directory symlink named psd inside a corpus passed the check and anchored the reader outside the corpus root, and file contents from elsewhere came back through the normal API.

One project moved from canonical to lexical to fix a bug; the other was vulnerable because it was lexical. Both are correct, because they were answering different questions.

Serguey Asael Shinder: Lexical or canonical: Maven kept a path by trusting the string, NLTK lost one
Lexical or canonical: Maven kept a path by trusting the string, NLTK lost one — Serguey Asael Shinder

Maven needed to remember a place. The user worked under H:\work\..., and every later path should be built from that same name. Asking the OS for the canonical form replaced the user's name for the place with the OS's current opinion of it, and that opinion changed with the JDK. A lexical normalisation keeps the identity the user chose.

NLTK needed to enforce a boundary. The question there is not what the path is called but where reading it will actually go, and only the file system knows that. A check on the string answers a question the attacker controls.

In Java the two operations sit side by side. Path.normalize()) removes redundant name elements and, in its own words, does not access the file system; I wrote about what that means for security checks in October. toRealPath() asks the file system and resolves links. Neither is the safe choice in general.

The rule I take from this week is to decide first which question the code is asking. If it is "what did the user mean", keep the string and normalise it lexically. If it is "may this read happen", resolve the real path at the moment of the check and compare that. Mixing them up produces either Maven's bug, a correct path turned into a wrong one, or NLTK's, a wrong path accepted as a correct one.