Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Auto mode: ask before PyYAML deserialization that can run code (#8432)

Daniel Han6a4595ab98fc
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index a98cf7c4f9d..124eb03a4f1 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -2521,9 +2521,35 @@ _AUTO_UNSAFE_PY_ATTRS = frozenset(
"read_pickle",
}
)
-# Pickle-backed loaders that can execute code embedded in the file; gated by
-# receiver module (torch.load, joblib.load) since bare `load` is too common.
-_AUTO_UNSAFE_PY_LOAD_MODULES = frozenset({"torch", "joblib", "cloudpickle"})
+# Loaders that can execute code embedded in the data they deserialize; gated by
+# receiver module (torch.load, yaml.load) since bare `load` is too common.
+_AUTO_UNSAFE_PY_LOAD_MODULES = frozenset({"torch", "joblib", "cloudpickle", "yaml"})
+# The load entry points on those modules. yaml.load runs whatever its Loader=
+# builds, and !!python/object/apply in the data is a call, so it asks like the
+# pickle-backed ones do. yaml.safe_load is untouched.
+_AUTO_UNSAFE_PY_LOAD_ATTRS = frozenset({"load", "load_all"})
+# Loader classes: the same deserialize one level down (yaml.Loader(s).get_data())
+# and what a custom loader subclasses. Ordinary words, so matched by receiver.
+_AUTO_UNSAFE_PY_LOAD_CLASSES = frozenset({"Loader", "Constructor"})
+# Names no other library uses, so they are matched wherever they appear rather
+# than by receiver: an alias, a subclass, a loop, a helper or a factory all name
+# one of these somewhere, and following the value through every binding form is
+# a game without an end.
+_AUTO_UNSAFE_YAML_LOADERS = frozenset(
+ {
+ "unsafe_load",
+ "unsafe_load_all",
+ "full_load",
+ "full_load_all",
+ "UnsafeLoader",
+ "CUnsafeLoader",
+ "FullLoader",
+ "CFullLoader",
+ "CLoader",
+ "UnsafeConstructor",
+ "FullConstructor",
+ }
+)
# Writer methods that persist to disk without going through open() (numpy.save,
# Image.save, plt.savefig, DataFrame.to_csv, json.dump). Gated as method calls
# only, so a bare attribute reference is not mistaken for a write.
@@ -3922,6 +3948,58 @@ def _python_is_potentially_unsafe(code: str) -> bool:
and _wraps_write_callable(_default.args[0])
):
dynamic_aliases.add(_param.arg) # def f(w=partial(open, mode="w"))
+ # Naming a code-executing loader asks, wherever the name appears. Presence
+ # rather than dataflow: a loader can be aliased, subclassed, packed into a
+ # container, returned from a helper or picked by a conditional, and following
+ # it through all of those is a game without an end. A safe read names none of
+ # these, so yaml.safe_load and json.load are unaffected.
+ _module_names = set(_AUTO_UNSAFE_PY_LOAD_MODULES) # receivers: yaml.load
+ _bare_names = set(_AUTO_UNSAFE_YAML_LOADERS) # loaders named on their own
+ _imported_modules = set() # only the module itself, for the return rule
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Import):
+ for alias in node.names:
+ _root = alias.name.split(".")[0]
+ if _root in _AUTO_UNSAFE_PY_LOAD_MODULES:
+ _imported_modules.add(alias.asname or _root)
+ elif isinstance(node, ast.ImportFrom):
+ _root = (node.module or "").split(".")[0]
+ for alias in node.names:
+ if alias.name in _AUTO_UNSAFE_YAML_LOADERS or (
+ _root in _AUTO_UNSAFE_PY_LOAD_MODULES
+ and (
+ alias.name in _AUTO_UNSAFE_PY_LOAD_ATTRS
+ or alias.name in _AUTO_UNSAFE_PY_LOAD_CLASSES
+ )
+ ):
+ _bare_names.add(alias.asname or alias.name)
+ elif _root in _AUTO_UNSAFE_PY_LOAD_MODULES:
+ # from yaml import loader as yl, so yl.Loader still reads as one.
+ _module_names.add(alias.asname or alias.name)
+ _module_names |= _imported_modules
+
+ def _loader_receiver(node) -> bool:
+ # yaml, a submodule of it (yaml.loader.Loader), or an import alias.
+ while isinstance(node, ast.Attribute):
+ node = node.value
+ return isinstance(node, ast.Name) and node.id in _module_names
+
+ for node in ast.walk(tree):
+ if isinstance(node, ast.Attribute):
+ if node.attr in _AUTO_UNSAFE_YAML_LOADERS:
+ return True
+ if (
+ node.attr in _AUTO_UNSAFE_PY_LOAD_ATTRS or node.attr in _AUTO_UNSAFE_PY_LOAD_CLASSES
+ ) and _loader_receiver(node.value):
+ return True
+ elif isinstance(node, ast.Name) and node.id in _bare_names:
+ return True
+ # Handing the module out of a function hands out every loader on it, and
+ # the caller's name for it cannot be seen from here.
+ elif isinstance(node, (ast.Return, ast.Lambda)):
+ _out = node.value if isinstance(node, ast.Return) else node.body
+ if isinstance(_out, ast.Name) and _out.id in _imported_modules:
+ return True
try:
for node in ast.walk(tree):
if isinstance(node, ast.Import):
diff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.py
index ab3f6cf4322..74f5a195f9a 100644
--- a/studio/backend/tests/test_permission_mode.py
+++ b/studio/backend/tests/test_permission_mode.py
@@ -1678,6 +1678,30 @@ def test_high_risk_dispatcher_non_terminal():
("from builtins import open as w\nw('x', 'w')", True),
("globals()['open']('x', 'w')", True), # dynamic open lookup
("import pickle\npickle.loads(b'')", True), # code exec on load
+ # PyYAML's non-safe loaders build arbitrary Python objects from tags
+ # (!!python/object/apply:os.system), so the command lives in the data.
+ ("import yaml\nyaml.load(s, Loader=yaml.Loader)", True),
+ ("import yaml\nyaml.unsafe_load(s)", True),
+ ("from yaml import unsafe_load\nunsafe_load(s)", True),
+ ("import yaml\nyaml.full_load(s)", True),
+ ("import yaml\nyaml.Loader(s).get_data()", True), # the loader class itself
+ ("import yaml.loader\nyaml.loader.Loader(s).get_data()", True), # through a submodule
+ ("from yaml import loader as yl\nyl.Loader(s).get_data()", True), # submodule alias
+ ("import yaml\nclass L(yaml.Loader): pass\nL(s).get_data()", True), # a subclass
+ # Naming one is enough, so the ways of moving it around need no tracking.
+ ("import yaml\nld = yaml.unsafe_load\nld(s)", True),
+ ("import yaml\nh.loader = yaml.unsafe_load\nh.loader(s)", True),
+ ("import yaml\nfor fn in [yaml.unsafe_load]: fn(s)", True),
+ ("import yaml\ndef choose(): return yaml.unsafe_load\nchoose()(s)", True),
+ ("import yaml\ndef get(): return yaml\nget().unsafe_load(s)", True), # the module
+ # The safe readers name none of those, so they still run unprompted.
+ ("import yaml\nprint(yaml.safe_load(open('c.yml')))", False),
+ ("import yaml\nfor d in yaml.safe_load_all(open('c.yml')): print(d)", False),
+ ("from yaml import safe_load\nprint(safe_load(open('c.yml')))", False),
+ ("import yaml\ncfg = yaml.safe_load(open('c.yml'))\nprint(cfg['lr'])", False),
+ ("import yaml\ndef read(q): return yaml.safe_load(open(q))\nprint(read('a.yml'))", False),
+ ("import yaml\nprint(yaml.dump({'a': 1}))", True), # dump is a writer, as before
+ ("import json\nprint(json.load(open('a.json')))", False), # json.load is data
("import io\nio.FileIO('out', 'w')", True), # raw write handle
(
"import zipfile\nprint(zipfile.ZipFile('a').open('n.txt', 'r'))",