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.

studio: fail closed on HF commit-operation uploads in the sandbox gate (#8544)

Daniel Hanefbc909bef75
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 124eb03a4f1..fe52585cc63 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -11123,6 +11123,7 @@ def _check_signal_escape_patterns(code: str):
"upload_folder",
"upload_large_folder",
"create_commit",
+ "preupload_lfs_files",
}
)
# Cloud-metadata / link-local hosts.
@@ -11380,6 +11381,19 @@ def _check_signal_escape_patterns(code: str):
}
)
+ _HF_UPLOAD_PATH_VIOLATION = (
+ "HF upload path must be a sandbox-local relative-path literal "
+ "(no absolute paths, no '..' segments, no dynamic expressions)"
+ )
+
+ # Upload methods that take CommitOperation* objects rather than a path, and
+ # the kwarg each one carries them in. `preupload_lfs_files` sends the file
+ # bytes to the LFS store on its own, so it needs the same gate as a commit.
+ _HF_OPERATIONS_KWARG = {
+ "create_commit": "operations",
+ "preupload_lfs_files": "additions",
+ }
+
def _is_os_environ(node: ast.AST) -> bool:
return (
isinstance(node, ast.Attribute)
@@ -11481,14 +11495,49 @@ def _check_signal_escape_patterns(code: str):
"HF upload cannot include os.environ / os.getenv / subprocess "
"env reads; secrets and tokens must not be exfiltrated"
)
- if method_name == "create_commit":
+ if method_name in _HF_OPERATIONS_KWARG:
+ ops_kwarg = _HF_OPERATIONS_KWARG[method_name]
+ # A `*args` / `**kwargs` splat can smuggle in the operations or a token,
+ # and either may sit after `operations=`, so scan before resolving.
+ if any(isinstance(a, ast.Starred) for a in node.args or []):
+ return _HF_UPLOAD_PATH_VIOLATION
+ if any(kw.arg is None for kw in node.keywords or []):
+ return _HF_UPLOAD_PATH_VIOLATION
+ # Both methods take the operation list as their 2nd positional param.
+ operations_node: ast.AST | None = node.args[1] if len(node.args or []) > 1 else None
for kw in node.keywords or []:
- if kw.arg == "operations" and isinstance(kw.value, ast.List):
- for elt in kw.value.elts:
- if isinstance(elt, ast.Call):
- inner = _hf_upload_violation(elt, "upload_file")
- if inner:
- return inner
+ if kw.arg == ops_kwarg:
+ operations_node = kw.value
+ break
+ if operations_node is None:
+ return None # no operations -> nothing is read off disk
+ if not isinstance(operations_node, (ast.List, ast.Tuple)):
+ return _HF_UPLOAD_PATH_VIOLATION
+ for elt in operations_node.elts:
+ if not isinstance(elt, ast.Call):
+ return _HF_UPLOAD_PATH_VIOLATION
+ inner = _hf_upload_violation(elt, "commit_operation")
+ if inner:
+ return inner
+ return None
+ if method_name == "commit_operation":
+ # A CommitOperation* constructor from the list above. Delete and copy get
+ # no exemption: a by-name one would trust a name sandboxed code can rebind.
+ # Add is (path_in_repo, path_or_fileobj) so both positionals are checked,
+ # and other keywords must be literals -- a computed one reads the file.
+ path_nodes = list(node.args or [])
+ for kw in node.keywords or []:
+ if kw.arg is None:
+ return _HF_UPLOAD_PATH_VIOLATION
+ if kw.arg == "path_or_fileobj":
+ path_nodes.append(kw.value)
+ elif not isinstance(kw.value, ast.Constant):
+ return _HF_UPLOAD_PATH_VIOLATION
+ if not path_nodes:
+ return _HF_UPLOAD_PATH_VIOLATION
+ for p in path_nodes:
+ if not _path_arg_is_sandbox_local(p):
+ return _HF_UPLOAD_PATH_VIOLATION
return None
path_node: ast.AST | None = node.args[0] if node.args else None
for kw in node.keywords or []:
@@ -11496,10 +11545,7 @@ def _check_signal_escape_patterns(code: str):
path_node = kw.value
break
if not _path_arg_is_sandbox_local(path_node):
- return (
- "HF upload path must be a sandbox-local relative-path literal "
- "(no absolute paths, no '..' segments, no dynamic expressions)"
- )
+ return _HF_UPLOAD_PATH_VIOLATION
return None
class NetworkAndIoVisitor(ast.NodeVisitor):
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index 604c05e534c..5efb8c2772d 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -1622,6 +1622,237 @@ class TestHfUploadSandboxLocalPaths:
expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
)
+ def test_create_commit_operation_positional_path_absolute_blocked(self):
+ # CommitOperationAdd(path_in_repo, path_or_fileobj) -- the read path can
+ # arrive positionally, so every positional arg has to be checked.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r', operations=[CommitOperationAdd('x', '/etc/passwd')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operations_positional_absolute_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " 'r', [CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operations_tuple_absolute_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=(CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x'),),\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operations_from_variable_blocked(self):
+ # The ops list is opaque to the static checker, so it cannot be allowed.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "ops = [CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x')]\n"
+ "huggingface_hub.HfApi().create_commit(repo_id='r', operations=ops)",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operation_element_from_variable_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "op = CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x')\n"
+ "huggingface_hub.HfApi().create_commit(repo_id='r', operations=[op])",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operations_via_kwargs_splat_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "kw = {'operations': [CommitOperationAdd(\n"
+ " path_or_fileobj='/etc/passwd', path_in_repo='x')]}\n"
+ "huggingface_hub.HfApi().create_commit(repo_id='r', **kw)",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_operation_tuple_relative_allowed(self):
+ _ok(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=(CommitOperationAdd(path_or_fileobj='m.bin', path_in_repo='m.bin'),),\n"
+ ")"
+ )
+
+ def test_create_commit_operation_positional_relative_allowed(self):
+ _ok(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " 'r', [CommitOperationAdd('m.bin', 'outputs/m.bin')],\n"
+ ")"
+ )
+
+ def test_create_commit_operation_open_relative_allowed(self):
+ _ok(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationAdd(\n"
+ " path_in_repo='m.bin', path_or_fileobj=open('m.bin', 'rb'))],\n"
+ ")"
+ )
+
+ def test_create_commit_delete_operation_blocked(self):
+ # A delete reads no local file, but the exemption would have to trust a
+ # constructor name the sandboxed code can rebind, so every operation is
+ # held to the path rule. This matches the behaviour before the gate.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationDelete\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r', operations=[CommitOperationDelete(path_in_repo='old.bin')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_positional_args_splat_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "args = ['r', [CommitOperationAdd('x', '/etc/passwd')]]\n"
+ "huggingface_hub.HfApi().create_commit(*args)",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_operation_computed_path_in_repo_blocked(self):
+ # The read path is safe, but path_in_repo is computed and its value is
+ # sent to the Hub, so the file contents leak through the repo path.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationAdd(\n"
+ " path_or_fileobj='safe.bin',\n"
+ " path_in_repo=open('/etc/machine-id').read().strip())],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_shadowed_no_read_constructor_blocked(self):
+ # A local def can rebind CommitOperationDelete to return an Add.
+ _blocked(
+ "import huggingface_hub\n"
+ "def CommitOperationDelete(path_in_repo):\n"
+ " return huggingface_hub.CommitOperationAdd('x', '/etc/hostname')\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r', operations=[CommitOperationDelete(path_in_repo='old')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_no_operations_allowed(self):
+ _ok(
+ "import huggingface_hub\n"
+ "huggingface_hub.HfApi().create_commit(repo_id='r', operations=[])"
+ )
+
+ def test_preupload_lfs_files_absolute_blocked(self):
+ # preupload_lfs_files ships the bytes to the LFS store by itself, so it
+ # exfiltrates without a create_commit ever running.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.preupload_lfs_files(\n"
+ " repo_id='r',\n"
+ " additions=[CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_preupload_lfs_files_from_variable_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "adds = [CommitOperationAdd(path_or_fileobj='/etc/passwd', path_in_repo='x')]\n"
+ "huggingface_hub.preupload_lfs_files(repo_id='r', additions=adds)",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_create_commit_trailing_kwargs_splat_blocked(self):
+ # The splat can follow operations=, so the whole keyword list is scanned
+ # before the operation argument is resolved.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "kw = {'token': 'attacker'}\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationAdd(path_or_fileobj='m.bin', path_in_repo='m.bin')],\n"
+ " **kw,\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_delete_operation_computed_argument_blocked(self):
+ # The constructor reads no file, but evaluating its argument does, and the
+ # value is sent to the Hub.
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationDelete\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationDelete(\n"
+ " path_in_repo=open('/etc/hostname').read().strip())],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_copy_operation_computed_argument_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationCopy\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationCopy(\n"
+ " src_path_in_repo='a', path_in_repo=open('/etc/hostname').read())],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_copy_operation_blocked(self):
+ _blocked(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationCopy\n"
+ "huggingface_hub.HfApi().create_commit(\n"
+ " repo_id='r',\n"
+ " operations=[CommitOperationCopy(src_path_in_repo='a', path_in_repo='b')],\n"
+ ")",
+ expect_phrase = "HF upload path must be a sandbox-local relative-path literal",
+ )
+
+ def test_preupload_lfs_files_relative_allowed(self):
+ _ok(
+ "import huggingface_hub\n"
+ "from huggingface_hub import CommitOperationAdd\n"
+ "huggingface_hub.preupload_lfs_files(\n"
+ " repo_id='r',\n"
+ " additions=[CommitOperationAdd(path_or_fileobj='m.bin', path_in_repo='m.bin')],\n"
+ ")"
+ )
+
class TestHfUploadEnvAndSecretLeakBlock:
"""HF upload gate rejects any arg sourced from os.environ / os.getenv /