sandboxed-open guard misses from-import aliases, pathlib methods, and reuses "secured" names across functions
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():
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.
from gzip import open(same for bz2/lzma/io/codecs) comes through as a bareast.Name, and only the nameopenis checked.Path.open/read_text/write_text/read_bytes/write_bytesaren't covered._secured_names()collects names across the whole module, so a name assigned frompathsec_openin 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.pyval_load/val_dumprunvalidate_path(db), but the files actually opened aredb + ".db"and the dbm siblings.app/wordnet_app.pyoverrideslog_messageand writes the raw request line to the logfile, skipping the control-character escaping that the default does.
Source: nltk/nltk