by westurner · 2y ago · 0 comments
In the standard library, the @functools.cache and @functools.cached_property function and method decorators do LRU caching in RAM only. https://docs.python.org/3/library/functools.html
Dask docs > "Automatic Opportunistic Caching": https://docs.dask.org/en/stable/caching.html#automatic-oppor... ; dask.cache.Cache(size_bytes:int) ... "Cache tasks, not expressions"
Pickles are not a safe way to deserialize data; there is not a data only pickling protocol.
So caching arbitrary cell objects (or e.g. stack frames) to disk, as pickles at least, creates risk of code injection if the serialized data contains executable code objects.
Similarly, the file permissions on e.g. rr traces. https://en.wikipedia.org/wiki/Rr_(debugging)
Dataclasses in the standard library helps with object serialization, but not with caching cell outputs containing code objects.
Apache Arrow and Parquet also require schema for efficient serialization.
LRU: Least-Recently Used
MRU: Most-Recently Used; most frequently accessed
Out-of-order execution in notebooks may or may not have wasted cycles of human time and CPU time. If the prompt numbers aren't sequential, what you ran in a notebook is not necessarily what others will get when running that computation graph in order; and so it's best practice to "Restart and Run All" to test the actual control flow before committing or pushing.
There are ways to run and test notebooks in order on or before git commit (and compare their output with the output from the previous run) like software with tests.
Yes, pickle can be exploited by someone with ~/ write access. But you know what else someone with that access do? Append to .bashrc, add to desktop startup script, create pth file in local python dist, create usercustomize, append code into any locally installed libraries, add code to config of many editors...
I've seen so much effort wasted because people rejected pickle for being insecure in contexts where it doesn't matter at all.
We were talking about jupyter notebooks, which are typically used for exploratory work, and thus are executed either on local desktop under default user account, or in containers with pretty open access controls. My point was that under those circumstances, it is OK to assume any cache data is trusted.
Your reply somehow talks about secured containers and privilege escalation but I cannot figure out how is it connected to running Jupyter on local laptop, nor the threat model you are worried about.
Should the app write additional .py files next to itself and exec those?
That's what caching code blocks as pickles on disk would get you there.
E.g. dask-labextension does not implicitly do dask.cache for you.
How are the objects serialized, and are code objects serialized to files on disk, what is the permission umask on such files, and what directory (/var/cache) should they be selinux-labeled in (when it is running code from not memcache instead of the source) because if you can write to those cache files, you control the execution flow of the notebook (which is already unreproducibly out-of-ordered without consideration)