Skip to content

fix: Replace diskcache with diskcache-community's SafeDisk (CVE-2025-69872) - #2001

Open
avgvi wants to merge 1 commit into
IBM:mainfrom
avgvi:fix/diskcache-safedisk
Open

avgvi wants to merge 1 commit into
IBM:mainfrom
avgvi:fix/diskcache-safedisk

Conversation

@avgvi

@avgvi avgvi commented Nov 9, 2026 •

Copy link
Copy Markdown

Summary

InferenceEngine.prepare() opens a diskcache Cache in ./inference_engine_cache/<engine class>/ (prepare(), default path), and infer() looks up each instance there before calling the model (L275). The default Disk passes any pickled entry to pickle.load, so a file dropped into that folder runs code in the evaluation process. That is CVE-2025-69872 (GHSA-w8v5-vhqr-4h9v); diskcache stopped at 5.6.3 in 2023, which is affected, and scanners flag every unitxt install for it (issue 1972).

Fixes #2000

Changes

  • Dependency: diskcache-community>=5.8.4 replaces diskcache in the core dependencies and the tests, inference-tests and remote_inference extras, and in LiteLLMInferenceEngine._requirements_list.
  • Disk: the inference cache uses SafeDisk.with_types(TextGenerationInferenceOutput).with_options(on_error="miss"), since predictions are plain values or that class; a refused entry is deleted and the model is asked again. infer() already reads with one get().
  • text2sql: its Cache gets plain SafeDisk for the SQL API's JSON results, so a refused entry raises UnpicklingError there.
  • Test: test_execute_query_does_not_unpickle_cached_result plants a pickle under a query's cache key and checks that execute_query neither runs it nor posts to the API.
  • conda-forge: the unitxt recipe lists diskcache, and conda-forge has no diskcache-community package yet, so the recipe needs one first.

Update

Every day new libraries getting updated, as example several libraries that were updated yesterday lithops and octotools.

improved performance

Measured over 20 rounds per build, each build in its own venv in random order, medians vs PyPI diskcache 5.6.3 — classic mix (100,000 ops/round, 90% gets / 9% sets / 1% deletes) plus size tiers (100 B to 1 MB) and Django backends, millions of ops total.

Operation vs diskcache 5.6.3
Cache.get hit ~17% faster
Cache.get miss ~12% faster
DjangoCache.get hit ~16% faster
Cache.set ~12–23% faster, best case
Cache.set with expire / Django set ~7–11% faster best case

Before and after

The script fakes the Ollama client, then overwrites the cached prediction with a pickle that calls planted(), a function in the script that prints one line and answers 'Toronto'.

On unitxt 1.26.10 with diskcache 5.6.3:

1. first inference: ['Ottawa'] model calls: 1
2. same instance again, served from the cache: ['Ottawa'] model calls: 1
3. tampered entry
  code from the cache directory ran in this process
  prediction: ['Toronto'] model calls: 1

With this change:

1. first inference: ['Ottawa'] model calls: 1
2. same instance again, served from the cache: ['Ottawa'] model calls: 1
3. tampered entry
treating an unreadable entry in './inference_engine_cache/OllamaInferenceEngine' as missing: UnpicklingError: SafeDisk refuses to unpickle __main__.planted
  prediction: ['Ottawa'] model calls: 2
Script
import os
import shutil
import tempfile
from unittest.mock import patch

try:
    import diskcache_community as writer_package
except ModuleNotFoundError:
    import diskcache as writer_package  # unitxt 1.26.10

os.environ["UNITXT_DEFAULT_VERBOSITY"] = "error"
from unitxt.inference import OllamaInferenceEngine

# unitxt keeps the cache in ./inference_engine_cache/ under the directory it
# runs from; run in an empty temporary one.
workdir = tempfile.mkdtemp()
os.chdir(workdir)
engine_cache = os.path.join("inference_engine_cache", "OllamaInferenceEngine")

model_calls = []


class FakeOllamaClient:
    def __init__(self, host=None):
        pass

    def chat(self, messages, model, options):
        model_calls.append(messages)
        return {"message": {"content": "Ottawa"}}


def planted():
    print("  code from the cache directory ran in this process")
    return "Toronto"


class Planted:
    def __reduce__(self):
        return (planted, ())


dataset = [
    {
        "source": "What is the capital of Canada? Answer in one word.",
        "data_classification_policy": ["public"],
    }
]

with patch("ollama.Client", FakeOllamaClient):
    engine = OllamaInferenceEngine(model="llama3.2:1b")
    print("1. first inference:", list(engine.infer(dataset)), "model calls:", len(model_calls))
    print("2. same instance again, served from the cache:", list(engine.infer(dataset)), "model calls:", len(model_calls))

    print("3. tampered entry")
    with writer_package.Cache(engine_cache) as other_writer:
        (key,) = list(other_writer)
        other_writer.set(key, Planted())
    print("  prediction:", list(engine.infer(dataset)), "model calls:", len(model_calls))

os.chdir(os.path.dirname(workdir))
shutil.rmtree(workdir)

Test plan

On macOS with Python 3.12:

  • python -m pytest tests/library/test_text2sql_utils.py: 23 passed, the new test among them.
  • With the default Disk back in text2sql_utils.py, the new test fails because the planted code runs.
  • ruff check and ruff format --check on the changed files: no new findings.

…69872)

Signed-off-by: August Vi <augustvilakia@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inference engine cache entries are unpickled on read (diskcache, CVE-2025-69872)

1 participant