diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f7d864d7..84e88a673 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 every `scan_interval_seconds` while the worker is busy too, not only between drains. +### Security + +- **The `[nltk]` extra now pins NLTK 3.10.4** (was 3.9.4). 3.9.4's + `PorterStemmer.stem()` can be driven into a slow path by a crafted word + (PYSEC-2026-3738, fixed in 3.10.3), and the `nltk` tokenizer stems every + token of ingested text. The two releases tokenize alike except for a word + right after an opening quote (`'cause`), which 3.9.4 dropped: 449 of + 83,484 LoCoMo and LongMemEval texts change. Turn-level BM25 over the 1,977 + LoCoMo questions with evidence is unchanged (Hit@5 60.09% -> 60.04%, MRR + 0.4600 -> 0.4600). + +### Upgrade + +- A store indexed with `[tokenizer] name = "nltk"` should run + `everos cascade rebuild` after upgrading, so stored tokens and query + tokens come from the same NLTK release. On Linux and macOS, NLTK 3.10 also + refuses data another account could write: if the `nltk` tokenizer now + reports its data missing, run `chmod -R go-w` on the data directory the + error names. The default `jieba` tokenizer is unaffected. + ## [1.7.1] - 2026-11-20 EverOS 1.7.1 writes each conversation's memory in its own language: a short diff --git a/Makefile b/Makefile index 7accaa1a5..dea232d9a 100644 --- a/Makefile +++ b/Makefile @@ -27,10 +27,13 @@ help: # Sync deps from uv.lock; CI calls this directly. --frozen means "lock is the # source of truth — fail rather than update it". Then fetch the NLTK data the -# nltk tokenizer reads, into the venv where NLTK looks for it. +# nltk tokenizer reads, into the venv where NLTK looks for it, private to its +# owner: NLTK >= 3.10 will not read data another account could write, which a +# group-writable checkout (umask 002) would otherwise give it. install-deps: uv sync --frozen uv run --frozen python -m nltk.downloader -q -e -d .venv/nltk_data punkt_tab stopwords + chmod -R go-w .venv/nltk_data # One-stop dev setup: deps + pre-commit hooks (both pre-commit and commit-msg # stages — gitlint runs on commit-msg). diff --git a/docs/configuration.md b/docs/configuration.md index 851790a62..bbb819f5c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -132,6 +132,12 @@ pip install "everos[nltk]" python -m nltk.downloader punkt_tab stopwords ``` +On Linux and macOS, NLTK 3.10 and later will not read data that another +account could write (a group or world write bit, or another owner), and that +data then reads as missing. Keep the data directory private to its owner +(`chmod -R go-w `); the startup error names the directory when this is +the cause. + The LanceDB keyword index lowercases, Snowball-stems and ASCII-folds tokens on both the indexed and the query side, so on that backend `nltk` tokens are stemmed a second time; matching stays symmetric. diff --git a/pyproject.toml b/pyproject.toml index 3a097d0ce..e959ddcdf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -121,10 +121,12 @@ otel = [ "opentelemetry-exporter-otlp-proto-http>=1.27.0", ] milvus = ["pymilvus>=3.0.0"] -# NLTK keyword tokenizer (`[tokenizer] name = "nltk"`). Pinned to the release the -# EverAlgo LoCoMo harness used: tokens change across NLTK releases, and the -# indexed side and the query side must produce the same ones. -nltk = ["nltk==3.9.4"] +# NLTK keyword tokenizer (`[tokenizer] name = "nltk"`). Pinned exactly: tokens +# change across NLTK releases, and the indexed side and the query side must +# produce the same ones. 3.10.4 fixes a PorterStemmer DoS (PYSEC-2026-3738) on +# the path every token takes; a store indexed under another release needs +# `everos cascade rebuild`. +nltk = ["nltk==3.10.4"] [project.urls] Homepage = "https://evermind.ai" @@ -566,6 +568,6 @@ dev = [ "opentelemetry-exporter-otlp-proto-http>=1.27.0", "pymilvus>=3.0.0", # The NLTK tokenizer tests must run, not skip, so the [nltk] extra is present too. - "nltk==3.9.4", + "nltk==3.10.4", "hypothesis>=6.100", ] diff --git a/src/everos/component/tokenizer/nltk_provider.py b/src/everos/component/tokenizer/nltk_provider.py index d6877d1c7..dc3811e24 100644 --- a/src/everos/component/tokenizer/nltk_provider.py +++ b/src/everos/component/tokenizer/nltk_provider.py @@ -14,13 +14,16 @@ * The ``punkt_tab`` and ``stopwords`` NLTK data must already be installed. Nothing is downloaded at runtime: a missing resource fails at construction. * The token stream depends on the NLTK release, so the ``[nltk]`` extra pins - the one the harness used. + one exactly. It is 3.10.4, newer than the harness's 3.9.4: it fixes + a PorterStemmer denial of service, and the two differ only on a word right + after an opening quote (``'cause``), which 3.9.4 dropped. """ from __future__ import annotations __all__ = ["NltkTokenizer"] +import os from collections.abc import Sequence from typing import Final @@ -78,12 +81,30 @@ def _require_data() -> None: CapabilityError: Naming every missing resource and the install command. """ missing = [name for name, path in _REQUIRED_DATA.items() if not _has(path)] - if missing: + if not missing: + return + # On POSIX, NLTK >= 3.10 skips data another account could write (a group or + # world write bit, or a foreign owner), so data that is there reads as + # missing; say so instead of asking for a download that would not help. + refused = sorted( + {root for root in nltk.data.path for n in missing if _on_disk(root, n)} + ) + if refused: raise CapabilityError( - f"The nltk tokenizer needs NLTK data that is not installed: " - f"{', '.join(missing)}. Install with: " - f"python -m nltk.downloader {' '.join(missing)}" + f"The nltk tokenizer found NLTK data ({', '.join(missing)}) under " + f"{', '.join(refused)} but NLTK will not read it: another account " + "could write it. Make it private to its owner: " + f"chmod -R go-w {' '.join(refused)}" ) + raise CapabilityError( + f"The nltk tokenizer needs NLTK data that is not installed: " + f"{', '.join(missing)}. Install with: " + f"python -m nltk.downloader {' '.join(missing)}" + ) + + +def _on_disk(root: str, name: str) -> bool: + return os.path.exists(os.path.join(root, *_REQUIRED_DATA[name].split("/"))) def _has(resource: str) -> bool: diff --git a/tests/unit/test_component/test_tokenizer/test_nltk.py b/tests/unit/test_component/test_tokenizer/test_nltk.py index 9b51353d3..a1ef4310d 100644 --- a/tests/unit/test_component/test_tokenizer/test_nltk.py +++ b/tests/unit/test_component/test_tokenizer/test_nltk.py @@ -10,13 +10,15 @@ from __future__ import annotations +import os import sys import warnings +from pathlib import Path import nltk import pytest -from everos.component.tokenizer import build_tokenizer +from everos.component.tokenizer import build_tokenizer, nltk_provider from everos.component.tokenizer.nltk_provider import NltkTokenizer from everos.config import TokenizerSettings from everos.core.errors import CapabilityError @@ -51,6 +53,17 @@ def test_tokens_match_the_everalgo_harness(text: str, expected: list[str]) -> No assert NltkTokenizer().tokenize(text) == expected +def test_a_word_after_an_opening_quote_is_kept() -> None: + """NLTK 3.10 splits a leading quote off a word; 3.9.4 dropped the word. + + The one token difference between the two releases on LoCoMo + LongMemEval + text (449 of 83,484 texts). The harness above ran 3.9.4, so it has no + such case; this pins the pinned release's behaviour. + """ + text = "Camping with my fam, 'cause nature helps." + assert NltkTokenizer().tokenize(text) == ["camp", "fam", "caus", "natur", "help"] + + def test_tokenize_batch_preserves_order() -> None: texts = ["What did Melanie paint?", "", "swimming lessons"] assert NltkTokenizer().tokenize_batch(texts) == [ @@ -69,11 +82,40 @@ def _missing(resource: str) -> None: raise LookupError(resource) monkeypatch.setattr(nltk.data, "find", _missing) + monkeypatch.setattr(nltk.data, "path", []) # nothing on disk either with pytest.raises(CapabilityError) as err: NltkTokenizer() assert "python -m nltk.downloader punkt_tab stopwords" in str(err.value) +@pytest.mark.skipif(os.name != "posix", reason="NLTK checks ownership on POSIX only") +def test_data_another_account_could_write_is_named_with_the_fix( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """NLTK >= 3.10 skips group- or world-writable data, so it reads as missing. + + The error must say why and how to fix it, not ask for a download. + """ + root = tmp_path / "nltk_data" + for resource in ("tokenizers/punkt_tab/english", "corpora/stopwords"): + (root / resource).mkdir(parents=True) + monkeypatch.setattr(nltk.data, "path", [str(root)]) + + for d in [root, *root.rglob("*")]: + d.chmod(0o775) + with ( + pytest.warns(RuntimeWarning, match="NLTK will not read"), + pytest.raises(CapabilityError) as err, + ): + nltk_provider._require_data() + assert f"chmod -R go-w {root}" in str(err.value) + assert "nltk.downloader" not in str(err.value) + + for d in [root, *root.rglob("*")]: + d.chmod(0o755) + nltk_provider._require_data() # the same data, private: accepted + + def test_the_factory_builds_nltk_when_selected() -> None: tokenizer = build_tokenizer(TokenizerSettings(name="nltk")) assert isinstance(tokenizer, NltkTokenizer) diff --git a/uv.lock b/uv.lock index 8e97673cc..9339996d4 100644 --- a/uv.lock +++ b/uv.lock @@ -682,7 +682,7 @@ requires-dist = [ { name = "jieba", specifier = ">=0.42.1,<1.0" }, { name = "lancedb", specifier = ">=0.34.0,<0.35.0" }, { name = "msvc-runtime", marker = "sys_platform == 'win32'", specifier = ">=14.44" }, - { name = "nltk", marker = "extra == 'nltk'", specifier = "==3.9.4" }, + { name = "nltk", marker = "extra == 'nltk'", specifier = "==3.10.4" }, { name = "openai", specifier = ">=1.0.0,<3" }, { name = "opentelemetry-exporter-otlp-proto-http", marker = "extra == 'otel'", specifier = ">=1.27.0" }, { name = "opentelemetry-sdk", marker = "extra == 'otel'", specifier = ">=1.27.0" }, @@ -709,7 +709,7 @@ dev = [ { name = "hypothesis", specifier = ">=6.100" }, { name = "import-linter", specifier = ">=2.0" }, { name = "ipdb", specifier = ">=0.13.13" }, - { name = "nltk", specifier = "==3.9.4" }, + { name = "nltk", specifier = "==3.10.4" }, { name = "opentelemetry-exporter-otlp-proto-http", specifier = ">=1.27.0" }, { name = "opentelemetry-sdk", specifier = ">=1.27.0" }, { name = "pre-commit", specifier = ">=4.0.0" }, @@ -1490,17 +1490,18 @@ wheels = [ [[package]] name = "nltk" -version = "3.9.4" +version = "3.10.4" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "click" }, + { name = "defusedxml" }, { name = "joblib" }, { name = "regex" }, { name = "tqdm" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/74/a1/b3b4adf15585a5bc4c357adde150c01ebeeb642173ded4d871e89468767c/nltk-3.9.4.tar.gz", hash = "sha256:ed03bc098a40481310320808b2db712d95d13ca65b27372f8a403949c8b523d0", size = 2946864, upload-time = "2026-03-24T06:13:40.641Z" } +sdist = { url = "https://files.pythonhosted.org/packages/3a/cb/bbaa9be58e95d21729c6b9194376610230e0755f6de0bf38b6acc0dce079/nltk-3.10.4.tar.gz", hash = "sha256:44679f4e9220abfd496d2c8cdb22f8425ccbe557604f9aa5cacb1c44003dabc1", size = 4300570, upload-time = "2026-10-21T11:34:35.851Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/9d/91/04e965f8e717ba0ab4bdca5c112deeab11c9e750d94c4d4602f050295d39/nltk-3.9.4-py3-none-any.whl", hash = "sha256:f2fa301c3a12718ce4a0e9305c5675299da5ad9e26068218b69d692fda84828f", size = 1552087, upload-time = "2026-03-24T06:13:38.47Z" }, + { url = "https://files.pythonhosted.org/packages/a2/04/e7816813e409ca874ba2fcc35455db0e4130b79eb277e1f9edd270ca3c69/nltk-3.10.4-py3-none-any.whl", hash = "sha256:769ba5413f265b7d3e87cd163cc28efa01e29eeef8bb6080bc907949d9fcfb51", size = 1722994, upload-time = "2026-10-21T11:34:27.975Z" }, ] [[package]]