#3920·nltk

sandboxed-open guard misses from-import aliases, pathlib methods, and reuses "secured" names across functions

Author: ZainnQureshiiCreated Sep 25, 2026Updated Sep 25, 2026

Following up on @alvations's suggestion in #3918 to scan for security gaps instead of small fixes. I ran a scan (Claude Code, Claude Opus 5.5) over the pathsec/picklesec/termsec/xmlsec paths. The runtime guards held up on everything I tried: fileid traversal, absolute fileids, file: URLs, .. resource paths and readers rooted at /etc all raised. The gaps I did find are in the CI guard, tools/check_no_unsandboxed_open.py, not in shipped code. Nothing in nltk/ uses these patterns today, so this is about keeping the guard from letting one in later.

A throwaway file and the guard's own find_violations():

python
from gzip import open as gopen
from pathlib import Path
import gzip, shutil
from nltk.pathsec import open as pathsec_open

def a(p): return gopen(p)               # not flagged
def b(p): return Path(p).read_text()    # not flagged
def c(p): Path(p).write_text("x")       # not flagged
def d(p): shutil.copy(p, "/tmp/x")      # not flagged
def e(p, q):
    fh = pathsec_open(q)
    return fh
def f(p):
    fh = p
    return gzip.open(fh)                # not flagged: "fh" counts as secured because of e()
def g(p): return gzip.open(p)           # flagged (control)

Only g is reported.

  1. from gzip import open (same for bz2/lzma/io/codecs) comes through as a bare ast.Name, and only the name open is checked.
  2. Path.open / read_text / write_text / read_bytes / write_bytes aren't covered.
  3. _secured_names() collects names across the whole module, so a name assigned from pathsec_open in one function makes the same name look safe everywhere.

Possible fix: resolve from X import open as Y aliases into _PATH_TAKING, flag the pathlib I/O methods, and scope secured names per function. Plus teeth tests for each case, like the ones in #3904. Happy to send a PR if you think it's worth doing, or to leave it if it's already on your list.

Two smaller things I noticed but didn't reproduce, in case they're useful:

  • sem/chat80.py val_load/val_dump run validate_path(db), but the files actually opened are db + ".db" and the dbm siblings.
  • app/wordnet_app.py overrides log_message and writes the raw request line to the logfile, skipping the control-character escaping that the default does.