Skip to content

Commit b82f06b

Browse files
authored
Add param to load_memory_cache to stop pkl files without explicit loading (#9055)
1 parent c542bb6 commit b82f06b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

dspy/clients/cache.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ def save_memory_cache(self, filepath: str) -> None:
170170
with open(filepath, "wb") as f:
171171
cloudpickle.dump(self.memory_cache, f)
172172

173-
def load_memory_cache(self, filepath: str) -> None:
173+
def load_memory_cache(self, filepath: str, allow_pickle: bool = False) -> None:
174+
if not allow_pickle:
175+
raise ValueError("Loading untrusted .pkl files can run arbitrary code, which may be dangerous. \
176+
Set `allow_pickle=True` to load if you are running in a trusted environment and the file is from a trusted source.")
177+
174178
if not self.enable_memory_cache:
175179
return
176180

tests/clients/test_cache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ def test_save_and_load_memory_cache(cache, tmp_path):
189189
memory_max_entries=100,
190190
)
191191

192-
# Load the memory cache
193-
new_cache.load_memory_cache(str(temp_cache_file))
192+
# Load the memory cache without allowing pickle (default)
193+
with pytest.raises(ValueError):
194+
new_cache.load_memory_cache(str(temp_cache_file))
195+
196+
# Load the memory cache with allow_pickle=True
197+
new_cache.load_memory_cache(str(temp_cache_file), allow_pickle=True)
194198

195199
# Verify items are in the new memory cache
196200
for req in requests:

0 commit comments

Comments
 (0)