GitHub advisory · PyPI
NLTK: Symlink escape in CorpusReader allows arbitrary local file read outside the corpus root
Description
nltk.corpus.reader.api.CorpusReader.open() can be used to read files outside the intended corpus root via a symlink placed inside that root. Although NLTK blocks absolute paths and .. traversal, the current boundary check is only lexical and does not account for symlink resolution. This leads to an arbitrary local file read / filesystem sandbox bypass for applications that rely on CorpusReader or FileSystemPathPointer to restrict file access.
The vulnerable flow is:
CorpusReader.open() blocks absolute paths and .., then calls self._root.join(file).open()FileSystemPathPointer.join() joins the requested file ID and checks whether the resulting path still appears to remain under the configured rootThe problem is that the check is based on the lexical path after os.path.normpath(), not on the resolved path after following symlinks.
Current behavior:
CorpusReader.open() rejects:
.. path traversalFileSystemPathPointer.join() computes:
joined = os.path.normpath(os.path.join(self._path, fileid))root = os.path.normpath(self._path)joined starts with rootThis misses the case where a path stays inside the root lexically, but resolves outside the root via a symlink already present under the allowed directory.
Example:
JOINED=/tmp/nltk-root/link/secret.txt
REALPATH=/tmp/outside/secret.txt
JOINED still appears to be inside the root, but REALPATH is outside it.
This is distinct from simple ../ traversal:
..Reproduced in an isolated Docker sandbox using the local nltk clone.
Minimal…
Manual fix · PyPI
pip install --upgrade "nltk>=3.9.4"Sources